Java doFilter() 是在 Servlet 的工作完成之前还是之后执行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1323009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is doFilter() executed before or after the Servlet's work is done?
提问by Jeremy Powell
The javax.servlet.Filterobject can be used both for authentication (where the Filter needs to catch the request before any servlet work needs to be done) and for XSLT translation (where the servlet needs to be completely finished generating content). When does it actually get executed?
所述javax.servlet.Filter的对象可以用于认证使用二者(其中过滤器需要赶上请求需要做任何servlet工作需要之前)和XSLT转换(其中的servlet需要是完全完成生成的内容)。什么时候真正执行?
I know this is implementation dependent (on the web container), but this seems to be problem that needs to be solved by all of them.
我知道这是依赖于实现的(在 Web 容器上),但这似乎是需要所有人解决的问题。
Maybe there is a configuration option set somewhere for each Filter registration with the web container?
也许在某个地方为每个过滤器注册设置了一个配置选项?
Additional:
额外的:
Also, what governs the order of Filter execution? Why would FooFilter get executed before BarFilter?
另外,是什么控制过滤器执行的顺序?为什么 FooFilter 会在 BarFilter 之前执行?
采纳答案by Rich Kroll
The filter chain in essence wraps the servlet invocation. The chain will process all links until it hits the "bottom", then allow the servlet to run, and then return up the chain in reverse. For example, if you have a new "example filter", your doFilter() method may look like this:
过滤器链本质上包装了 servlet 调用。链将处理所有链接,直到它到达“底部”,然后允许 servlet 运行,然后反向返回链。例如,如果您有一个新的“示例过滤器”,您的 doFilter() 方法可能如下所示:
public void doFilter(ServletRequest request,
ServletResponse response, FilterChain chain)
throws IOException, ServletException {
// do pre-servlet work here
chain.doFilter(request, response);
// do post servlet work here
}
回答by Anil Kesarwani
According to the servlet2.3 specification filter is performed according to web.xml configuration of filter- mapping sequence Ref-http://www.programering.com/a/MTMyADOwATI.html
根据servlet2.3规范过滤器根据web.xml配置过滤器映射序列Ref- http://www.programering.com/a/MTMyADOwATI.html