欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

How Tomcat Works(十四)補充

系統 2086 0

How Tomcat Works(十四) 中,本人并沒有對javax.servlet.Filter及javax.servlet.FilterChain做詳細的描述,本文在這里做一下補充

FilterChain接口只有一個方法,方法聲明如下:

public void doFilter ( ServletRequest request, ServletResponse response ) throws IOException, ServletException;

在tomcat中,org.apache.catalina.core.ApplicationFilterChain類實現了該接口

ApplicationFilterChain類采用一個私有成員變量private ArrayList filters = new ArrayList()保存ApplicationFilterConfig對象的集合,ApplicationFilterConfig類實現了javax.servlet.FilterConfig接口,封裝了對Filter實例的管理

下面是向ArrayList filters集合添加ApplicationFilterConfig實例

      
        void
      
      
         addFilter(ApplicationFilterConfig filterConfig) {

        
      
      
        this
      
      
        .filters.add(filterConfig);

    }
      
    

setServlet()方法用于向ApplicationFilterChain對象設置當前的servlet實例

      
        void
      
      
         setServlet(Servlet servlet) {

        
      
      
        this
      
      .servlet =
      
         servlet;

    }
      
    

在ApplicationFilterChain類的實現FilterChain接口方法doFilter()中,調用了其私有方法internalDoFilter(),下面是該方法的具體實現

      
        private
      
      
        void
      
      
         internalDoFilter(ServletRequest request, ServletResponse response)
        
      
      
        throws
      
      
         IOException, ServletException {

        
      
      
        //
      
      
         Construct an iterator the first time this method is called
      
      
        if
      
       (
      
        this
      
      .iterator == 
      
        null
      
      
        )
            
      
      
        this
      
      .iterator =
      
         filters.iterator();

        
      
      
        //
      
      
         Call the next filter if there is one
      
      
        if
      
       (
      
        this
      
      
        .iterator.hasNext()) {
            ApplicationFilterConfig filterConfig 
      
      =
      
        
              (ApplicationFilterConfig) iterator.next();
            Filter filter 
      
      = 
      
        null
      
      
        ;
            
      
      
        try
      
      
         {
                filter 
      
      =
      
         filterConfig.getFilter();
                support.fireInstanceEvent(InstanceEvent.BEFORE_FILTER_EVENT,
                                          filter, request, response);
                filter.doFilter(request, response, 
      
      
        this
      
      
        );
                support.fireInstanceEvent(InstanceEvent.AFTER_FILTER_EVENT,
                                          filter, request, response);
            } 
      
      
        catch
      
      
         (IOException e) {
                
      
      
        if
      
       (filter != 
      
        null
      
      
        )
                    support.fireInstanceEvent(InstanceEvent.AFTER_FILTER_EVENT,
                                              filter, request, response, e);
                
      
      
        throw
      
      
         e;
            } 
      
      
        catch
      
      
         (ServletException e) {
                
      
      
        if
      
       (filter != 
      
        null
      
      
        )
                    support.fireInstanceEvent(InstanceEvent.AFTER_FILTER_EVENT,
                                              filter, request, response, e);
                
      
      
        throw
      
      
         e;
            } 
      
      
        catch
      
      
         (RuntimeException e) {
                
      
      
        if
      
       (filter != 
      
        null
      
      
        )
                    support.fireInstanceEvent(InstanceEvent.AFTER_FILTER_EVENT,
                                              filter, request, response, e);
                
      
      
        throw
      
      
         e;
            } 
      
      
        catch
      
      
         (Throwable e) {
                
      
      
        if
      
       (filter != 
      
        null
      
      
        )
                    support.fireInstanceEvent(InstanceEvent.AFTER_FILTER_EVENT,
                                              filter, request, response, e);
                
      
      
        throw
      
      
        new
      
      
         ServletException
                  (sm.getString(
      
      "filterChain.filter"
      
        ), e);
            }
            
      
      
        return
      
      
        ;
        }

        
      
      
        //
      
      
         We fell off the end of the chain -- call the servlet instance
      
      
        try
      
      
         {
            support.fireInstanceEvent(InstanceEvent.BEFORE_SERVICE_EVENT,
                                      servlet, request, response);
            
      
      
        if
      
       ((request 
      
        instanceof
      
       HttpServletRequest) &&
      
        
                (response 
      
      
        instanceof
      
      
         HttpServletResponse)) {
                servlet.service((HttpServletRequest) request,
                                (HttpServletResponse) response);
            } 
      
      
        else
      
      
         {
                servlet.service(request, response);
            }
            support.fireInstanceEvent(InstanceEvent.AFTER_SERVICE_EVENT,
                                      servlet, request, response);
        } 
      
      
        catch
      
      
         (IOException e) {
            support.fireInstanceEvent(InstanceEvent.AFTER_SERVICE_EVENT,
                                      servlet, request, response, e);
            
      
      
        throw
      
      
         e;
        } 
      
      
        catch
      
      
         (ServletException e) {
            support.fireInstanceEvent(InstanceEvent.AFTER_SERVICE_EVENT,
                                      servlet, request, response, e);
            
      
      
        throw
      
      
         e;
        } 
      
      
        catch
      
      
         (RuntimeException e) {
            support.fireInstanceEvent(InstanceEvent.AFTER_SERVICE_EVENT,
                                      servlet, request, response, e);
            
      
      
        throw
      
      
         e;
        } 
      
      
        catch
      
      
         (Throwable e) {
            support.fireInstanceEvent(InstanceEvent.AFTER_SERVICE_EVENT,
                                      servlet, request, response, e);
            
      
      
        throw
      
      
        new
      
      
         ServletException
              (sm.getString(
      
      "filterChain.servlet"
      
        ), e);
        }

    }
      
    

