java 过滤器链调用如何工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25196867/
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
How filter chain invocation works?
提问by Abhijeet Panwar
I am trying to understand filter chaining.As defined in this question
我正在尝试了解过滤器链接。正如在这个问题中所定义的
All filters are chained (in the order of their definition in web.xml). The chain.doFilter() is proceeding to the next element in the chain. The last element of the chain is the target resource/servlet.
所有过滤器都是链接的(按照它们在 web.xml 中的定义顺序)。chain.doFilter() 正在处理链中的下一个元素。链的最后一个元素是目标资源/servlet。
I am interested to know behind the scene in container that how container handles filter chaining.Can someone explain that how Filter chaining is handled inside container?
我很想知道在容器的幕后,容器如何处理过滤器链接。有人可以解释一下容器内部是如何处理过滤器链接的吗?
回答by SparkOn
Each filter implements the javax.servlet.Filter
interface, which includes a doFilter()
method that takes as input a request
and response
pair along with a filter chain
, which is an instance of a class (provided by the servlet container) that implements the javax.servlet.FilterChain
interface. The filter chain reflects the order of the filters. The servlet container
, based on the configuration order in the web.xml
file, constructs the chain of filters
for any servlet
or other resource that has filters
mapped to it. For each filter in the chain, the filter chain object passed to it represents the remaining filters to be called, in order, followed by the target servlet.
每个过滤器都实现了javax.servlet.Filter
接口,其中包括一个doFilter()
将 arequest
和当作输入的方法response
pair along with a filter chain
,它是实现该javax.servlet.FilterChain
接口的类(由 servlet 容器提供)的一个实例。过滤器链反映了过滤器的顺序。The servlet container
,根据web.xml
文件中的配置顺序,filters
为映射到它的任何servlet
或其他资源构造 的链filters
。对于链中的每个过滤器,传递给它的过滤器链对象表示要调用的其余过滤器,然后是目标 servlet。
If there are two filters
, for example, the key steps of this mechanism would be as follows:
filters
例如,如果有两个,则该机制的关键步骤如下:
1.The target servlet
is requested. The container
detects that there are two filters
and creates the filter chain
.
1.servlet
请求目标。在container
检测到有两个filters
和创建filter chain
。
2.The first filter
in the chain is invoked by its doFilter()
method.
2.filter
链中的第一个由其doFilter()
方法调用。
3.The first filter
completes any preprocessing, then calls the doFilter()
method of the filter chain
. This results in the second filter
being invoked by its doFilter()
method.
3.第一filter
任何预处理完成,然后调用doFilter()
的方法filter chain
。这导致第二个filter
被其doFilter()
方法调用。
4.The second filter
completes any preprocessing, then calls the doFilter()
method of the filter chain
. This results in the target servlet
being invoked by its service()
method.
4.第二filter
完成任何预处理,然后调用doFilter()
的方法filter chain
。这导致目标servlet
被其service()
方法调用。
5.When the target servlet
is finished, the chain doFilter()
call in the second filter
returns, and the second filter
can do any postprocessing.
5.当目标servlet
完成后,doFilter()
第二个中的链调用filter
返回,第二个filter
可以做任何后处理。
6.When the second filter
is finished, the chain doFilter()
call in the first filter
returns, and the first filter
can do any postprocessing.
6.当第二个filter
完成时,第一个中的链doFilter()
调用filter
返回,第一个filter
可以做任何后处理。
7.When the first filter
is finished, execution is complete.
7.当第一个filter
完成时,执行完成。
Filters can be interposed between servlets and the servlet container to wrap and preprocess requests or to wrap and postprocess responses. None of the filters are aware of their order. Ordering is handled entirely through the filter chain, according to the order in which filters are configured in web.xml
过滤器可以插入 servlet 和 servlet 容器之间,以包装和预处理请求或包装和后处理响应。没有一个过滤器知道它们的顺序。排序完全通过过滤器链处理,根据过滤器在 web.xml 中配置的顺序