URI vs URL vs URN

4.6k views Asked by At

Well there are lot of discussion, post, comments and questions over internet to differentiate URI, URL and URN. One answer on SO explain about it, but i am confused in implementation result in my code.

Q : If URI is super set of URL then how come it got this following output:

URI : /XXX/abc.do 

URL : http://examplehost:8080/XXX/abc.do

When i write the below code:

System.out.println(“URI : “+ httpRequestObj.getRequestURI());
System.out.println(“URL : “+ httpRequestObj.getRequestURL());

EDIT : Could you share a detailed answer by keeping JAVA and original concept of URI,URL and URN in scope.

Regards,

Arun Kumar

3

There are 3 answers

7
Stephen C On BEST ANSWER

If URI is super set of URL then how come it got this following output ...

The definitions of URI and URL cannot be used to infer the behaviour of getRequestURI() and getRequestURL(). To understand what the methods return, you need to read the javadocs and the Servlet specification.

The meaning of those methods are what they are because the HttpRequest API has evolved over time, and that evolution has had to maintain backwards compatibility.

getRequestURI() does return a URI, and getRequestURL() does return a URL, but the URI and URL are for different things.

4
Evgeniy Dorofeev On

java.net.URI API gives a good explanation:

A URI is a uniform resource identifier while a URL is a uniform resource locator. Hence every URL is a URI, abstractly speaking, but not every URI is a URL. This is because there is another subcategory of URIs, uniform resource names (URNs), which name resources but do not specify how to locate them. The mailto, news, and isbn URIs shown above are examples of URNs.

0
Ala'a mansour On

imagine you have a book. The URL (Uniform Resource Locator) is like telling someone the exact address of the library where the book is kept. It includes the specific location, like the street name and building number.

Now, URI (Uniform Resource Identifier) is a bit more general. It's like saying, "Look in the library on the science section shelf." It gives a general idea of where to find something but not the exact location.

Lastly, URN (Uniform Resource Name) is like a name tag on the book itself. It doesn't say where the book is in the library; it just gives it a unique name. So, URI is like telling where to find it roughly, and URN is like giving it a special name.

so in conclusion a URL might be also a URN and both are URIs

enter image description here