Filter责任链模式
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 null
if (servlet == null)
return null; // Create and initialize a filter chain object
ApplicationFilterChain filterChain = null;
if (request instanceof Request) {
//.....
} else {
// Request dispatcher in use
filterChain = new ApplicationFilterChain();
}
// 责任链上绑定一个servlet
filterChain.setServlet(servlet);
filterChain.setServletSupportsAsync(wrapper.isAsyncSupported()); // Acquire the filter mappings for this Context
StandardContext context = (StandardContext) wrapper.getParent();
FilterMap filterMaps[] = context.findFilterMaps(); // If there are no filter mappings, we are done
if ((filterMaps == null) || (filterMaps.length == 0))
return (filterChain); // ...
//根据web.xml中filter的配置查找是否和当前request匹配
// Add the relevant path-mapped filters to this filter chain
for (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 problem
continue;
}
// 和request URI匹配,则filter会加入到责任链中
filterChain.addFilter(filterConfig);
} /.....// Return the completed filter chain
return filterChain;
}
filterChain创建完成后就会调用doFilter启用这个链, 此方法也是被org.apache.catalina.core.StandardWrapperValve#invoke调用的
org.apache.catalina.core.ApplicationFilterChain#doFilter
private void internalDoFilter(ServletRequest request,
ServletResponse response)
throws IOException, ServletException { // Call the next filter if there is one
if (pos < n) {
//调用下一个filter, 每调用一次 当前pos都会++
ApplicationFilterConfig filterConfig = filters[pos++];
try {
Filter filter = filterConfig.getFilter(); //...
filter.doFilter(request, response, this); } catch (IOException | ServletException | RuntimeException e) {
throw e;
} catch (Throwable e) {
// ...
}
return;
} // We fell off the end of the chain -- call the servlet instance
try {
if (request.isAsyncSupported() && !servletSupportsAsync) {
request.setAttribute(Globals.ASYNC_SUPPORTED_ATTR,
Boolean.FALSE);
}
// ... 在链的末尾,调用具体servlet的doService
servlet.service(request, response);
//
} catch (IOException | ServletException | RuntimeException e) {
throw e;
} catch (Throwable e) {
//...
} finally {
//...
}
}
Filter责任链模式的更多相关文章
- 由浅入深讲解责任链模式,理解Tomcat的Filter过滤器
本文将从简单的场景引入, 逐步优化, 最后给出具体的责任链设计模式实现. 场景引入 首先我们考虑这样一个场景: 论坛上用户要发帖子, 但是用户的想法是丰富多变的, 他们可能正常地发帖, 可能会在网页中 ...
- 设计模式学习笔记(十四)责任链模式实现以及在Filter中的应用
责任链模式(Chain Of Responsibility Design Pattern),也叫做职责链,是将请求的发送和接收解耦,让多个接收对象都有机会处理这个请求.当有请求发生时,可将请求沿着这条 ...
- 责任链模式/chain of responsibility/行为型模式
职责链模式 chain of responsibility 意图 使多个对象都有机会处理请求,从而避免请求的发送者和接受者之间的耦合关系.将这些对象连成一条链,并沿着这条链传递该请求,直到有一个对象处 ...
- [工作中的设计模式]责任链模式chain
一.模式解析 责任链模式是一种对象的行为模式.在责任链模式里,很多对象由每一个对象对其下家的引用而连接起来形成一条链.请求在这个链上传递,直到链上的某一个对象决定处理此请求.发出这个请求的客户端并不知 ...
- JAVA设计模式之责任链模式
在阎宏博士的<JAVA与模式>一书中开头是这样描述责任链(Chain of Responsibility)模式的: 责任链模式是一种对象的行为模式.在责任链模式里,很多对象由每一个对象对其 ...
- Java设计模式系列之责任链模式
责任链模式 责任链模式是一种对象的行为模式.在责任链模式里,很多对象由每一个对象对其下家的引用而连接起来形成一条链.请求在这个链上传递,直到链上的某一个对象决定处理此请求.发出这个请求的客户端并不知道 ...
- JavaScript责任链模式
介绍 责任链模式(Chain of responsibility)是使多个对象都有机会处理请求,从而避免请求的发送者和接受者之间的耦合关系.将对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理 ...
- 设计模式(一)Chain Of Responsibility责任链模式
设计模式篇章,源于网课的学习,以及个人的整理 在我们接收用户提交的字符时,常常会使用到过滤,在学习责任链模式前,我们是这样做的 1.定义一个类 public class MsgProcesser { ...
- 设计模式《JAVA与模式》之责任链模式
在阎宏博士的<JAVA与模式>一书中开头是这样描述责任链(Chain of Responsibility)模式的: 责任链模式是一种对象的行为模式.在责任链模式里,很多对象由每一个对象对其 ...
随机推荐
- pageBean实现分页
PageBean类 package com.xujingyang.domain ; import java.util.List ; /** * @author oldmonk * @date 2017 ...
- Angular07 利用angular打造管理系统页面
1 创建一个新的angular应用 ng new adminSystem 2 利用WebStorm打开adminSystem应用 3 借助AdminLTE这个开源项目来辅助开发 AdminLTE项目: ...
- sleep()和usleep()
函数名: sleep头文件: #include <windows.h> // 在VC中使用带上头文件 #include <unistd.h> // 在gcc编译 ...
- Gstreamer编程
一.简介 GStreamer是一个开源的多媒体框架库.利用它,可以构建一系列的媒体处理模块,包括从简单的ogg播放功能到复杂的音频(混音)和视频(非线性编辑)的处理.应用程序可以透明的利用解码和过滤技 ...
- cf123E Maze
传送门 分析 见ptx大爷的博客 代码 #include<iostream> #include<cstdio> #include<cstring> #include ...
- ARC061E Snuke's Subway Trip
传送门 题目大意 已知某城市的地铁网由一些地铁线路构成,每一条地铁线路由某一个公司运营,该城市规定:若乘坐同一公司的地铁,从开始到换乘只需要一块钱,换乘其他公司的价格也是一块钱,问从1号地铁站到n号地 ...
- 面试经常问的一个问题:final、finalize、finally
http://m.blog.csdn.net/u010980446/article/details/51493658
- SDUT 3374 数据结构实验之查找二:平衡二叉树
数据结构实验之查找二:平衡二叉树 Time Limit: 400MS Memory Limit: 65536KB Submit Statistic Problem Description 根据给定的输 ...
- leetcode Jump Game I II 待续 贪心看不懂啊!!!!
下面是这两个题的解法: 参考博客:http://blog.csdn.net/loverooney/article/details/38455475 自己写的第一题(TLE): #include< ...
- Java50道经典习题-程序45 被9整除
题目:判断一个素数能被几个9整除分析:素数只能被1和其本身整除,不能被9整除,所以返回false import java.util.Scanner; public class Prog45{ publ ...