Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get request url in jsf managed bean without the requested servlet?

Tags:

java

jsf

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.

like image 502
Lan Avatar asked Jun 01 '11 05:06

Lan


3 Answers

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 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.
like image 125
Vineet Reynolds Avatar answered Oct 31 '22 21:10

Vineet Reynolds


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.

like image 26
BalusC Avatar answered Oct 31 '22 19:10

BalusC


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.

like image 7
McDowell Avatar answered Oct 31 '22 21:10

McDowell