filter过滤器,filterchain的dofilter方法

tomcat责任链设计模式 FilterChain原理解析

实现filterchain的dofilter方法里面存在这种机制,把自身接收到的请求request对象和response对象和自身对象即filterchain

作为下一个过滤器的dofilter的三个形参传递过去,这样才能使得过滤器传递下去,当然这个方法中还存在一些判断if等机制

用来判断现在的这个过滤器是不是最后一个,是的话就可以把请求和响应对象传递给浏览器请求的页面

转自:http://javapolo.iteye.com/blog/1287747

今天晚上花了些时间debug了下tomcat,注意观察了下tomcat内部过滤器的实现,其实tomcat内部过滤器采用了责任链的设计模式,

(其实struts2拦截器那一块采用了相似的设计模式),以下是个人对源码的解读,

ApplicationFilterChain详解

首先是对该类的定义的介绍

[java] view plain copy
  1. /** 
  2.  * Implementation of <code>javax.servlet.FilterChain</code> used to manage 
  3.  * the execution of a set of filters for a particular request.  When the 
  4.  * set of defined filters has all been executed, the next call to 
  5.  * <code>doFilter()</code> will execute the servlet's <code>service()</code> 
  6.  * method itself. 
  7.  * 
  8.  * @author Craig R. McClanahan 
  9.  * @version $Id: ApplicationFilterChain.java 1078022 2011-03-04 15:52:01Z markt $ 
  10.  */  
  11.   
  12. final class ApplicationFilterChain implements FilterChain, CometFilterChain  

第一个疑问是该过滤器链里面的过滤器源于哪里?

答案是该类里面包含了一个ApplicationFilterConfig对象,而该对象则是个filter容器

[java] view plain copy
  1. /** 
  2.  * Filters. 
  3.  */  
  4. private ApplicationFilterConfig[] filters =  
  5.     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
  1. /** 
  2.  * The int which is used to maintain the current position 
  3.  * in the filter chain. 
  4.  */  
  5. private int pos = 0;  


通过n来记录当前过滤器链里面拥有的过滤器数目

[java] view plain copy
  1. /** 
  2.   * The int which gives the current number of filters in the chain. 
  3.   */  
  4.  private int n = 0;  



通过addFilter方法向容器中添加一个filter(参数即为容器初始化生成的filterConfig对象)

[java] view plain copy
  1. /** 
  2.   * Add a filter to the set of filters that will be executed in this chain. 
  3.   * 
  4.   * @param filterConfig The FilterConfig for the servlet to be executed 
  5.   */  
  6.  void addFilter(ApplicationFilterConfig filterConfig)  


ApplicationFilterChain采用责任链设计模式达到对不同过滤器的执行
首先ApplicationFilterChain 会调用它重写FilterChain的doFilter方法,然后doFilter里面会调用
 internalDoFilter(request,response)方法;该方法使过滤器容器拿到每个过滤器,然后调用它们重写Filter接口里面的dofilter方法

以下是ApplicationFilterChain 里面重写FilterChain里面的doFilter方法的描述

[java] view plain copy
  1. /** 
  2.  * Invoke the next filter in this chain, passing the specified request 
  3.  * and response.  If there are no more filters in this chain, invoke 
  4.  * the <code>service()</code> method of the servlet itself. 
  5.  * 
  6.  * @param request The servlet request we are processing 
  7.  * @param response The servlet response we are creating 
  8.  * 
  9.  * @exception IOException if an input/output error occurs 
  10.  * @exception ServletException if a servlet exception occurs 
  11.  */  
  12. @Override  
  13. public void doFilter(ServletRequest request, ServletResponse response)  


以下是internalDoFilter的部分代码

[java] view plain copy
  1. // Call the next filter if there is one  
  2.         if (pos < n) {  
  3.             //先拿到下个过滤器,将指针向下移动一位  
  4.             ApplicationFilterConfig filterConfig = filters[pos++];  
  5.             Filter filter = null;  
  6.             try {  
  7.                 //获取当前指向的filter实例  
  8.                 filter = filterConfig.getFilter();  
  9.                 support.fireInstanceEvent(InstanceEvent.BEFORE_FILTER_EVENT,  
  10.                                           filter, request, response);  
  11.                  
  12.                 if (request.isAsyncSupported() && "false".equalsIgnoreCase(  
  13.                         filterConfig.getFilterDef().getAsyncSupported())) {  
  14.                     request.setAttribute(Globals.ASYNC_SUPPORTED_ATTR,  
  15.                             Boolean.FALSE);  
  16.                 }  
  17.                 if( Globals.IS_SECURITY_ENABLED ) {  
  18.                     final ServletRequest req = request;  
  19.                     final ServletResponse res = response;  
  20.                     Principal principal =  
  21.                         ((HttpServletRequest) req).getUserPrincipal();  
  22.   
  23.                     Object[] args = new Object[]{req, res, this};  
  24.                     SecurityUtil.doAsPrivilege  
  25.                         ("doFilter", filter, classType, args, principal);  
  26.                      
  27.                 } else {   
  28.                     //filter调用doFilter(request, response, this)方法  
  29.                     //ApplicationFilterChain里面的filter都实现了filter                   //接口  
  30.                     filter.doFilter(request, response, this);  
  31.                 }  


以下是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
  1. // We fell off the end of the chain -- call the servlet instance  
  2.    try {  
  3.        if (ApplicationDispatcher.WRAP_SAME_OBJECT) {  
  4.            lastServicedRequest.set(request);  
  5.            lastServicedResponse.set(response);  
  6.        }  
  7.   
  8.        support.fireInstanceEvent(InstanceEvent.BEFORE_SERVICE_EVENT,  
  9.                                  servlet, request, response);  


举个例子
假如访问的是个jsp,首先开启一个 JspServlet对象,然后该JspServlet对象会调用它的service方法

以下是个JspServlet的定义以及service方法的描述

[java] view plain copy
  1. public class JspServlet extends HttpServlet implements PeriodicEventListener  
  2.   
  3.  public void service (HttpServletRequest request,  
  4.                              HttpServletResponse response)  
  5.   
  6.  //jspFile may be configured as an init-param for this servlet instance     //该jspUri代表访问jsp的相对路径   
  7.         String jspUri = jspFile;  


拿到该路径后它就会去判断该jsp是否已经预编译过
            boolean precompile = preCompile(request);

           serviceJspFile(request, response, jspUri, null, precompile);
如果已经编译则直接调用该jsp对应的servlet的_jspService方法
否则先编译在调用

假如是个html页面的访问则直接调用DefaultServlet做相应的处理

简单的分析了下,作为一个学习笔记吧


  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值