逻辑是过滤器被命中,条件不成立,所以它通过过滤器链。提交响应后,过滤器被命中,现在条件为真(设置了请求属性)。它进入执行转发,但页面从不转发。我知道这与提交的响应有关,因为我测试了它在第一次到达链之前转发的不同逻辑,并且它确实转发成功。
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest httpServletRequest = (HttpServletRequest)request;
if (some condition equals true) {
httpServletRequest.getRequestDispatcher("/home.jsp").forward(request, response);
return;
} else {
chain.doFilter(request, response);
}
}
我的部署描述符中的示例:
<filter>
<filter-name>MyFilter</filter-name>
<filter-class>com.filters.MyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>MyFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>