Filter责任链的创建

org.apache.catalina.core.ApplicationFilterFactory#createFilterChain,  此方法是被org.apache.catalina.core.StandardWrapperValve#invoke调用的,  对每个request都会创建FilterChain

public static ApplicationFilterChain createFilterChain(ServletRequest request,Wrapper wrapper, Servlet servlet) {
//传入参数必须有一个servlet,这个servlet是在链的末尾被调用的. 这个servlet的查找过程是根据request的URI从web.xml配置中匹配出来的// If there is no servlet to execute, return nullif (servlet == null)return null;// Create and initialize a filter chain objectApplicationFilterChain filterChain = null;if (request instanceof Request) {//.....} else {// Request dispatcher in usefilterChain = new ApplicationFilterChain();}
// 责任链上绑定一个servletfilterChain.setServlet(servlet);filterChain.setServletSupportsAsync(wrapper.isAsyncSupported());// Acquire the filter mappings for this ContextStandardContext context = (StandardContext) wrapper.getParent();FilterMap filterMaps[] = context.findFilterMaps();// If there are no filter mappings, we are doneif ((filterMaps == null) || (filterMaps.length == 0))return (filterChain);// ...
//根据web.xml中filter的配置查找是否和当前request匹配// Add the relevant path-mapped filters to this filter chainfor (int i = 0; i < filterMaps.length; i++) {if (!matchDispatcher(filterMaps[i] ,dispatcher)) {continue;}if (!matchFiltersURL(filterMaps[i], requestPath))continue;ApplicationFilterConfig filterConfig = (ApplicationFilterConfig)context.findFilterConfig(filterMaps[i].getFilterName());if (filterConfig == null) {// FIXME - log configuration problemcontinue;}// 和request URI匹配,则filter会加入到责任链中filterChain.addFilter(filterConfig);}/.....// Return the completed filter chainreturn filterChain;}

filterChain创建完成后就会调用doFilter启用这个链,  此方法也是被org.apache.catalina.core.StandardWrapperValve#invoke调用的

org.apache.catalina.core.ApplicationFilterChain#doFilter

 1 private void internalDoFilter(ServletRequest request,
 2                                   ServletResponse response)
 3         throws IOException, ServletException {
 4
 5         // Call the next filter if there is one
 6         if (pos < n) {
 7             //调用下一个filter, 每调用一次 当前pos都会++
 8             ApplicationFilterConfig filterConfig = filters[pos++];
 9             try {
10                 Filter filter = filterConfig.getFilter();
11
12                 //...
13                     filter.doFilter(request, response, this);
14
15             } catch (IOException | ServletException | RuntimeException e) {
16                 throw e;
17             } catch (Throwable e) {
18                // ...
19             }
20             return;
21         }
22
23         // We fell off the end of the chain -- call the servlet instance
24         try {
25             if (request.isAsyncSupported() && !servletSupportsAsync) {
26                 request.setAttribute(Globals.ASYNC_SUPPORTED_ATTR,
27                         Boolean.FALSE);
28             }
29             // ...  在链的末尾,调用具体servlet的doService
30                 servlet.service(request, response);
31             //
32         } catch (IOException | ServletException | RuntimeException e) {
33             throw e;
34         } catch (Throwable e) {
35            //...
36         } finally {
37            //...
38         }
39     }

转载于:https://www.cnblogs.com/yszzu/p/9626167.html

Filter责任链模式相关推荐

  1. 责任链模式之Tomcat Filter应用

    责任链模式之Tomcat Filter应用 责任链模式是一种对象的行为型模式. 其定义为:很多对象由每一个对象对其下家的引用而连接起来形成一条链.请求在这个链上传递,直到链上的某一个对象处理此请求.发 ...

  2. Java 责任链模式

    顾名思义,责任链模式(Chain of Responsibility Pattern)为请求创建了一个接收者对象的链.这种模式给予请求的类型,对请求的发送者和接收者进行解耦.这种类型的设计模式属于行为 ...

  3. 一起学设计模式 - 责任链模式

    责任链模式(ChainOfResponsibilityPattern)属于 行为型模式的一种,将请求沿着一条链传递,直到该链上的某个对象处理它为止. 概述 定义如下:一个请求有多个对象来处理,这些对象 ...

  4. 折腾Java设计模式之责任链模式

    责任链模式 顾名思义,责任链模式(Chain of Responsibility Pattern)为请求创建了一个接收者对象的链.这种模式给予请求的类型,对请求的发送者和接收者进行解耦.这种类型的设计 ...

  5. 最近学习了责任链模式

    2019独角兽企业重金招聘Python工程师标准>>> 前言 来菜鸟这个大家庭10个月了,总得来说比较融入了环境,同时在忙碌的工作中也深感技术积累不够,在优秀的人身边工作必须更加花时 ...

  6. [转]《JAVA与模式》之责任链模式

    http://www.cnblogs.com/java-my-life/archive/2012/05/28/2516865.html 在阎宏博士的<JAVA与模式>一书中开头是这样描述责 ...

  7. 《JAVA与模式》之责任链模式

    2019独角兽企业重金招聘Python工程师标准>>> 详细请访问原博客:http://www.cnblogs.com/java-my-life/archive/2012/05/28 ...

  8. 行为型模式:责任链模式

    定义:责任链模式(Chain of Responsibility)使多个对象都有机会处理请求,从而避免请求的发送者和接受者之间的耦合关系.将这些对象连成一条链,并沿着这条链传递该请求,直到有对象能够处 ...

  9. 【设计模式】责任链模式

    顾名思义,责任链模式(Chain of Responsibility Pattern)为请求创建了一个接收者对象的链.这种模式给予请求的类型,对请求的发送者和接收者进行解耦.这种类型的设计模式属于行为 ...

最新文章

  1. python数据分析类库_python数据分析类库系列-Pandas入门之数据结构Series
  2. Scapy学习笔记一
  3. SecureCRT连接Linux终端中文乱码解决方法
  4. C++中delete和delete[]的区别
  5. Python的看门狗实现自动化实时对服务器、Windows或Linux文件夹的实时监控
  6. array_push_PHP array_push()函数与示例
  7. 机器学习没有捷径,根据机器学习算法地图学习是最有效的一种方式!
  8. PCI的imagework已由freeview软件代替
  9. php 随机码类,php随机类型验证码
  10. pytorch---之随机种子初始化
  11. ReentrantLock与Condition构造有界缓存队列与数据栈
  12. php 解析字符串编码方式 GB2312 UTF8
  13. Matlab安装过程
  14. helix server配置教程
  15. Xmind8 Pro 最新版 破解教程(序列号|破解文件)
  16. 最直白的编译原理-词法分析(清华-王书3版)
  17. 房地产公司网站建设需要多少钱
  18. 我的大学十年 -- 林锐
  19. [BZOJ1208]宠物收养所
  20. Android开发项目实战:实现折叠式布局,android组件化开发与sdk

热门文章

  1. PAT——1018. 锤子剪刀布
  2. javascript 自定义Map
  3. IO流入门-第十二章-ObjectInputStream_ObjectOutputStream
  4. 堆溢出的DWORD Shoot核心原理-口语化
  5. 获取当前目录绝对路径
  6. Java基于redis实现分布式锁(SpringBoot)
  7. Vim 行号的显示与隐藏
  8. 第一阶段:前端开发_Mysql——表与表之间的关系
  9. Linux邮件系统整合windows 2008 R2 AD域认证更新
  10. 2017视频监控行业应用趋势与市场发展分析