servlet容器是用来处理请求servlet资源,并为Web客户端填充response对象的模块。在上一篇文章(也就是书的第四章)我们设计了SimpleContainer类,让他实现Container接口,也基本完成了容器的作用。但是我们得知道在实际的tomcat中有4类容器:

Engine: 表示整个Catalina servlet引擎;

Host: 包含一个或多个Context容器的虚拟主机;

Context:表示一个Web应用程序,一个Context可以包含多个Wrapper

Wrapper: 表示一个独立的Servlet;

UML图如下:

Container容器中包含addChild(Container con),removeChild(Container con),findChild(String name),findChildren()四个方法,方法的作用就不解释了,Wrapper就表示一个独立的Servlet所以,它的addChild方法体为空。

在本节,我们主要展示Wrapper的特性,其他的容器以后再说。

首先我们看看时序图(第一次画 可能在时序图规则上有些问题)

管道任务

这节主要说明档连接器调用了servlet容器的invoke方法后发生的事情。

管道包含了一个servlet容器要执行的任务(和本身的servlet无关,是指额外执行的任务)

一个Wrapper会包含一个管道,而一个管道会包含若干个Valve(阀)

public class SimpleWrapper implements Wrapper, Pipeline {

  // the servlet instance
  private Servlet instance = null;
  private String servletClass;
  private Loader loader;
  private String name;
  private SimplePipeline pipeline = new SimplePipeline(this);
  protected Container parent = null;
  ...
}
public class SimplePipeline implements Pipeline {

  public SimplePipeline(Container container) {
    setContainer(container);
  }

  // The basic Valve (if any) associated with this Pipeline.
  protected Valve basic = null;
  // The Container with which this Pipeline is associated.
  protected Container container = null;
  // the array of Valves
  protected Valve valves[] = new Valve[0];            //所有的阀
}



在阀中,请求会像水一样经过管道中的每一个阀,上图中的阀n会调用我们请求的servlet类。

再调用容器的invoke方法后,会到如下的方法

invokeNext是StandardPipelineValveContext类中的方法(StandardPipelineValveContext是管道的一个内部类,因此可以访问管道的所有成员)
 public void invokeNext(Request request, Response response)
      throws IOException, ServletException {
      int subscript = stage;
      stage = stage + 1;
      // Invoke the requested Valve for the current request thread
      if (subscript < valves.length) {
        valves[subscript].invoke(request, response, this);
      }
      else if ((subscript == valves.length) && (basic != null)) {
        basic.invoke(request, response, this);
      }
      else {
        throw new ServletException("No valve");
      }
    }
  } // end of inner class

ok?如水般流过。下面这行代码值得我们仔细看看

valves[subscript].invoke(request, response, this);

也可参阅拙作

<<说说struts2中拦截器的请求流程一(模拟大致流程)>>

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

public class HeaderLoggerValve implements Valve, Contained {

  protected Container container;

  public void invoke(Request request, Response response, ValveContext valveContext)
    throws IOException, ServletException {

    // Pass this request on to the next valve in our pipeline
    valveContext.invokeNext(request, response);

    System.out.println("Header Logger Valve");
    ServletRequest sreq = request.getRequest();
    if (sreq instanceof HttpServletRequest) {
      HttpServletRequest hreq = (HttpServletRequest) sreq;
      Enumeration<?> headerNames = hreq.getHeaderNames();
      while (headerNames.hasMoreElements()) {
        String headerName = headerNames.nextElement().toString();
        String headerValue = hreq.getHeader(headerName);
        System.out.println(headerName + ":" + headerValue);
      }

    }
    else
      System.out.println("Not an HTTP Request");

    System.out.println("------------------------------------");
  }
}

这行代码 valveContext.invokeNext(request, response); 看懂了吧

另一方面 大家应该能看出来,所有的阀都是逆向执行的,每次执行一个阀的时候,阀都会先传递给后面的阀,等到后面的执行完毕后,才执行自己的业务逻辑。

当通道里的处了最后一个基础阀外,都走了一遍的时候自然就执行如下代码

 basic.invoke(request, response, this);

 问题是basic是谁?

这个咱们等会再说。

Pipeline接口

我们在这里使用的是SimplePipeline,它实现了Pipeline的接口。

public interface Pipeline {
public Valve getBasic();
public void setBasic(Valve valve);
public void addValve(Valve valve);
public Valve[] getValves();
public void invoke(Request request, Response response)
throws IOException, ServletException;
public void removeValve(Valve valve);
}

Pipeline里面有个基础阀,是最后调用的,负责处理request对象鱼response对象。因而这里有getset方法

通道里面存放的是阀,所以剩下的方法就不用多说了吧。

另外tomcat4中,还存在一个StandardPipelineValveContext,上面已经说它是SimplePipeline的一个内部类。

