3

JSF 1.2-1.2_07-b03-FCS

JSTL 1_1-mr2 (special build)

Java 1.6.0_22-b04

Eclipse 3.6.0 (Helios)

Tomcat 6.0.28 (needs to run also on Weblogic)

IE 7.0.5730.13

Firefox: 6.0

We have page: http://{host:port}/mybase/faces/mypage.jsp...

It is called from multiple external pages via hyperlink, redirect, etc.

We would like to determine the URL of the page that called it (in order to implement a command button "back" button) in a pure "JSF" manner.

We know we can do this:

    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = facesContext.getExternalContext();        
    HttpServletRequest origRequest = 
        (HttpServletRequest)externalContext.getRequest();
    String referrer = origRequest.getHeader("referer"); 

This, however, requires the HttpServletRequest which requires including the servlet-api.jar file.

The question: can this be done in a pure JSF manner and thus, without including the servlet-api.jar file?

Thanks, John

John K
  • 111
  • 1
  • 4
  • 11

1 Answers1

5

This, however, requires the HttpServletRequest which requires including the servlet-api.jar file

This makes no sense. JSF at its own has already a Servlet API dependency. Perhaps you're referring to the compilation error in your IDE because the project isn't associated with a target runtime at all? In that case, please read this carefully: How do I import the javax.servlet API in my Eclipse project?

As to the concrete question, just use ExternalContext#getRequestHeaderMap() to get a mapping of the request headers.

String referrer = externalContext.getRequestHeaderMap().get("referer"); 
// ...
Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Yes, it works. Went down a lot of wrong paths, but missed the getRequestHeaderMap(). Thank you. John – John K Sep 08 '11 at 18:05
  • Ouch! - point taken. Actually, I did spend more time with the API than I will admit too - just looking for the wrong things. Many internet searches, but nothing found was as clean as your solution. John – John K Sep 08 '11 at 18:27
  • No problem. It helps to memorize that the `ExternalContext` is the key entry point to the "raw" Servlet API classes and methods which JSF is using under the covers, such as `HttpServletRequest`, `HttpServletResponse`, `HttpSession` and `ServletContext`. There are indeed a lot of getters. The `ExternalContext` part in my answer is clickable and points to the API doc directly. – BalusC Sep 08 '11 at 18:29