过滤器链调用如何工作? [英] How filter chain invocation works?

查看:111
本文介绍了过滤器链调用如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试了解过滤器链接.如

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()方法,该方法将requestresponse ,它是实现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 chaindoFilter()方法.这导致第二个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 chaindoFilter()方法.这导致目标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

这篇关于过滤器链调用如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