Valve接口

public interface Valve {
public String getInfo();
public void invoke(Request request, Response response,ValveContext context) throws IOException, ServletException;
}

很清晰,不是吗?

Contained接口

public interface Contained {
public Container getContainer();
public void setContainer(Container container);
}

实现这个接口的类,至多与一个servlet容器相关联。

Wrapper接口

首先我们在文章开始的时候就说了,Wrapper包装的是一个最基础的servlet

这个接口里面有两个方法比较重要

public javax.servlet.Servlet allocate() throws javax.servlet.ServletException;

public void load() throws javax.servlet.ServletException;

allocate会分配一个已经初始化的servlet实例,

load会载入它。

Wrapper应用程序

类图如下:

SimpleLoader类

public class SimpleLoader implements Loader {

  public static final String WEB_ROOT =
    System.getProperty("user.dir") + File.separator  + "webroot";

  ClassLoader classLoader = null;
  Container container = null;

  public SimpleLoader() {
    try {
      URL[] urls = new URL[1];
      URLStreamHandler streamHandler = null;
      File classPath = new File(WEB_ROOT);
      String repository = (new URL("file", null, classPath.getCanonicalPath() + File.separator)).toString() ;
      urls[0] = new URL(null, repository, streamHandler);
      classLoader = new URLClassLoader(urls);
    }
    catch (IOException e) {
      System.out.println(e.toString() );
    }
  }
}

构造函数会初始会类加载器,供SimpleWrapper使用

SimpleWrapper类

该类实现了 org.apache.catalina.Wrapper 接口并且实现了 allocate 方法和load 方法。

器构造函数如下

  public SimpleWrapper() {

    pipeline.setBasic(new SimpleWrapperValve());

  }

上文我们曾问过,basic从哪里来,从这里来!

SimpleWrapperValve类

通过pipeline.setBasic(new SimpleWrapperValve())我们知道SimpleWrapperValve类是一个基础阀,用来处理传递Servlet的参数,如下:

public void invoke(Request request, Response response, ValveContext valveContext)
    throws IOException, ServletException {

    SimpleWrapper wrapper = (SimpleWrapper) getContainer();
    ServletRequest sreq = request.getRequest();
    ServletResponse sres = response.getResponse();
    Servlet servlet = null;
    HttpServletRequest hreq = null;
    if (sreq instanceof HttpServletRequest)
      hreq = (HttpServletRequest) sreq;
    HttpServletResponse hres = null;
    if (sres instanceof HttpServletResponse)
      hres = (HttpServletResponse) sres;

    // Allocate a servlet instance to process this request
    try {
      servlet = wrapper.allocate();
      if (hres!=null && hreq!=null) {
        servlet.service(hreq, hres);
      }
      else {
        servlet.service(sreq, sres);
      }
    }
    catch (ServletException e) {
    }
  }

应该没有什么问题。

ClientIPLoggerValve类

是用来显示客户端的ip地址的,代码从略,另外还有HeaderLoggerValve,用来显示http的请求头,代码从略。

Bootstrap1启动类

