How do I get request url in jsf managed bean without the requested servlet?
Assuming the URL is http://localhost:8080/project-name/resource.xhtml,
I want to obtain the following http://localhost:8080/project-name in a JSF managed bean.
Solution 1:
I'll assume that you are using JSF 2 and Java EE 6 for this answer.
The implementation of the actual mechanism will vary depending on the extent to which you'll need the original URL.
You'll first need to get access to the underlying servlet container (assumed to one, instead of a portlet container) produced HttpServletRequest object. Use the FacesContext
object to access the HttpServletRequest object in the following manner:
HttpServletRequest origRequest = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
The HttpServletRequest
class provides several utility methods to obtain a near representation of the original request:
-
getRequestURL()
, which provides the original request sans the query string -
getScheme
,getServerName
,getServerPort
,getContextPath
,getServletPath
,getPathInfo
andgetQueryString
all of whose outputs can be combined in sequence to obtain the original request. You may have to omit the latter invocations if you want a lesser fragment of the URL.
Solution 2:
You can get it as follows:
HttpServletRequest req = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
String url = req.getRequestURL().toString();
return url.substring(0, url.length() - req.getRequestURI().length()) + req.getContextPath() + "/";
// ...
Note that there are possibly better ways to achieve the requirement. Getting the raw Servlet API inside a JSF managed bean is a code smell alarm.
Solution 3:
You can avoid container-specific dependencies by using the ExternalContext
in something like this form:
public String getApplicationUri() {
try {
FacesContext ctxt = FacesContext.getCurrentInstance();
ExternalContext ext = ctxt.getExternalContext();
URI uri = new URI(ext.getRequestScheme(),
null, ext.getRequestServerName(), ext.getRequestServerPort(),
ext.getRequestContextPath(), null, null);
return uri.toASCIIString();
} catch (URISyntaxException e) {
throw new FacesException(e);
}
}
However, note that this code may not be entirely container-agnostic - some of those methods throw an UnsupportedOperationException
in their default implementation. This code relies on JSF 2.0 methods.
You should also note that using a URI like this as a base is not the correct way to refer to a resource in your application in the general case; the ViewHandler
and ExternalContext
should be used in concert to create resource URLs for referring to application resources to fetch resources or action URLs for invoking the JSF lifecycle, for example.
Unfortunately, I don't think there is a general, container-agnostic way to do everything you might want to do in a JSF app, so sometimes you're dependent on the implementation and you have little choice but to cast down to other APIs.
Solution 4:
The best way is to access to the ExternalContext RequestHeaderMap attributes.
ExternalContext ext = FacesContext.getCurrentInstance().getExternalContext();
Map<String,String> requestHeader = ext.getRequestHeaderMap();
urlRefered = requestHeader.get("referer");
You could save the urlRefered attribute in your bean and process in your xhtml page as following:
<h:outputText value="#{notFoundBean.urlRefered}
Don't forget mapping your error page in your web.xml file too.
<error-page>
<error-code>404</error-code>
<location>/xhtml/pg/error/404.xhtml</location>
</error-page>
By other side you also could get it from this line directly to the xhtml page:
<h:outputText value="#{requestScope['javax.servlet.forward.request_uri']}" />