filterchain
tomcat责任链设计模式 FilterChain原理解析
实现filterchain的dofilter方法里面存在这种机制,把自身接收到的请求request对象和response对象和自身对象即filterchain
作为下一个过滤器的dofilter的三个形参传递过去,这样才能使得过滤器传递下去,当然这个方法中还存在一些判断if等机制
用来判断现在的这个过滤器是不是最后一个,是的话就可以把请求和响应对象传递给浏览器请求的页面
2014年09月05日 22:54:48
阅读数:5719
转自:http://javapolo.iteye.com/blog/1287747
今天晚上花了些时间debug了下tomcat,注意观察了下tomcat内部过滤器的实现,其实tomcat内部过滤器采用了责任链的设计模式,
(其实struts2拦截器那一块采用了相似的设计模式),以下是个人对源码的解读,
APPlicationFilterChain详解
首先是对该类的定义的介绍
[java] view plain copy
- /**
- * Implementation of <code>javax.servlet.FilterChain</code> used to manage
- * the execution of a set of filters for a particular request. When the
- * set of defined filters has all been executed, the next call to
- * <code>doFilter()</code> will execute the servlet's <code>service()</code>
- * method itself.
- *
- * @author Craig R. McClanahan
- * @version $Id: ApplicationFilterChain.java 1078022 2011-03-04 15:52:01Z markt $
- */
- final class ApplicationFilterChain implements FilterChain, CometFilterChain
第一个疑问是该过滤器链里面的过滤器源于哪里?
答案是该类里面包含了一个Applicationfilterconfig对象,而该对象则是个filter容器
[java] view plain copy
- /**
- * Filters.
- */
- private ApplicationFilterConfig[] filters =
- new ApplicationFilterConfig[0];
以下是ApplicationFilterConfig类的声明
org.apache.catalina.core.ApplicationFilterConfig
Implementation of a javax.servlet.FilterConfig useful in managing the filter instances instantiated when a web application is first started.
当web容器启动是ApplicationFilterConfig自动实例化,它会从该web工程的web.xml文件中读取配置的filter信息,然后装进该容器
下个疑问是它如何执行该过滤器容器里面的filter呢?
答案是通过pos它来标识当前ApplicationFilterChain(当前过滤器链)执行到哪个过滤器
[java] view plain copy
- /**
- * The int which is used to maintain the current position
- * in the filter chain.
- */
- private int pos = 0;
通过n来记录当前过滤器链里面拥有的过滤器数目
[java] view plain copy
- /**
- * The int which gives the current number of filters in the chain.
- */
- private int n = 0;
通过addFilter方法向容器中添加一个filter(参数即为容器初始化生成的filterConfig对象)
[java] view plain copy
- /**
- * Add a filter to the set of filters that will be executed in this chain.
- *
- * @param filterConfig The FilterConfig for the servlet to be executed
- */
- void addFilter(ApplicationFilterConfig filterConfig)
ApplicationFilterChain采用责任链设计模式达到对不同过滤器的执行
首先ApplicationFilterChain 会调用它重写FilterChain的doFilter方法,然后doFilter里面会调用
internalDoFilter(request,response)方法;该方法使过滤器容器拿到每个过滤器,然后调用它们重写Filter接口里面的dofilter方法
以下是ApplicationFilterChain 里面重写FilterChain里面的doFilter方法的描述
[java] view plain copy
- /**
- * Invoke the next filter in this chain, passing the specified request
- * and response. If there are no more filters in this chain, invoke
- * the <code>service()</code> method of the servlet itself.
- *
- * @param request The servlet request we are processing
- * @param response The servlet response we are creating
- *
- * @exception IOException if an input/output ERROR occurs
- * @exception ServletException if a servlet exception occurs
- */
- @Override
- public void doFilter(ServletRequest request, ServletResponse response)
以下是internalDoFilter的部分代码
[java] view plain copy
- // Call the next filter if there is one
- if (pos < n) {
- //先拿到下个过滤器,将指针向下移动一位
- ApplicationFilterConfig filterConfig = filters[pos++];
- Filter filter = null;
- try {
- //获取当前指向的filter实例
- filter = filterConfig.getFilter();
- support.fireInstanceEvent(InstanceEvent.BEFORE_FILTER_EVENT,
- filter, request, response);
- if (request.isAsyncSupported() && "false".equalsignorecase(
- filterConfig.getFilterDef().getAsyncSupported())) {
- request.setattribute(Globals.ASYNC_SUPPORTED_ATTR,
- Boolean.FALSE);
- }
- if( Globals.IS_SECURITY_ENABLED ) {
- final ServletRequest req = request;
- final ServletResponse res = response;
- Principal principal =
- ((HttpServletRequest) req).getUserPrincipal();
- Object[] args = new Object[]{req, res, this};
- SecurityUtil.doAsPrivilege
- ("doFilter", filter, classType, args, principal);
- } else {
- //filter调用doFilter(request, response, this)方法
- //ApplicationFilterChain里面的filter都实现了filter //接口
- filter.doFilter(request, response, this);
- }
以下是Filter接口doFilter定义如下
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
过滤器链里面的filter在调用dofilter完成后,会继续调用chain.doFilter(request,response)方法,而这个chain其实就是applicationfilterchain,所以调用过程又回到了上面调用dofilter和调用internalDoFilter方法,这样执行直到里面的过滤器全部执行
当filte都调用完成后,它就会初始化相应的servlet,(例如jsp资源,默认它会开启一个 JspServlet对象)
[java] view plain copy
- // We fell off the end of the chain -- call the servlet instance
- try {
- if (ApplicationDispatcher.WRAP_SAME_OBJECT) {
- lastServicedRequest.set(request);
- lastServicedResponse.set(response);
- }
- support.fireInstanceEvent(InstanceEvent.BEFORE_SERVICE_EVENT,
- servlet, request, response);
举个例子
假如访问的是个jsp,首先开启一个 JspServlet对象,然后该JspServlet对象会调用它的service方法
以下是个JspServlet的定义以及service方法的描述
[java] view plain copy
- public class JspServlet extends HttpServlet implements PeriodicEventListener
- public void service (HttpServletRequest request,
- HttpServletResponse response)
- //jspFile may be configured as an init-param for this servlet instance //该jspUri代表访问jsp的相对路径
- String jspUri = jspFile;
拿到该路径后它就会去判断该jsp是否已经预编译过
boolean precompile = preCompile(request);
serviceJspFile(request, response, jspUri, null, precompile);
如果已经编译则直接调用该jsp对应的servlet的_jspService方法
否则先编译在调用
假如是个html页面的访问则直接调用DefaultServlet做相应的处理
简单的分析了下,作为一个学习笔记吧
相关阅读
#firewalld&netfilter permissive,遇到真正需要阻断时不会真正阻断,只会提醒 在centos7以前叫netfilter,在7中称之为netfilter 由
一、什么是CharacterEncodingFilter官方解释如下是spring内置过滤器的一种,用来指定请求或者响应的编码格式。在web开发中经常被从
缓存中无值(未宕机) 互斥锁 缓存永不过期 缓存宕机 白名单 布隆过滤器 代码实现 前面的文章介绍了缓存的分类和使用的场景
【 FPGA 】FIR 滤波器之Single-rate FIR滤波器的系数
首先要明确什么是单速率 FIR 滤波器? The basic FIR filter core is a single-rate (input sample rate = output sample rate) fi
文章目录Bloom FilterScrapy-Redis的存储Bloom Filter算法散列算法多个散列函数insert方法exists方法测试实例setbit和getbit的用