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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 07:33:40  来源:igfitidea点击:

How filter chain invocation works?

javajakarta-eeservlet-filters

提问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.Filterinterface, which includes a doFilter()method that takes as input a requestand responsepair along with a filter chain, which is an instance of a class (provided by the servlet container) that implements the javax.servlet.FilterChaininterface. The filter chain reflects the order of the filters. The servlet container, based on the configuration order in the web.xmlfile, constructs the chain of filtersfor any servletor other resource that has filtersmapped 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和当作输入的方法responsepair 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例如,如果有两个,则该机制的关键步骤如下:

enter image description here

在此处输入图片说明

1.The target servletis requested. The containerdetects that there are two filtersand creates the filter chain.

1.servlet请求目标。在container检测到有两个filters和创建filter chain

2.The first filterin the chain is invoked by its doFilter()method.

2.filter链中的第一个由其doFilter()方法调用。

3.The first filtercompletes any preprocessing, then calls the doFilter()method of the filter chain. This results in the second filterbeing invoked by its doFilter()method.

3.第一filter任何预处理完成,然后调用doFilter()的方法filter chain。这导致第二个filter被其doFilter()方法调用。

4.The second filtercompletes any preprocessing, then calls the doFilter()method of the filter chain. This results in the target servletbeing invoked by its service()method.

4.第二filter完成任何预处理,然后调用doFilter()的方法filter chain。这导致目标servlet被其service()方法调用。

5.When the target servletis finished, the chain doFilter()call in the second filterreturns, and the second filtercan do any postprocessing.

5.当目标servlet完成后,doFilter()第二个中的链调用filter返回,第二个filter可以做任何后处理。

6.When the second filteris finished, the chain doFilter()call in the first filterreturns, and the first filtercan do any postprocessing.

6.当第二个filter完成时,第一个中的链doFilter()调用filter返回,第一个filter可以做任何后处理。

7.When the first filteris 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 中配置的顺序