过滤器中确定哪个页面为RequestDispatcher的转发服

过滤器中确定哪个页面为RequestDispatcher的转发服

本文介绍了如何从过滤器中确定哪个页面为RequestDispatcher的转发服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个过滤器,该过滤器处理请求以记录请求,因此我可以跟踪哪个会话在什么时间以什么请求参数访问了页面.效果很好...从jsp发布到jsp,或直接调用jsp.但是,当将表单发布到将请求转发到新的jsp的servlet时,我无法看到将请求转发到哪个jsp.

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.

例如,假设我有一个登录页面,该页面发布到LoginServlet,然后将请求转发到index.jsp或index1.jsp.如何从请求中确定LoginServlet返回的是index.jsp还是index1.jsp?

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?

这是在使用2.3 Servlet规范的Java 1.5环境中.

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);
}
}

推荐答案

chain.doFilter(..)

这篇关于如何从过滤器中确定哪个页面为RequestDispatcher的转发服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 10:01