首先,在处理我的问题之前,我将向您介绍我的测试用例。我的基本 Maven Web 应用程序中有几个组件:
- page.xhtml:用于生成我的请求/回复(启动过滤)
- Pretty Faces:用于根据客户的需要重新定义 URL
- FirstFilter:在 Pretty Faces 之前执行(在这个体育场进行测试)
- ThirdFilter:在 Pretty Faces 之后执行(在这个体育场进行测试)
- web.xml:定义我的完整过滤器链的行为
我将分享重要组件的代码。
漂亮的配置.xml
<url-mapping id="page">
<pattern value="/page" />
<view-id value="/page.xhtml" />
</url-mapping>
FirstFilter.java
@WebFilter
public class FirstFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println("FirstFilter, request: " +
((HttpServletRequest)request).getRequestURL().toString());
chain.doFilter(request, response);
System.out.println("FirstFilter, response");
}
// override init and destroy
}
第三过滤器.java
@WebFilter
public class ThirdFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println("ThirdFilter, request: " +
((HttpServletRequest)request).getRequestURL().toString());
chain.doFilter(request, response);
System.out.println("ThirdFilter, response");
}
// override init and destroy
}
web.xml
<filter>
<filter-name>FirstFilter</filter-name>
<filter-class>nl.mhoogeveen.nl.rootapplication.FirstFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>FirstFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>Pretty Filter</filter-name>
<filter-class>testingapplications.filterchaining.PrettyFilter</filter-class>
<async-supported>true</async-supported>
</filter>
<filter-mapping>
<filter-name>Pretty Filter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>FORWARD</dispatcher>
<dispatcher>REQUEST</dispatcher>
<dispatcher>ERROR</dispatcher>
<dispatcher>ASYNC</dispatcher>
</filter-mapping>
<filter>
<filter-name>ThirdFilter</filter-name>
<filter-class>testingapplications.filterchaining.ThirdFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>ThirdFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
情况
调用 localhost:8080/page.xhtml (因此不激活 Pretty Faces 重定向)
INFO: FirstFilter, request: http://localhost:8080/page.xhtml
INFO: ThirdFilter, request: http://localhost:8080/page.xhtml
INFO: ThirdFilter, response
INFO: FirstFilter, response
调用 localhost:8080/page (从而激活 Pretty Faces 重定向)
INFO: FirstFilter, request: http://localhost:8080/page
INFO: FirstFilter, response
问题
是什么导致我的链条不完整的这种情况?它不会被切断,因为我仍然会在 FirstFilter 上得到回复。似乎它永远不会到达 ThirdFilter。
我的有什么问题吗web.xml
,我错过了一个dispatcher
吗?
提前致谢。