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, Used to indicate the location of resources, such as: Imgurl,logouri, etc. 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 article at the beginning of the question raised, 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 up to the query string in the first line of the HTTP request ", such as" Post/some/path.html?a=b http/1.1 ", the value returned is"/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 not include query string parameters. ", full request resource path, not including QueryString. Summary: 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: absolute (absolute) here refers to include scheme, while relative (relative) does not include scheme. uri Abstract Structure [
Scheme
:]
Scheme-specific-part[
#
Fragment] [
Scheme
:][
//
Authority][
Path][
?
Query][
#
Fragment] authority is [
User-info
@]
Host[
:
Port] Reference: 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.
The difference between a URI and a URL