在調用了filter.doFilter(request, response, this)方法之后,繼續執行servlet的service()方法

這里的this是ApplicationFilterChain對象自身,在我們編寫的Filter實現類里面同在在執行完我們實現的過濾方法之后會繼續調用FilterChain對象的void doFilter(ServletRequest request, ServletResponse response)方法,我們自定義的過濾器通常會這樣實現:

      
        class
      
       SampleFilter 
      
        implements
      
      
         Filter {   
      ........   
      
      
      
        public
      
      
        void
      
      
         doFilter(ServletRequest request, ServletResponse response,FilterChain chain)   
        
      
      
        throws
      
      
         IOException, ServletException {   
            
         
      
      
        //
      
      
        do something    
      
      
                 .....   
         
      
      
        //
      
      
        request, response傳遞給下一個過濾器進行過濾   
      
      
                 chain.doFilter(request, response);   
    }   
         
}  
      
    

ApplicationFilterConfig類實現了javax.servlet.FilterConfig接口,代表一個Filter容器,FilterConfig接口定義如下:

      
        public
      
      
        interface
      
      
         FilterConfig {    
      
      
        public
      
      
         String getFilterName(); 
      
      
        public
      
      
         ServletContext getServletContext();    
      
      
        public
      
      
         String getInitParameter(String name);   
      
      
        public
      
      
         Enumeration getInitParameterNames();
}
      
    

可以通過傳入org.apache.catalina.Context對象和FilterDef對象(FilterDef對象用于對過濾器類的定義,包括過濾器類名、相關參數等)傳給其構造函數構造一個ApplicationFilterConfig對象:

      
        public
      
      
         ApplicationFilterConfig(Context context, FilterDef filterDef)
        
      
      
        throws
      
      
         ClassCastException, ClassNotFoundException,
               IllegalAccessException, InstantiationException,
               ServletException {

        
      
      
        super
      
      
        ();
        
      
      
        this
      
      .context =
      
         context;
        setFilterDef(filterDef);

    }
      
    

