`
javapolo
  • 浏览: 131908 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

tomcat 责任链设计模式 底层源码剖析

 
阅读更多

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

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

ApplicationFilterChain详解

首先是对该类的定义的介绍
/**
 * 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容器

    /**
     * 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(当前过滤器链)执行到哪个过滤器
    /**
     * The int which is used to maintain the current position
     * in the filter chain.
     */
    private int pos = 0;

通过n来记录当前过滤器链里面拥有的过滤器数目
   /**
     * The int which gives the current number of filters in the chain.
     */
    private int n = 0;


通过addFilter方法向容器中添加一个filter(参数即为容器初始化生成的filterConfig对象)
   /**
     * 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方法的描述
    /**
     * 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的部分代码
// 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对象)

        // 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方法的描述
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做相应的处理

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

 

 

 

分享到:
评论

相关推荐

    Tomcat深入剖析pdf+源码(Tomcat运行原理)

    总之,《Tomcat深入剖析》结合源码分析,是一本全面且深入的Tomcat学习资源,无论是初学者还是经验丰富的开发者,都能从中受益匪浅。通过学习,读者不仅能够掌握Tomcat的基本操作,还能深入了解其内部机制,为构建...

    Tomcat8.0底层源码

    《深入剖析Tomcat 8.0底层源码》 Tomcat作为一款广泛应用的开源Java Servlet容器,其8.0版本的源码对于开发者来说是一份宝贵的教育资源。通过深入学习Tomcat 8.0的源码,我们可以理解Web服务器的工作原理,提升对...

    Tomcat设计模式分析

    ### Tomcat设计模式分析 #### 一、门面设计模式 **1.1 门面设计模式原理** 门面设计模式是一种结构型设计模式,它为一个复杂的子系统提供了一个简单的接口,使得客户端可以通过该接口访问子系统中的一组接口。...

    「Tomcat源码剖析」.pdf

    Tomcat源码剖析 : 整体架构 层层分析 源码解析 架构分析 (Http服务器功能:Socket通信(TCP/IP)、解析Http报文 Servlet容器功能:有很多Servlet(自带系统级Servlet+自定义Servlet),Servlet处理具体的业务逻辑...

    Tomcat 系统架构与设计模式

    Tomcat 系统架构与设计模式,第 1 部分 工作原理

    tomcat源码+文档pdf+源码解析

    源码解析部分则是对Tomcat源码的深度剖析,涵盖了关键类和方法的作用、设计模式的运用以及性能优化技巧。这有助于开发者理解Tomcat内部的工作流程,例如,如何处理HTTP请求的生命周期,以及线程池是如何调度和管理的...

    Tomcat系统架构与设计模式[整理].pdf

    Tomcat 系统架构与设计模式 Tomcat 系统架构是 Apache 软件基金会的一款开源的 Java Web 服务器,它的架构设计非常复杂,具有很强的模块化特点。本文将从 Tomcat 的工作原理、设计模式两个方面来分析 Tomcat 的...

    Tomcat 系统架构与设计模式,第 1 部分: 工作原理1

    Tomcat 系统架构与设计模式,第 1 部分:工作原理 本文将从 Tomcat 系统架构与设计模式的角度,探讨其工作原理的第 1 部分,我们将从以下几个方面进行讨论: 1. Tomcat 系统架构概述 Tomcat 是一个基于 Java 的...

    深入剖析Tomcat书本源码

    《深入剖析Tomcat》这本书是Java开发者们探索Web服务器内部工作原理的重要参考资料,它详细解析了Tomcat的源代码,帮助我们理解这个流行的开源Servlet容器的运作机制。Tomcat是Apache软件基金会的一个项目,它是Java...

    tomcat5 源码学习,深度剖析tomcat一书的指定tomcat版本

    tomcat5 源码学习,深度剖析tomcat一书的指定tomcat版本,随着tomcat版本的升级,内容发生 了变化,但为了读懂书籍,还是得使用老版本得源码,欢迎大家下载,官网下载会出现下载不了的情况,我主页有tomcat4-tomcat9...

    tomcat 源码分析系列文档

    4. "Tomcat源码分析(4)容器处理链接之责任链模式.doc":分析了Tomcat如何利用责任链模式来处理请求,使得请求可以被多个处理器(如过滤器)有序处理。 5. "tomcat加载类的顺序.doc":详细说明了Tomcat加载类的具体...

    tomcat源码

    《深入理解Tomcat:工作原理与源码剖析》 Tomcat作为一款开源的Java Servlet容器,是Apache软件基金会Jakarta项目的重要组成部分,广泛应用于各种Java Web应用的部署。本篇文章将深入探讨Tomcat的工作原理,并结合...

    Tomcat 系统架构与设计模式,第 2 部分: 设计模式分析1

    Tomcat 系统架构与设计模式,第 2 部分:设计模式分析 1 本文将对 Tomcat 系统架构与设计模式进行深入分析,着重介绍设计模式在 Tomcat 中的应用。 标题:Tomcat 系统架构与设计模式,第 2 部分:设计模式分析 1 ...

    tomcat7源码

    《深入剖析Tomcat7源码》 Tomcat7是一款广泛使用的开源Java Servlet容器,它实现了Java EE中的Servlet和JSP规范。源码分析是提升开发者对服务器内部运作机制理解的重要途径,尤其对于Tomcat这样的核心组件,源码的...

    《深入剖析Tomcat》的光盘源码

    通过学习《深入剖析Tomcat》的源码,开发者不仅可以提升对Tomcat工作原理的理解,还能提升解决问题的能力,为解决实际开发中的性能瓶颈和安全问题提供强有力的支持。同时,对Tomcat源码的深入理解也有助于向其他Java...

    深入剖析Tomcat 随书 源码

    总的来说,《深入剖析Tomcat源码》这本书将带领我们探索Tomcat的每一个角落,从基础架构到高级特性,全面解析其设计思想和实现细节,帮助开发者更好地理解和优化自己的Java Web应用。通过学习和研究Tomcat源码,我们...

    最新设计模式超级详解+Tomcat架构源码分析+Spring源码分析 资深级设计模型课程

    Spring源码分析,web源码分析,Tomcat架构源码分析都是非常深入的源码级课程,期待研究设计模式和深入学习源码内功的朋友们,一定要仔细的学习研究。 (0);目录中文件数:1个 ├─3.代码.zip (1)\1.笔记;目录中文...

    Tomcat核心源码剖析.rar

    Tomcat核心源码剖析.rar

    tomcat8源码

    Apache Tomcat 8.5.23 源码分析 Apache Tomcat 是一个开源的、免费的Web服务器和Servlet容器,它实现了Java Servlet和JavaServer Pages(JSP)规范,是开发和部署Java Web应用的重要平台。深入理解Tomcat的源码有助...

Global site tag (gtag.js) - Google Analytics