2

I have a filter which processes requests in order to log them, so I can keep track of which session hit a page at what time with what request parameters. works great... posting from jsp to jsp, or making a direct call to a jsp. When a form is posted to a servlet which forwards that request to a new jsp, however, I am unable to see which jsp the request was forwarded to.

For example, suppose I have a login page, which posts to a LoginServlet, which then forwards the request to either index.jsp or index1.jsp. How can I determine from the request whether LoginServlet is returning index.jsp or index1.jsp?

This is in a java 1.5 environment using the 2.3 servlet specification.

public class PageLogFilter implements Filter {

    FilterConfig filterConfig = null;

    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
    }

    public void destroy() {
        this.filterConfig = null;
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        try {
            if (request instanceof HttpServletRequest) {
                HttpServletRequest req = (HttpServletRequest) request;
                HttpSession session = req.getSession(false);

                //For non-forwards, I can call req.getRequestURI() to determine which 
                //page was returned. For forwards, it returns me the URI of the  
                //servlet which processed the post. I'd like to also get the URI
                //of the jsp to which the request was forwarded by the servlet, for
                //example "index.jsp" or "index1.jsp"
            }
        } catch (Exception e {
            System.out.println("-- ERROR IN PageLogFilter: " + e.getLocalizedMessage());
        }

        chain.doFilter(request, response);
    }
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
rpierce
  • 58
  • 7
  • possible duplicate of [How to determine from a filter which page serviced a forward from RequestDispatcher?](http://stackoverflow.com/questions/9011106/how-to-determine-from-a-filter-which-page-serviced-a-forward-from-requestdispatc) – Bozho Jan 27 '12 at 19:34

2 Answers2

2

If you are OK with performing an additional check then you can use attribute to set/get original request URI. In your LoginServlet set the attribute:

//LoginServlet
public void doFilter(...) {
      HttpServletRequest oReq = (HttpServletRequest)request;
      ..
      ...
      //save original request URI
      oReq.setAttribute("originalUri", oReq.getRequestURI());
}

and in your PageLogFilter check if originalUri attribute has value then consider this value as the request URI

//PageLogFilter 
public void doFilter(...) {
      HttpServletRequest req = (HttpServletRequest) request;
      if(req.getAttribute("originalUri") != null) {
          String strOriginalUri = (String) req.getAttribute("originalUri");
          //set it back to null
      req.setAttribute("originalUri", null);
      }
}
Waqas
  • 6,812
  • 2
  • 33
  • 50
  • Thans Waqas, that will definately work and I will implement it if no other solution presents. Is there no other way to do this without having to add that code to every servlet I write? – rpierce Jan 27 '12 at 16:29
0

Although it wont help solve your immediate problem, Servlet 2.4 added further detailed control on the dispatcher, which is what you want.

With it, you can configure the filter to add the following dispatcher element, which would cause the filter to also apply to forwards.

<filter-mapping>
       <filter-name>PageLogFilter</filter-name>
       <url-pattern>/*</url-pattern>
       <dispatcher>FORWARD</dispatcher>
</filter-mapping>

The following article explains it in more detail

http://www.ibm.com/developerworks/java/library/j-tomcat2/

Patrick
  • 2,102
  • 15
  • 11