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.
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 stringgetScheme
, getServerName
, getServerPort
, getContextPath
, getServletPath
, getPathInfo
and getQueryString
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.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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With