【问题标题】:How to determine from a filter which page serviced a forward from RequestDispatcher?如何从过滤器中确定哪个页面服务于 RequestDispatcher 的转发?
【发布时间】:2012-01-25 22:08:49
【问题描述】:

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

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

这是在使用 2.3 servlet 规范的 java 1.5 环境中。

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

【问题讨论】:

    标签: java servlets servlet-filters forward requestdispatcher


    【解决方案1】:

    chain.doFilter(..)之后调用request.getRequestURI()

    【讨论】:

    • 它返回服务 servlet 的 URI,例如"/LoginServlet"
    • 是的,如果你调用它before chain.doFilter()...如果你转发请求,请求URI会改变
    • 不幸的是它没有 Bozho :-(
    • 恐怕不相关博卓。
    • 表示人们在转发后调用getRequestURI()时正在获取JSP文件。
    猜你喜欢
    • 1970-01-01
    • 2012-02-15
    • 2012-09-04
    • 2011-02-23
    • 2016-05-30
    • 2022-11-07
    • 1970-01-01
    • 2015-09-15
    • 2012-09-11
    相关资源
    最近更新 更多