views:

394

answers:

1

I've got a jspx that needs to know the current HttpServletRequest's getServerName(). The jspx can fetch this with #{mybean.serverName} from its bean, like this:

public String getServerName() {
  HttpServletRequest request = (HttpServletRequest) FacesInstance.getCurrentInstance().getExternalContent().getRequest();
  return request.getServerName();
}

However, when this is served behind a proxy (Apache with mod_proxy), getServerName() will some times return the node's host name instead of the frontend's host name. Other times it works correctly.

A plain jsp with <% request.getServerName(); %> will, however, always return the frontend's host name.

What's the problem with FacesInstance's HttpServletRequest? Is there a way to fetch the "real" request object?

+2  A: 

What's the problem with FacesContext's HttpServletRequest?

You are getting the underlying implementation request (which happens to be the servlet API). This has little to do with JSF since you are leaving the API to call an object provided by the container to JSF's servlet.

The JSP request object is also a javax.servlet.ServletRequest; they are likely to be the same object.

There is a possibility that the request is being wrapped (e.g. by a HttpServletRequestWrapper), but it is hard to see what would be gained by altering the return value from getServerName().

I suspect there is some side-effect to the way the host name is resolved. I would look into how the implementation of getServerName() works in your server.

FYI: you should be able to reference the value without a managed bean. The EL expression ${pageContext.request.serverName} (untested) should return the value (pageContext is an implicit variable in JSPs).

McDowell
+1 and yes `${pageContext.request.serverName}` is legal.
BalusC
${pageContext.request.serverName} is empty in my .jspx. I tried with #{pageContext.request.serverName} as well, also without success.
neu242
@neu242: that's caused by the proxy and probably also DNS.
BalusC
@BalusC: I don't think so, it acts the same way when I run it without the proxy. Other values, such as getScheme(), are also empty.
neu242
#{facesContext.externalContext.request.serverName} works right now, but who knows if it will suddenly drop back to displaying the node's host name..?
neu242
@neu242 - it appears that `pageContext` cannot be used with deferred evaluation (`#{}`). Since JSF views exist beyond the JSP page and the page context doesn't, this is understandable. If possible, you should step-debug into `getServerName()`. I still feel the answer lies in there.
McDowell