3

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

Community
  • 1
  • 1
Arun Kumar
  • 6,534
  • 13
  • 40
  • 67

2 Answers2

6

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.

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
  • So does it mean `/XXX/abc.do` is a super-set of `http://examplehost:8080/XXX/abc.do` ?? – Arun Kumar Sep 03 '13 at 10:24
  • @ArunKumar - No. "/XXX/abc.do" is not a "set", so it cannot be a "super-set" of anything. – Stephen C Sep 03 '13 at 10:31
  • 1
    I am not sure what relations are between these two. URI API does not define super-set notion. According to API the first one is relative, the second is absolute. API also defines 3 operations between URI instances - normalization, resolution, and relativization. – Evgeniy Dorofeev Sep 03 '13 at 10:32
  • @EvgeniyDorofeev - In fact, this is not really about the `URL` and `URI` APIs. The OP is confused about the meaning of the Strings returned by a couple of methods of the `HttpServletRequest` class. – Stephen C Sep 03 '13 at 10:46
1

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.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Yes, as per JavaDoc, `getRequestURI()` Returns the part of this request's URL from the protocol name up to the query string in the first line of the HTTP request whereas `getRequestURL()` returns the string contains a protocol, server name, port number, and server path, but it does not include query string parameters. – Arun Kumar Sep 03 '13 at 10:45
  • 2
    Correct. The explanation for what you observed is given by the javadocs ... not by any logical inferences from the definitions of URI, URL or URN. – Stephen C Sep 03 '13 at 10:48
  • *"Could you share a detailed answer by keeping JAVA and original concept of URI,URL and URN in scope."* - My answer already does that. – Stephen C Sep 03 '13 at 12:34
  • Yes but not completely. You are explaining that `getRequestURI()` and `getRequestURL()` in `HttpServerletRequest` are different from URI and URL concept. – Arun Kumar Sep 04 '13 at 04:51
  • @ArunKumar - Yes. They are. What *else* do you want to explain? – Stephen C Sep 04 '13 at 11:34
  • I want to know if If URI is super set of URL as per w3 definitions, how can i find it in JAVA? – Arun Kumar Sep 05 '13 at 05:49
  • i think Stephen is right here as per original question. I need to put another question for my above comment or someone can answer it here. – Arun Kumar Sep 05 '13 at 05:51