public final class Bootstrap1 {
  @SuppressWarnings("deprecation")
public static void main(String[] args) {

/* call by using http://localhost:8080/ModernServlet,
   but could be invoked by any name */

    HttpConnector connector = new HttpConnector();
    Wrapper wrapper = new SimpleWrapper();
    wrapper.setServletClass("ModernServlet");

    Loader loader = new SimpleLoader();

    Valve valve1 = new HeaderLoggerValve();
    Valve valve2 = new ClientIPLoggerValve();

    wrapper.setLoader(loader);
    ((Pipeline) wrapper).addValve(valve1);
    ((Pipeline) wrapper).addValve(valve2);

    connector.setContainer(wrapper);

    try {
      connector.initialize();
      connector.start();

      // make the application wait until we press a key.
      System.in.read();
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
}

运行结果如下:



看到了,其实在部分里,不管你请求那个服务,返回的结果都一样,为什么?这个还用我来解释吗?





这一章东西比较多,关于context容器的使用,我们放在下一节中。

how tomcat works 五 servlet容器 上的更多相关文章

  1. how tomcat works 5 servlet容器 下

    上一节,我们主要说的是Wrapper容器,这一节我们说Context容器. 再重申一遍,一个Context容器可以包含多个Wrapper容器; 一个Wrapper容器就表示一个独立的servlet. ...

  2. Tomcat是一个Servlet容器?

    "Tomcat是一个Servlet容器",这句话对于2019年的程序员应该是耳熟能详的. 单纯的思考一下这句话,我们可以抽象出来这么一段代码: class Tomcat { Lis ...

  3. How Tomcat works — 五、tomcat启动(4)

    前面摆了三节的姿势,现在终于要看到最终tomcat监听端口,接收请求了. 目录 Connector Http11Protocol JIoEndpoint 总结 在前面的初始化都完成之后,进行Conne ...

  4. Web服务器(Apache)与Servlet容器(Tomcat)

    之前一直比较迷惑Apache与Tomcat的关系,通过查询资料,有所了解,现记录于此. Apache与Tomcat 两者定位:Apache是HTTP Web服务器,Tomcat是Web容器. 有一个非 ...

  5. httpServletRequest对象、filter、servlet、servlet容器、catalina、tomcat、以及web容器之间的关系

    学习servlet的时候经常感到疑惑 HttpServletRequest是服务器创建的?还是servlet容器创建的? 过滤器是服务器创建的?还是servlet容器创建的? serlet容器和tom ...

  6. Servlet容器container

    通俗点说,所谓容器,就是放东西的地方.Servlet容器自然就是放Servlet的地方.J2EE开发,是有分工的.一般的程序员,写得都是应用开发,我们会按照一定的规则,开发我们的系统,比如用Servl ...

  7. 为什么要有 Servlet ,什么是 Servlet 容器,什么是 Web 容器?

    本文已收录至 https://github.com/yessimida/yes ,这里有我的所有文章分类汇总,欢迎 star! 以下代码相信大家都很熟悉,大学时学 Java Web 都写过这样的代码. ...

  8. tomcat与jboss等容器的区别

    1.JBoss 是 J2EE 应用服务器,而 Tomcat 只是一个 Servlet 容器,或者说是一个简单的 J2EE 应用服务器. JBoss 中的 Servlet 容器还是 Tomcat. 与  ...

  9. springboot(七) 配置嵌入式Servlet容器

    github代码地址:https://github.com/showkawa/springBoot_2017/tree/master/spb-demo/spb-brian-query-service ...

随机推荐

  1. Android系统对话框——自定义关闭

    Android系统对话框--自定义关闭 Dialog是我们在项目中经常用到的,5.x以后的Dialog也很好看,很安卓风,Android也给我们提供了新的包,低版本可以显示一样的效果.我们在使用的导入 ...

  2. dubbo安装

    dubbo 管控台可以对注册到 zookeeper 注册中心的服务或服务消费者进行管理,分享牛系列,分享牛专栏,分享牛.但管控台是否正常对 Dubbo 服务没有影响,管控台也不需要高可用,因此可以单节 ...

  3. 剑指Offer——知识点储备-网络基础

    剑指Offer--知识点储备-网络基础 计算机网络 http和https的区别 (1)http是http协议运行在tcp之上,所传输的内容都是明文,客户端和服务器端都无法验证对方的身份. (2)htt ...

  4. 无需密码通过scp命令+key的方式实现文件传输

    如果觉得scp每次都要输入密码很麻烦, 那么这是解决方案.假设你平时在windows上开发,用户名是xiang, 你有一台Ubuntu服务器wdksw.com, 用户名是root.现在你准备上传一些文 ...

  5. BI Publisher(rtf)模板开发语法大全

    Rtf模板开发例如背景,纹理分栏等等功能都能用word工具实现不再具体总结大家可以参考word教程.....   一.组 定义一个组的目的是告诉XMLPublisher对重复的数据行进行循环显示,也就 ...

  6. FORM开发之说明性弹性域开发

    1.注册使用弹性域的表,字段 注册表语法:ad_dd.register_table('所有者','表名','T自动扩展/S非自动扩展','下一区','自由','已使用') AD_DD.REGISTER ...

  7. activiti 配置节点 连线信息获取

    1.1.1. 前言 当使用eclipse插件进行流程设计的时候,部署流程之后,我们如何获取我们定义的所有的节点.连线.关口等配置信息呢?有的人看到这个需求,不免窃喜,这不很简单嘛,重新打来bmpn中定 ...

  8. Android时遇到R.java was modified manually! Reverting to generated version!

    欢迎关注公众号,每天推送Android技术文章,二维码如下:(可扫描) 进入 eclipse后clipse Menu >Projects > clean 这么做就把R文件删了,但是别担心, ...

  9. Swift基础之对FMDB第三方的使用方法

    相信大家都熟悉OC使用FMDB第三方库,进行数据库操作,增.删.改.查,现在我就来利用代码展示一下Swift对此库的使用方法,我是通过Pods添加的第三方库,如果手动添加记得创建桥接文件,在文件中调用 ...

  10. Erlang 集群互连测试

    Erlang 集群互连测试Erlang节点相同cookie全互联成为一个集群(cluster).如果2个集群不同cookie, 然后其中有节点连接到对方集群的节点,这2个集群会合并成一个集群吗?连接到 ...