《How Tomcat works》

容器是一个处理用户servlet请求并返回对象给web用户的模块。
org.apache.catalina.Container接口定义了容器的形式,用四种容器:Engine(引擎),Host(主机),Context(上下文),和Wrapper(包装器)。这一章将会介绍Context和wrapper。而Enginer和Host会留到第十三章介绍。这一章首先介绍容器接口,然后介绍容器的工作流程。然后介绍的内容是Wrapper和Context接口。然后用两个例子来总结wrapper和context容器。

容器接口

一个容器必须实现org.apache.catalina.Container接口。就如在第四章看到的那样,传递一个Container实例给Connect对象的setContainer方法,然后Connect对象就可以使用container的invoke方法,重新看下第四章中Bootstrap类的代码如下:

HttpConnector connector = new HttpConnector();
SimpleContainer container = new SimpleCOntainer();
connector.setContainer(container);

对于Catalina容器首先需要注意的是它一共有四种不同的容器:
Engine:表示整个Catalina的servlet引擎
Host:表示一个拥有数个上下文的虚拟主机
Context:表示一个web应用,一个context包含一个或多个wrapper
Wrapper:表示一个独立的servlet

每一个概念之上都是用org.apache.catalina包来表示的。Engine Host Context 和 Wrapper接口都实现了Container即可。它们的标准实现是StandardEngine,StandardHost,StandardContext,and StandardWrapper,它们都是org.apache.catalina.core包的一部分。
所有的类都扩展自抽象类ContainerBase.
一个Catalina功能部署不一定需要所有的四种类型的容器。
一个容器可以有一个或者多个低层次的子容器。其大小关系是Engine->Host->Context->Wrapper。

Contaner接口被设计成Tomcat管理员可以通过Server.xml文件配置来决定其工作方式的模式。他通过一个pipeline(流水线)和一系列的阀门来实现。

原文地址:https://www.cnblogs.com/theone67/p/11179274.html