(基于tomcat 5.0.28)
A Container is an object that can execute requests received from a client, and return responses based on those requests. A Container may optionally support a pipeline of Valves that process the request in an order configured at runtime, by implementing the Pipeline interface as well.
Containers will exist at several conceptual levels within Catalina. The following examples represent common cases:
Engine - Representation of the entire Catalina servlet engine, most likely containing one or more subcontainers that are either Host or Context implementations, or other custom groups.
Host - Representation of a virtual host containing a number of Contexts.
Context - Representation of a single ServletContext, which will typically contain one or more Wrappers for the supported servlets.
Wrapper - Representation of an individual servlet definition (which may support multiple servlet instances if the servlet itself implements SingleThreadModel).
A given deployment of Catalina need not include Containers at all of the levels described above. For example, an administration application embedded within a network device (such as a router) might only contain a single Context and a few Wrappers, or even a single Wrapper if the application is relatively small. Therefore, Container implementations need to be designed so that they will operate correctly in the absence of parent Containers in a given deployment. (此處摘自Container的javadoc)
Container - 執(zhí)行來自客戶的request,并生起相應(yīng)的response. container可以通過 pipleline 和 valvel 來實(shí)現(xiàn)執(zhí)行時(shí)的rquest動(dòng)態(tài)處理,container也可以自己實(shí)現(xiàn) Pipeline接口。
Container的概念覆蓋Catalina的幾個(gè)其他結(jié)構(gòu)層次,比如下面的幾個(gè)例子:
Engine - 代表了整個(gè) Catalina servlet engine, 一般包括幾個(gè)子 container,比如 Host, Context,或其他的一些配置
Host - 代表了一個(gè)包含Context的虛擬Host,
Wrapper - 代表某個(gè)特定servlet得實(shí)現(xiàn)(此實(shí)現(xiàn)可以僅僅是一個(gè)servlet的實(shí)例,或者如果此servlet實(shí)現(xiàn)了SingleTreadModel,可以是多個(gè)servlet)
一個(gè)特定的Catalina部署不必包含所有上面描述的container。比如說一個(gè)特殊的內(nèi)嵌在網(wǎng)絡(luò)設(shè)備上(比如路由器)的管理應(yīng)用程序,可以只包含單獨(dú)的一個(gè)context和很少的幾個(gè)Wrapper, 或者是干脆只有一個(gè)Wrapper。因此,在實(shí)現(xiàn)container時(shí)要考慮到也許他們會(huì)在沒有父container的環(huán)境中運(yùn)行。
Engine - 代表了整個(gè) Catalina servlet engine。
Engine的子container一般會(huì)是Host,或者 Context.
Engine總是在Catalina 的container 中最頂?shù)囊粋€(gè)Container, 所以Engine的 setParent方法拋出一個(gè)IllegalArgumentException異常。
Host - 代表了一個(gè)包含Context的虛擬Host,比如localhost 或者 http://www.yourdomain.com/等等
Host的父Container一般是engine或者為空
Host的子Container一般是Context,代表一個(gè)部署在此Host上的一個(gè)特定的應(yīng)用。
在server.xml中context可能會(huì)有如下的對應(yīng)
<Host name="http://www.yourdomain.com/" debug="0" unpackWARs="true" autoDeploy="true" xmlValidation="false" >
......
</Host>
Context- Context 也是一個(gè)Container,它代表了Host 下的一個(gè)特定的應(yīng)用(web application),context的父Container通常是Host,子Container通常是Wrapper. 在server.xml中context可能會(huì)有如下的對應(yīng)
<Context path="/ctxpath" docBase="d:\docbase" workDir="d:\workdir" />
Wrapper - Wrapper 代表某個(gè)特定servlet得實(shí)現(xiàn),它也是一個(gè)Container. Wrapper 負(fù)責(zé)裝載,初始化,銷毀其所對應(yīng)的 servlet。 它的父Container通常是Context. web.xml中的servlet定義對應(yīng)相應(yīng)的wrapper. Wrapper 沒有子container.
Connector-負(fù)責(zé)接受來自客戶端的request,并返回response到客戶端。比如coyote 是一個(gè)http connector,它復(fù)雜接受http請求,
也可以有一個(gè)ftp connector, 負(fù)責(zé)處理ftp請求。
Service 是一組共享同一個(gè)container(比如engine)的connector.比如一個(gè)service中可以有處理ssl-http請求和non-ssl-http的Connctor,它們共享同一個(gè)Container
valve - 是屬于特定Container的專門處理request的組件。一個(gè)特定container中會(huì)有多個(gè)valve,他們在container的pipeline中。在處理request時(shí)被依次調(diào)用。
pipeline,屬于特定container的, valve的集合。 在處理request時(shí),pipeline中的request會(huì)被依次調(diào)用。每個(gè)container一般都有特定的一個(gè)valve生成最終返回給用戶的response(其他的valve也許只是對request或response作一些檢查或設(shè)置),這個(gè)valve在pipeline中最后被執(zhí)行。因此,在pipeline的實(shí)現(xiàn)中,有一個(gè)setBasic()方法,就是設(shè)置這個(gè)特定的valve 的。與 engine對應(yīng)的這個(gè)basic valve 是StandardEngineValue, Host對應(yīng)的是 StandardHostPipeLine. 大多數(shù)情況下,每個(gè)Container只有一個(gè)basic valve.
以上的幾個(gè)概念一般在server.xml和web.xml中都有對應(yīng)。
一般來講,一個(gè)簡單的catalina實(shí)現(xiàn)就是最頂層一個(gè)server, 之后是一個(gè)service,其中包括兩個(gè)結(jié)構(gòu)上并列的的組件,connctor,和engine.(connector 接受請求,engine處理請求). engine 下面是Host, Host 下面是 Context, Context下面是Wrapper,最下面是 servlet.