ApplicationFilterConfig類的getFilter()方法返回一個javax.servlet.Filter對象,該方法負責載入并實例化一個過濾器類

      Filter getFilter() 
      
        throws
      
      
         ClassCastException, ClassNotFoundException,
        IllegalAccessException, InstantiationException, ServletException {

        
      
      
        //
      
      
         Return the existing filter instance, if any
      
      
        if
      
       (
      
        this
      
      .filter != 
      
        null
      
      
        )
            
      
      
        return
      
       (
      
        this
      
      
        .filter);

        
      
      
        //
      
      
         Identify the class loader we will be using
      
      
        String filterClass =
      
         filterDef.getFilterClass();
        ClassLoader classLoader 
      
      = 
      
        null
      
      
        ;
        
      
      
        if
      
       (filterClass.startsWith("org.apache.catalina."
      
        ))
            classLoader 
      
      = 
      
        this
      
      
        .getClass().getClassLoader();
        
      
      
        else
      
      
        
            classLoader 
      
      =
      
         context.getLoader().getClassLoader();

        ClassLoader oldCtxClassLoader 
      
      =
      
        
            Thread.currentThread().getContextClassLoader();

        
      
      
        //
      
      
         Instantiate a new instance of this filter and return it
      
      
        Class clazz =
      
         classLoader.loadClass(filterClass);
        
      
      
        this
      
      .filter =
      
         (Filter) clazz.newInstance();
        filter.init(
      
      
        this
      
      
        );
        
      
      
        return
      
       (
      
        this
      
      
        .filter);

    }
      
    

現在,也許我們更容易理解StandardWrapperValve類中創建過濾器鏈createFilterChain()方法了

      
        private
      
      
         ApplicationFilterChain createFilterChain(Request request,
                                                     Servlet servlet) {

        
      
      
        //
      
      
         If there is no servlet to execute, return null
      
      
        if
      
       (servlet == 
      
        null
      
      
        )
            
      
      
        return
      
       (
      
        null
      
      
        );

        
      
      
        //
      
      
         Create and initialize a filter chain object
      
      
        ApplicationFilterChain filterChain =
          
      
        new
      
      
         ApplicationFilterChain();
        filterChain.setServlet(servlet);
        StandardWrapper wrapper 
      
      =
      
         (StandardWrapper) getContainer();
        filterChain.setSupport(wrapper.getInstanceSupport());

        
      
      
        //
      
      
         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);

      
      
        //
      
      
                if (debug >= 1)

      
      
        //
      
      
                    log("createFilterChain:  Processing " + filterMaps.length +

      
      
        //
      
      
                        " filter map entries");

        
      
      
        //
      
      
         Acquire the information we will need to match filter mappings
      
      
        String requestPath = 
      
        null
      
      
        ;
        
      
      
        if
      
       (request 
      
        instanceof
      
      
         HttpRequest) {
            HttpServletRequest hreq 
      
      =
      
        
                (HttpServletRequest) request.getRequest();
            String contextPath 
      
      =
      
         hreq.getContextPath();
            
      
      
        if
      
       (contextPath == 
      
        null
      
      
        )
                contextPath 
      
      = ""
      
        ;
            String requestURI 
      
      =
      
         ((HttpRequest) request).getDecodedRequestURI();
            
      
      
        if
      
       (requestURI.length() >=
      
         contextPath.length())
                requestPath 
      
      =
      
         requestURI.substring(contextPath.length());
        }
        String servletName 
      
      =
      
         wrapper.getName();

      
      
        //
      
      
                if (debug >= 1) {

      
      
        //
      
      
                    log(" requestPath=" + requestPath);

      
      
        //
      
      
                    log(" servletName=" + servletName);

      
      
        //
      
      
                }
      
      
        int
      
       n = 0
      
        ;

        
      
      
        //
      
      
         Add the relevant path-mapped filters to this filter chain
      
      
        for
      
       (
      
        int
      
       i = 0; i < filterMaps.length; i++
      
        ) {

      
      
        //
      
      
                    if (debug >= 2)

      
      
        //
      
      
                        log(" Checking path-mapped filter '" +

      
      
        //
      
      
                            filterMaps[i] + "'");
      
      
        if
      
       (!
      
        matchFiltersURL(filterMaps[i], requestPath))
                
      
      
        continue
      
      
        ;
            ApplicationFilterConfig filterConfig 
      
      =
      
         (ApplicationFilterConfig)
                context.findFilterConfig(filterMaps[i].getFilterName());
            
      
      
        if
      
       (filterConfig == 
      
        null
      
      
        ) {

      
      
        //
      
      
                        if (debug >= 2)

      
      
        //
      
      
                            log(" Missing path-mapped filter '" +

      
      
        //
      
      
                                filterMaps[i] + "'");
      
      
                ;       
      
        //
      
      
         FIXME - log configuration problem
      
      
        continue
      
      
        ;
            }

      
      
        //
      
      
                    if (debug >= 2)

      
      
        //
      
      
                        log(" Adding path-mapped filter '" +

      
      
        //
      
      
                            filterConfig.getFilterName() + "'");
      
      
                    filterChain.addFilter(filterConfig);
            n
      
      ++
      
        ;
        }

        
      
      
        //
      
      
         Add filters that match on servlet name second
      
      
        for
      
       (
      
        int
      
       i = 0; i < filterMaps.length; i++
      
        ) {

      
      
        //
      
      
                    if (debug >= 2)

      
      
        //
      
      
                        log(" Checking servlet-mapped filter '" +

      
      
        //
      
      
                            filterMaps[i] + "'");
      
      
        if
      
       (!
      
        matchFiltersServlet(filterMaps[i], servletName))
                
      
      
        continue
      
      
        ;
            ApplicationFilterConfig filterConfig 
      
      =
      
         (ApplicationFilterConfig)
                context.findFilterConfig(filterMaps[i].getFilterName());
            
      
      
        if
      
       (filterConfig == 
      
        null
      
      
        ) {

      
      
        //
      
      
                        if (debug >= 2)

      
      
        //
      
      
                            log(" Missing servlet-mapped filter '" +

      
      
        //
      
      
                                filterMaps[i] + "'");
      
      
                ;       
      
        //
      
      
         FIXME - log configuration problem
      
      
        continue
      
      
        ;
            }

      
      
        //
      
      
                    if (debug >= 2)

      
      
        //
      
      
                        log(" Adding servlet-mapped filter '" +

      
      
        //
      
      
                             filterMaps[i] + "'");
      
      
                    filterChain.addFilter(filterConfig);
            n
      
      ++
      
        ;
        }

        
      
      
        //
      
      
         Return the completed filter chain

      
      
        //
      
      
                if (debug >= 2)

      
      
        //
      
      
                    log(" Returning chain with " + n + " filters");
      
      
        return
      
      
         (filterChain);

    }
      
    

