These two days are being writtenCodeBecause of the resource location, you need to define some fields in Java Bean to indicate the resource location, such as imgurl and logouri. But every time I define it, I am very confused. Should I use imgurl or imguri?
Similarly, another question is: What is the difference between string httpservletrequest. getrequesturi (); and stringbuffer httpservletrequest. getrequesturl? Why?
I searched the internet with these questions and did not find any clear explanation for myself. So I thought that the Java class library has two corresponding classes java.net. uri and java.net. finally, the answer is found in the javadoc of these two classes.
Uris, URLs, and urns
First, Uri is an Uniform Resource Identifier. It is a unified resource identifier that uniquely identifies a resource. The URL is the uniform resource locator, which is a uniform resource locator. It is a specific URI, that is, the URL can be used to identify a resource and specify how to locate the resource. While urn, Uniform Resource Name, unified resource name, is identified by the name of the resource, such as mailto: java-net@java.sun.com. That is to say, Uri is an abstract high-level concept that defines a unified resource identifier, while URL and urn are specific resource identifiers. URL and urn are both URI types.
In Java Uris, a URI instance can represent absolute or relative, as long as it complies with the URI syntax rules. The URL class not only conforms to the semantics, but also contains information for locating the resource. Therefore, it cannot be relative and the schema must be specified.
OK, answer nowArticleIs imgurl good or imguri good? Obviously, imguri is certainly okay, because even if it is actually a URL, it is also a URI. Is there a problem with using imgurl? It depends on its possible values. If it is an absolute path and can be located, it is okay to use imgurl. If it is a relative path, it is better not to use imgurl. In short, imguri is certainly no problem, while imgurl depends on the actual situation.
Second, we can see from the javadoc of httpservletrequest that 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 returned value is"/Some/path.html ". Now we can understand why it is getrequesturi instead of getrequesturl, because the relative path is returned here. 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. ", the complete request resource path, excluding querystring.
To sum up, a URL is a specific URI that not only uniquely identifies a resource, but also provides information for locating the resource. Uri is a semantic abstract concept. It can be absolute or relative, while URL must provide sufficient information for locating. Therefore, it is absolute, generally, the relative URL is targeted at another absolute URL, which is basically absolute.
Note: Here, absolute (absolute) refers to scheme, while relative does not.
Uri abstract Structure[Scheme:]Scheme-specific-part[#Fragment]
[Scheme:] [//Authority] [Path] [?Query] [#Fragment]
Authority is [User-Info@]Host[:Port]
References:
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 for special characters specified by rfc2396. Therefore, the caller must encode each part of the URL. Java.net. Uri provides the escape function. Therefore, the recommended way to manage the encoding and decoding of URLs is to use java.net. uri. you can use Uri. tourl () and URL. touri () method to convert two types of objects. For the URL encode/Decode of HTML form, you can use java.net. urlencoder and java.net. urldecoder, but they are not applicable to URL objects.