过滤器链调用如何工作?
I am trying to understand filter chaining.As defined in this question
所有过滤器都已链接(按照其在web.xml中的定义顺序). chain.doFilter()正在前进到链中的下一个元素. 链的最后一个元素是目标资源/servlet.
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.
我很想知道容器背后的情况,即容器如何处理过滤器链.有人可以解释一下如何在容器内部处理过滤器链吗?
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?
每个过滤器都实现javax.servlet.Filter
接口,该接口包括一个doFilter()
方法,该方法将request
和response
javax.servlet.FilterChain
接口的类的实例(由servlet容器提供).过滤器链反映了过滤器的顺序. The servlet container
根据web.xml
文件中的配置顺序,为任何servlet
或映射了filters
的其他资源构造filters
链.对于链中的每个过滤器,传递给它的过滤器链对象依次表示要调用的其余过滤器,然后是目标servlet.
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.
例如,如果有两个filters
,则该机制的关键步骤如下:
If there are two filters
, for example, the key steps of this mechanism would be as follows:
1.请求目标servlet
. container
检测到有两个filters
并创建filter chain
.
1.The target servlet
is requested. The container
detects that there are two filters
and creates the filter chain
.
2.链中的第一个filter
由其doFilter()
方法调用.
2.The first filter
in the chain is invoked by its doFilter()
method.
3.第一个filter
完成任何预处理,然后调用filter chain
的doFilter()
方法.这导致第二个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.
4.第二个filter
完成任何预处理,然后调用filter chain
的doFilter()
方法.这导致目标servlet
被其service()
方法调用.
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.
5.当目标servlet
完成时,第二个filter
中的链doFilter()
调用返回,第二个filter
可以进行任何后处理.
5.When the target servlet
is finished, the chain doFilter()
call in the second filter
returns, and the second filter
can do any postprocessing.
6.当第二个filter
完成时,第一个filter
中的链doFilter()
调用返回,并且第一个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.
7.第一个filter
完成后,执行完成.
7.When the first filter
is finished, execution is complete.
可以在Servlet和Servlet容器之间插入过滤器,以包装和预处理请求,或者包装和后处理响应.没有一个过滤器知道其顺序.根据在web.xml中配置过滤器的顺序,整个处理过程完全通过过滤器链进行.
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