---------------------------------------------------------------------------?

本系列How Tomcat Works系本人原創?

轉載請注明出處 博客園 刺猬的溫馴?

本人郵箱: ? chenying998179 # 163.com ( #改為@

本文鏈接 http://www.cnblogs.com/chenying99/p/3250342.html

How Tomcat Works(十四)補充


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。?!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 在线欧美日韩 | 五月天色婷婷综合 | 午夜电影网址 | 欧美精品国产第一区二区 | 亚洲精品无| 亚洲成人精品 | 日韩经典视频 | 天天操天天插天天干 | 国产高清一区二区 | 国内自拍偷拍网 | 男人天堂综合 | 天天插天天射天天操 | 中文字幕在线观看视频一区 | 成人国产在线 | 精品国产福利在线 | 亚洲国产精品一区二区第一页 | 久久久精品免费热线观看 | 亚洲国产精品久久久久秋霞蜜臀 | 欧美日韩三级在线观看 | 黑人性xxxⅹxxbbbbb | 国产亚洲欧美日本一二三本道 | 久久成人国产 | 精品久| 亚洲日本视频 | 91在线视频免费观看 | 日韩精品免费观看 | 泰国一级毛片aaa下面毛多 | 公么吃奶满足了我苏媚 | 综合色久 | 大逼逼影院 | 欧美综合久久 | 欧美成人精品激情在线观看 | 久久夜夜操 | 一个人看aaaa免费中文 | 看免费5xxaaa毛片 | 香蕉视频免费 | 一级视频在线 | 午夜激情小视频 | 欧美日韩欧美日韩 | 亚洲国产精品久久久 | 国产精品成人在线播放 |