The difference between a URI and a URL

Source: Internet
Author: User

Transferred from: http://www.cnblogs.com/gaojing/archive/2012/02/04/2413626.html

These two days when writing code, because of the location of the resources involved, so you need to define some fields in the Java bean to represent the location of the resources, such as: Imgurl,logouri and so on. However, every time the definition of time, the heart is very tangled, is to use Imgurl or imguri it?

Similarly, another question: String Httpservletrequest.getrequesturi (); and StringBuffer Httpservletrequest.getrequesturl (); What is the difference in what is returned? Why is that so?

With these questions to the online search, did not find to let himself read the explanation, so, think of Java class Library has two corresponding classes Java.net.URI and Java.net.URL, finally, in these two classes Javadoc found the answer.

URIs, URLs, and urns

First, the URI, which is the Uniform Resource identifier, is the Uniform Resource identifier used to uniquely identify a resource. The URL is a Uniform Resource Locator, a Uniform Resource locator, which is a specific URI that the URL can use to identify a resource and also how to locate the resource. and Urn,uniform resource Name, unified resource naming, is the name to identify resources, such as Mailto:[email protected]. That is, URIs define uniform resource identities in an abstract, high-level concept, whereas URLs and urns are specific resource identities. URLs and urns are all a kind of URI.

In the Java URI, a URI instance can represent absolute or relative, as long as it conforms to the syntax rules of the URI. The URL class not only conforms to semantics, but also contains information that locates the resource, so it cannot be relative, and the schema must be specified.

OK, now answer the questions raised at the beginning of the article, in the end is Imgurl good, or Imguri good? Obviously, if Imguri is definitely not a problem, because even if it is actually a URL, it is also a URI. So is there a problem with Imgurl? At this point to see its possible value, if it is an absolute path, can be positioned, then use Imgurl is not a problem, and if it is a relative path, that still do not use imgurl good. In short, with Imguri is sure no problem, and the use of Imgurl depends on the actual situation.

Second, as can be seen from HttpServletRequest's Javadoc, Getrequesturi returns a string, "the part of this request's URL from the protocol name The query string in the first line of the HTTP request ", such as" Post/some/path.html?a=b http/1.1 ", returns the value"/some/path.html ". Now you can see why Getrequesturi is not getrequesturl, because the relative path is returned here. And Getrequesturl returns a StringBuffer, "The returned URL contains a protocol, server name, port number, and server path, but it does Es not include query string parameters. ", complete request resource path, excluding querystring.

To summarize: A URL is a specific URI that uniquely identifies a resource and provides information to locate that resource. A URI is a semantic abstraction that can be absolute or relative, and the URL must provide enough information to locate it, so it is absolute, and the usual relative URL is for another absolute URL, which is essentially absolute.

Note: the absolute (absolute) here refers to the inclusion scheme, while the relative (relative) does not include scheme.

URI abstract structure [scheme:]scheme-specific-part[#fragment]

[scheme:] [//Authority] [Path] [? Query] [#fragment]

authority for [user-info@]host[:Port]

Resources:

Http://docs.oracle.com/javase/1.5.0/docs/api/java/net/URI.html

Http://en.wikipedia.org/wiki/Uniform_Resource_Identifier

Http://docs.oracle.com/javaee/5/api/javax/servlet/http/HttpServletRequest.html

Ps:

The Java.net.URL class does not provide escape from the special characters specified by the standard RFC2396, so the caller is required to encode the components of the URL themselves. The Java.net.URI provides escape functionality. So the recommended to manage the encoding and decoding of the URLs are to use Java.net.URI. You can use the Uri.tourl () and Url.touri () methods to convert two types of objects to one another. URL Encode/decode for HTML form can be done using Java.net.URLEncoder and java.net.URLDecoder, but not for URL objects.

Category: Technical bits

The difference between a URI and a URL

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.