如何从过滤器确定 RequestDispatcher.forward 调用转发到哪个 jsp?

发布于 2024-12-29 12:04:16 字数 1549 浏览 2 评论 0原文

我有一个过滤器,用于处理请求以记录它们,因此我可以跟踪哪个会话在什么时间使用什么请求参数访问页面。效果很好...从 jsp 发送到 jsp,或者直接调用 jsp。但是,当将表单发布到 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, for
                //example "index.jsp" or "index1.jsp"
            }
        } catch (Exception e {
            System.out.println("-- ERROR IN PageLogFilter: " + e.getLocalizedMessage());
        }

        chain.doFilter(request, response);
    }
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧时光的容颜 2025-01-05 12:04:17

如果您同意执行额外检查,则可以使用属性来设置/获取原始请求 URI。
在您的 LoginServlet 中设置属性:

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

并在您的 PageLogFilter 中检查originalUri 属性是否有值,然后将该值视为请求 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);
      }
}

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);
      }
}
寄居人 2025-01-05 12:04:17

虽然它不能帮助解决您眼前的问题,但 Servlet 2.4 添加了对调度程序的更详细的控制,这正是您想要的。

使用它,您可以配置过滤器以添加以下调度程序元素,这将导致过滤器也适用于转发。

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

以下文章更详细地解释了它

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

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文