Difference Between URL and URN in Java


The modern day 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. In this article, we will point out some differences between URL and URN in Java.

URL vs URN in Java

URL

Its full form is Uniform Resource Locator. It provides the feature to locate and access resources distinctly over the Internet by utilizing 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'.

Here,

  • 'https://' is the protocol used.

  • example.com is the domain name

  • /login is the path or endpoint on the server

  • ?username=User123&password=MyPasswrd represents the query string.

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 asisbn 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.

Example

In the following example, we will use a few in-built methods and classes of ‘java.net’ package to check the type of a file available on the Internet.

import java.net.*;
public class FileTypes {
   public static void main(String args[]) throws Exception {
      try {
         // link of file
         URL shareLink = new URL("https://www.tutorialspoint.com/java/pdf/java_networking.pdf");
         // making connection with the file
         URLConnection urlConn = shareLink.openConnection();
         // 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 file type!!");
      }
   }
}

Output

Type of the content: null

In the above code, we have created an instance of the URL class and passed a link of file to its constructor. Then, using the built-in method getContentType(), we determined the type of given file. But, before that, we need to establish a connection with the file using openConnection() method of URL class.

Difference between URL and URN

The below table shows the differences between URL and URN −

URL

URN

We can use it to locate and retrieve a resource on the Internet

It can only be used to identify a resource on the Internet.

URL can be updated if the corresponding resource gets modified.

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

It is not associated with any URN.

It may have one or more URLs associated with it

It defines how to obtain the resource.

It defines only the name of a certain resource.

Conclusion

The URI is the superset of URN and URL. Both have different purposes and syntaxes. A URL specifies the location and access method of a resource, while a URN specifies the name or identity of a resource.

Updated on: 21-Jul-2023

51 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements