How to determine from a filter which page serviced a forward from RequestDispatcher?

╄→гoц情女王★ 提交于 2019-12-12 02:46:58

问题


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
            }
        }
    } catch (Exception e) {
        System.out.println("-- ERROR IN PageLogFilter: " + e.getLocalizedMessage());
    }
    chain.doFilter(request, response);
}
}

回答1:


Call request.getRequestURI() after chain.doFilter(..)



来源:https://stackoverflow.com/questions/9011106/how-to-determine-from-a-filter-which-page-serviced-a-forward-from-requestdispatc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!