Difference Between URL, URI and URN in Java


The modern Internet is all about the World Wide Web which holds billions of websites and resources. There are several ways to access those web-based resources by following the protocols. Java has the concept of networking that is used to establish communication between clients and those resources. But, to locate a specific resource among the millions available, we need a unique identifier. There are three components: URI, URL and URN that helps us locate a certain resource on the web. Together they serve a single purpose, but through this article, we will discuss the difference between URI, URL and URN.

URI vs URL vs URN in Java

URI

It is an abbreviation of Uniform Resource Locator. It encompasses a sequence of characters that identifies a resource on the web. It defines a scheme such as http, ftp, mailto, and so forth that specifies how to access the resource. A URI enables the use of various resource identifiers within the same context, regardless of the differences in the methods used to access those resources. Java provides the URI class in ‘java.net’ package.

Syntax

URI nameOfObject = new URI( "URIspecifier" );

Here, URIspecifier is the actual URI or we can say link of the resource.

Instance

https://example.com/resource?id=12345&category=books

In the above example URI, we have a hypothetical domain 'example.com' followed by a resource path '/resource'. It also includes two query parameters named 'id' with a value of '12345' and 'category' with a value of 'books'.

URL

Its full form is Uniform Resource Locator. It provides the feature to locate and access resources distinctly over the Internet by means of the address of the given resource. There are four components of a URL −

  • Protocol

  • Host name or IP address

  • Port number

  • Path

The URL class of Java networking is used to make connections or to locate resources across the Internet with the help of URLs. This class throws MalformedURLException

Syntax

URL nameOfObject = new URL( "URLspecifier" );

Where

URLspecifier is the actual URL or we can say link of the resource. It must be enclosed in double quotes

Instance

https://example.com/login?username=User123&password=MyPasswrd

The above example URL indicates that the user is attempting to log in to the ‘example.com’ website using the username 'User123' and the password 'MyPasswrd'.

URN

It is an acronym that stands for Uniform Resource Name. It defines the name of a certain resource. In other words, URN is a specific type of URI that identifies a resource by its name rather than its location. It has the scheme urn and a namespace identifier such as isbn and uuid that specifies the naming authority for the resource. Due to the unavailability of a universal standard for naming an object on the Internet, this method of identifying resources has failed.

Instance

urn:example:resource:books:12345

In the above example URN, we have used the 'example' namespace, followed by a resource identifier 'resource:books:12345'. The URN format consists of a hierarchical structure with different components separated by colons.

Approach

  • First, define an instance of URI class and passed a link of file to its constructor.

  • Again, define an instance of URL class is defined using the object of URI so that we can make a connection with the given link.

  • Now, Using the getScheme() method, retrieve the name of the protocol used in the link.

  • In the end, use the built-in method getContentType() to determine the type of given file.

Example

The following example illustrates the practical implementation of URI and URL in a Java program.

import java.net.*;
public class URIExample {
   public static void main(String args[]) throws Exception {
      try {
         // link of file
         URI uri = new URI("https://www.tutorialspoint.com/java/pdf/java_networking.pdf");
         // creating URL object from the URI
         URL url = uri.toURL();
         // making connection with the file
         URLConnection urlConn = url.openConnection();
         // retrieving the name of protocol
         String scheme = uri.getScheme();
         System.out.println("The name of protocol is: " + scheme);
         // retrieving type of the file
         System.out.println("Type of the content: " + urlConn.getContentType());
      }
      catch(Exception exp) {
         System.out.println("Something went wrong!! Please check the link!!");
      }
   }
}

Output

The name of protocol is: https
Type of the content: null

Difference between URI, URL and URN

URI

URL

URN

It can locate and access a resource available on the Internet.

It can also locate and retrieve a resource on the Internet.

It can only name a resource.

URI can be updated if the corresponding resource gets modified.

URL can also be updated if the corresponding resource gets modified

It is persistent and does not depend on the location or name of the resource

The URI consists of URN and URL.

URL is the subset of URI

URN is the subset of URI

Conclusion

In this article, we have explained the components that are used to locate resources available on the web. Those components are URI, URL and URN. The URI is the superset of URN and URL. It can identify a resource by its name, location or both.

Updated on: 21-Jul-2023

181 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements