StandardWrapperValve

StandardWrapperValve是StandardWrapper的基础阀,主要完成了三个工作。

1 调用StandardWrapper的allocate的方法来获得该StandardWrapper所表示的servlet实例

2 执行与该servelt相关联的全部过滤器

3 调用servlet的service方法



其中第二三步可以细分为

  调用它的 private createFilterChain 方法获得过滤链

  调用过滤器链的 doFilter 方法。这里面就调用了servlet 的 service方法

  释放过滤器链

  调用包装器的deallocate方法

如果Servlet无法使用了,调用包装器的 unload 方法

我们看看大致的代码片段

// Allocate a servlet instance to process this request
try {
    if (!unavailable) {
    servlet = wrapper.allocate();
    }
}
...
// Acknowlege the request
try {
    response.sendAcknowledgement();
}
...
// Create the filter chain for this request
    ApplicationFilterChain filterChain = createFilterChain(request,servlet);
// Call the filter chain for this request// This also calls the servlet's servicet() method
try {
    String jspFile = wrapper.getJspFile();
    if (jspFile != null)
    sreq.setAttribute(Globals.JSP_FILE_ATTR, jspFile);
    else
    sreq.removeAttribute(Globals.JSP_FILE_ATTR);
    if ((servlet != null) && (filterChain != null)) {
    filterChain.doFilter(sreq, sres);
    }
    sreq.removeAttribute(Globals.JSP_FILE_ATTR);
}
...
// Release the filter chain (if any) for this request
try {
    if (filterChain != null)
        filterChain.release();
}
...
// Deallocate the allocated servlet instance
try {
    if (servlet != null) {
        wrapper.deallocate(servlet);
    }
}
...
// If this servlet has been marked permanently unavailable,
// unload it and release this instance
try {
    if ((servlet != null) && (wrapper.getAvailable() ==Long.MAX_VALUE)) {
        wrapper.unload();
    }
}

看了上面的代码,大家应该能看出来最复杂的部分有两处

其一 ApplicationFilterChain filterChain = createFilterChain(request,servlet);

其二 filterChain.doFilter(sreq, sres);

一步一步来。

FilterDef类

这个类的全名应该是FilterDefinition,过滤器描述类。

里面包含了一个Filter的filterClass,filterName等基本信息,及get/set方法。

这里面的属性,我们可以看看这个

    /**
     * The set of initialization parameters for this filter, keyed by
     * parameter name.
     */
    private Map<String, String> parameters = new HashMap<String, String>();

用HashMap存储了初始化参数,它有get方法,增加属性的方法是addInitParameter(String name, String value)。

ApplicationFilterConfig类

org.apache.catalina.core.ApplicationFilterConfig 实现了javax.servlet.FilterConfig 接口。ApplicationFilterConfig 负责管理 web应用程序启动的时候创建的过滤器实例。

其构造函数如下:

public ApplicationFilterConfig(Context context, FilterDef filterDef)

                                 throws ClassCastException, ClassNotFoundException,IllegalAccessException, InstantiationException, ServletException

在这里我们主要谈谈它的getFilter方法,该方法的功能其实就是加载过滤器类并初始化它。

首先从filterDef里面获得filterClass;

        String filterClass = filterDef.getFilterClass();
        ClassLoader classLoader = null;
        if (filterClass.startsWith("org.apache.catalina."))
            classLoader = this.getClass().getClassLoader();
        else
            classLoader = context.getLoader().getClassLoader();
    .....
            Class<?> clazz = classLoader.loadClass(filterClass);
        this.filter = (Filter) clazz.newInstance();
        filter.init(this);
        return (this.filter);

还是没有什么要说的。

ApplicationFilterChain类

StandardWrapperValve 类中的 invoke 方法创建一个该类的实例并且调用它的 doFilter 方法。ApplicationFilterChain类的doFilter(其实是internalDoFilter)调用该链中第一个过滤器的 doFilter 方法。

ApplicationFilterChain类中,有一个ApplicationFilterConfig的引用

private ArrayList<ApplicationFilterConfig> filters = new ArrayList<ApplicationFilterConfig>();

看到了把,数组形式来存放链条。

典型的责任链模式。

Filter 接口中doFilter 方法的签名如下:

public void doFilter(ServletRaquest request, ServletResponse response,FilterChain chain)
            throws java.io.IOException, ServletException
在ApplicationFilterChain的doFilter方法中,它会将自己作为第三个参数传递给它。
我们看一个Filter实现类的例子
public void doFilter(ServletRequest request, ServletResponse response,
                    FilterChain chain) throws IOException, ServletException {
    // do something here
    ...
    chain.doFilter(request, response);
}

循环往复了

此处的循环不是那么容易理解,建议参考鄙人的另一篇博客

http://blog.csdn.net/dlf123321/article/details/40078583

等所有的Filter都执行完了,就是下面的代码

           if ((request instanceof HttpServletRequest) &&
                (response instanceof HttpServletResponse)) {
                servlet.service((HttpServletRequest) request,
                                (HttpServletResponse) response);
            } else {
                servlet.service(request, response);
            }

什么时候Filter才算执行完了呢?

 private Iterator<ApplicationFilterConfig> iterator = null;

....

 if (this.iterator == null)
            this.iterator = filters.iterator();

        // Call the next filter if there is one
        if (this.iterator.hasNext()) {

                    //执行filter

       }
...
      //调用servlet的service的代码块
...

应用程序

和之前的几章没有什么区别,不再赘述。

how tomcat works 读书笔记 十一 StandWrapper 下的更多相关文章

  1. how tomcat works 读书笔记 十一 StandWrapper 上

    方法调用序列 下图展示了方法调用的协作图:  这个是前面第五章里,我画的图:  我们再回顾一下自从连接器里  connector.getContainer().invoke(request, resp ...

  2. how tomcat works 读书笔记(二)----------一个简单的servlet容器

    app1 (建议读者在看本章之前,先看how tomcat works 读书笔记(一)----------一个简单的web服务器 http://blog.csdn.net/dlf123321/arti ...

  3. how tomcat works 读书笔记四 tomcat的默认连接器

    事实上在第三章,就已经有了连接器的样子了,只是那仅仅是一个学习工具,在这一章我们会開始分析tomcat4里面的默认连接器. 连接器 Tomcat连接器必须满足下面几个要求 1 实现org.apache ...

  4. How tomcat works 读书笔记十五 Digester库 下

    在这一节里我们说说ContextConfig这个类. 这个类在很早的时候我们就已经使用了(之前那个叫SimpleContextConfig),但是在之前它干的事情都很简单,就是吧context里的co ...

  5. How tomcat works 读书笔记十七 启动tomcat 下

    在上一节中,我们程序的起始位置还是Bootstrap,现在我们通过bat文件来启动这个类. 在分析catalina.bat之前,我们先看看几个简单的我们能用到的dos命令. 基础知识 1 rem 注释 ...

  6. How tomcat works 读书笔记十二 StandardContext 下

    对重载的支持 tomcat里容器对重载功能的支持是依靠Load的(在目前就是WebLoader).当在绑定载入器的容器时 public void setContainer(Container cont ...

  7. how tomcat works 读书笔记 八 载入器下

    载入类 我们看看之前的文章,这一节就从SimpleWrapper的loadServlet讲起. SimpleWrapper.java如下(省略了try catch及其他部分代码) public Ser ...

  8. how tomcat works 读书笔记(一)----------一个简单的webserver

    http协议 若是两个人能正常的说话交流,那么他们间必然有一套统一的语言规则<在网络上server与client能交流也依赖与一套规则,它就是我们说的http规则(超文本传输协议Hypertex ...

  9. how tomcat works 读书笔记(一)----------一个简单的web服务器

    http协议 若是两个人能正常的说话交流,那么他们间必定有一套统一的语言规则<在网络上服务器与客户端能交流也依赖与一套规则,它就是我们说的http规则(超文本传输协议Hypertext tran ...

随机推荐

  1. everything of people’s life can changed in their twenties

    还记得三年前,独自背着行李,流浪远方,来到曾经只在地理课本上才熟悉的北国,带着好奇,带着期望,带着激动的心情,想感受毛爷爷当年霸气的北国风光,千里冰封的美丽,想知道北方的面条到底有多少种花样,想走进那 ...

  2. 12 SharedPreferences

    SharedPreferences 创建方式 SharedPreferences preferences = getPreferences(Context context ,int mode); 参数 ...

  3. android获取设备唯一标示

    概述 有时需要对用户设备进行标识,所以希望能够得到一个稳定可靠并且唯一的识别码.虽然Android系统中提供了这样设备识别码,但是由于Android系统版本.厂商定制系统中的Bug等限制,稳定性和唯一 ...

  4. Android实战之ListView复选框

    项目中有用到复选框的例子,啊啊......在网上查找有关资料,大多都是过于繁琐,所以自己决定写个这个方面的demo... 先给个效果图: 在ListView中添加复选框主要注意以下几个问题: 1.Li ...

  5. Andriod Studio科普篇——4.关于编译的常见问题

    1.android支持库未安装 编译不过,提示如下: Could not find any version that matches com.android.support:appcompat-v7: ...

  6. iOS中 简单易懂的秒杀倒计时/倒计时

    示例代码简单易懂: 每日更新关注:http://weibo.com/hanjunqiang  新浪微博 #import <UIKit/UIKit.h> @interface ViewCon ...

  7. JAVA之旅(二十五)——文件复制,字符流的缓冲区,BufferedWriter,BufferedReader,通过缓冲区复制文件,readLine工作原理,自定义readLine

    JAVA之旅(二十五)--文件复制,字符流的缓冲区,BufferedWriter,BufferedReader,通过缓冲区复制文件,readLine工作原理,自定义readLine 我们继续IO上个篇 ...

  8. ASP.NET实现网页版小优盘

    今天看到了一篇不错的文章,就拿来一起分享一下吧. 实现的是文件的上传与下载功能. 关于文件上传: 谈及文件上传到网站上,首先我们想到的就是通过什么上传呢?在ASP.NET中,只需要用FileUploa ...

  9. TCP的定时器系列 — 超时重传定时器

    主要内容:TCP定时器概述,超时重传定时器.ER延迟定时器.PTO定时器的实现. 内核版本:3.15.2 我的博客:http://blog.csdn.net/zhangskd Q:一条TCP连接会使用 ...

  10. 使用LogKit进行日志操作

    1.      概述 任何一个系统中,日志都是不可缺少的,现在Apache提供了两套日志工具,一个就是Log4j,另一个是本文要给出例子的LogKit. Log4j和LogKit有很多相似的地方.比如 ...