简体   繁体   English

Servlet 过滤器无法正常运行

[英]Servlet Filter not functioning properly

After some research and trial error, I've found a method that would allow me to add servlet filters programmatically while preserving their order as desired without using web.xml and annotations.经过一些研究和试错后,我找到了一种方法,该方法允许我以编程方式添加 servlet 过滤器,同时在不使用 web.xml 和注释的情况下根据需要保留它们的顺序。 Based upon the method found, the filters aren't functioning as I've understand it represented by the picture in this thread .根据找到的方法,过滤器无法正常工作,正如我所理解的,此线程中图片表示。 From my logs, steps 5, 6, & 7 of filter-chain invocation never executed.从我的日志来看,过滤器链调用的第 5、6 和 7 步从未执行过。

On successful web app context startup:在成功的 Web 应用上下文启动时:

GodFilter.init
Filter1.init
Filter2.init
...
FilterFinal.init

When browsing the web app:浏览网络应用程序时:

GodFilter.doFilter
Filter1.doFilter
GodFilterChain.doFilter
Filter2.doFilter
GodFilterChain.doFilter
...
FilterFinal.dofilter
MyServlet.service(ServletRequest request, ServletResponse response)
MyServlet.service(HttpServletRequest request, HttpServletResponse response)
MyServlet.getLastModified
MyServlet.doGet

At the very minimum, I'm expecting to see GodFilter.doFilter (per steps 5, 6, & 7 of filter-chain invocation) after the MyServlet.doGet but it's not showing up.至少,我希望在GodFilter.doFilter看到GodFilter.doFilter (按照过滤器链调用的第MyServlet.doGet和 7 步),但它没有出现。 Am I missing something or misunderstood how servlet filter-chain works?我是否遗漏了什么或误解了 servlet 过滤器链的工作原理? Is the GodFilter class incorrect? GodFilter类不正确吗?

TIA!蒂亚!

[Edit 1] [编辑 1]

I forgot to mention that the GodFilter is loaded via a class implementing ServletContainerInitializer :我忘了提到GodFilter是通过实现ServletContainerInitializer的类加载的:

FilterRegistration fr = context.addFilter(GodFilter.class.getSimpleName(), GodFilter.class);
fr.addMappingForUrlPatterns(EnumSet.of( //
        DispatcherType.ASYNC, //
        DispatcherType.ERROR, //
        DispatcherType.FORWARD, //
        DispatcherType.INCLUDE, //
        DispatcherType.REQUEST), false, "/*");
fr.setInitParameter("param1", "value1");

I've tried isMatchedAfter to true also but no changes.我也试过isMatchedAfter为 true 但没有变化。

After reviewing some more of my code for the Filters, I found I left out the crucial part of post chain.doFilter(request, response) processing in the Filter.doFilter :回顾一些我的代码的过滤器后,我发现我离开了岗位的重要组成部分chain.doFilter(request, response)在处理Filter.doFilter

Filter1.doFilter(ServletRequest request, ServletResponse response, FilterChain chain) {
    // pre chain processing 

    // if filter is last in filter chain, then mapped servlet will process hereafter
    chain.doFilter(request, response);

    // post chain processing
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM