joi, 26 martie 2015

[OmniFaces utilities (2.0)] Get HTTP request URL / URI / domain URL / base URL


[OmniFaces utilities] The getRequestDomainURL() method returns the HTTP request domain URL. This is the URL with the scheme and domain, without any trailing slash.
[OmniFaces utilities] The getRequestBaseURL() method returns the HTTP request base URL. This is the URL from the scheme, domain until with context path, including the trailing slash. This is the value you could use in HTML <base> tag.
[OmniFaces utilities] The getRequestURL() method returns the HTTP request URL. This is the full request URL as the enduser sees in browser address bar. This does not include the request query string.
[OmniFaces utilities] The getRequestURI() method returns the HTTP request URI. This is the part after the domain in the request URL, including the leading slash. This does not include the request query string.

MethodFaces#getRequestDomainURL() returns the HTTP request domain URL

Method Faces#getRequestBaseURL() returns the HTTP request base URL

MethodFaces#getRequestURL() returns the HTTP request URL

Method Faces#getRequestURI() returns the HTTP request URI

Usage:

Let's suppose the following HTTP request (faces servlet mapping is a prefix mapping, /faces/*):

http://localhost:8080/MyApp/faces/index.xhtml

Further, let's apply the above methods, one by one, as follows:

·         get the HTTP request domain URL (Faces#getRequestDomainURL())
import org.omnifaces.util.Faces;
...
// returns http://localhost:8080
String requestDomainURL = Faces.getRequestDomainURL();

·         get the HTTP request base URL (Faces#getRequestBaseURL())
import org.omnifaces.util.Faces;
...
// returns http://localhost:8080/MyApp/
String requestBaseURL = Faces.getRequestBaseURL();

·         get the HTTP request URL (Faces#getRequestURL())
import org.omnifaces.util.Faces;
...
// returns http://localhost:8080/MyApp/faces/index.xhtml
String requestURL = Faces.getRequestURL();

·         get the HTTP request URI (Faces#getRequestURI())
import org.omnifaces.util.Faces;
...
// returns /MyApp/faces/index.xhtml
String requestURI = Faces.getRequestURI();

In the below figure, you can see these four methods and what they return:


Let's suppose the following HTTP request (faces servlet mapping is a suffix mapping, *.xhtml):

http://localhost:8080/MyApp/index.xhtml

Further, let's apply the above methods, one by one, as follows:

·         get the HTTP request domain URL (Faces#getRequestDomainURL())
import org.omnifaces.util.Faces;
...
// returns http://localhost:8080
String requestDomainURL = Faces.getRequestDomainURL();

·         get the HTTP request base URL (Faces#getRequestBaseURL())
import org.omnifaces.util.Faces;
...
// returns http://localhost:8080/MyApp/
String requestBaseURL = Faces.getRequestBaseURL();

·         get the HTTP request URL (Faces#getRequestURL())
import org.omnifaces.util.Faces;
...
// returns http://localhost:8080/MyApp/index.xhtml
String requestURL = Faces.getRequestURL();

·         get the HTTP request URI (Faces#getRequestURI())
import org.omnifaces.util.Faces;
...
// returns /MyApp/index.xhtml
String requestURI = Faces.getRequestURI();

In the below figure, you can see these four methods and what they return:

Niciun comentariu:

Trimiteți un comentariu