逻辑是过滤器被命中,条件不为真,所以它通过过滤器链。提交响应后,过滤器被命中,并且条件现在为真(请求属性已设置)。它进去执行转发,但页面从未转发。我知道这与提交的响应有关,因为我测试了它在第一次到达链之前转发的不同逻辑,并且它确实转发成功。
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>
的“承诺”状态HttpServletResponse
实际上是一种表示响应头是否已写入底层套接字的方法。 “承诺”的回应已经(至少)写下了第一行。由于响应的第一行包含状态代码,因此您无法更改已提交响应的状态代码...这意味着将状态更改为 3xx 来进行重定向为时已晚。同样,您无法执行本地转发,因为您已经开始发送响应。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)