---
title: "tomcat 7 源码分析-14 tomcat的container设计"
author: "smartvessel"
date: "2010-08-05T14:52:43Z"
tags: []
categories: ["企业架构"]
slug: "tomcat-7-14-tomcat-container"
draft: false
lastmod: "2010-08-06T10:41:22Z"
id: 729829
url: "https://smartvessel.iteye.com/blog/729829"
---

实现  
container←-----containerBase  
    ↑                          ↑  
    |继承                    |继承  
    |                          |  
    |         实现           |  
Engine←-----—-- StandardEngine  
context               Standardcontext  
host                    Standardhost  
wrapper              Standardwrapper

 

四种container分别是Engine,context,host和wrapper。

containerBase是对container相同部分的实现，四个interface又继承了container，增加了不同的部分。

如此StandardEngine，Standardcontext，Standardhost和Standardwrapper继承了相同的部分，同时实现了差异性的部分。

 

Engine:An Engine is a Container that represents the entire Catalina servlet engine.

 

Host:A Host is a Container that represents a virtual host in the Catalina servlet engine.

 

Context:A Context is a Container that represents a servlet context, and therefore an individual web application, in the Catalina servlet engine.

 

Wrapper: A Wrapper is a Container that represents an individual servlet definition from the deployment descriptor of the web application.

 

补充：写了一个简单的servlet，然后跟踪下发现如下

Engine=StandardEngine\[Catalina]

host=StandardEngine\[Catalina].StandardHost\[localhost]  
context=StandardEngine\[Catalina].StandardHost\[localhost].StandardContext\[/WelcomeServlet]  
wrapper=StandardEngine\[Catalina].StandardHost\[localhost].StandardContext\[/WelcomeServlet].StandardWrapper\[com.hotnosh.ServletTest.FirstServlet]

 

可以看出：

host实际是虚拟主机

context是webapplication，发布的web应用的根目录名

wrapper是具体处理servlet的

如此我在浏览器上输入http://localhost:8080/WelcomeServlet/FisrtServlet

 

 

GET /WelcomeServlet/FisrtServlet HTTP/1.1  
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, \*/*  
ThreadID: 4936  
Accept-Language: zh-cn  
UA-CPU: x86  
Accept-Encoding: gzip, deflate  
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; SE 2.X; .NET CLR 2.0.50727; .NET CLR 1.1.4322; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; SE 2.X)  
Host: localhost:8080  
Connection: Keep-Alive

 

 

向tomcat发出http的request，tomcat解析请求后，一步步直到具体的servlet处理函数。

调用栈：

Http11Processor.process(SocketWrapper&lt;Socket&gt; socketWrapper)

adapter.service(request, response);

connector.getService().getContainer().getPipeline().getFirst().invoke(request, response);

//调用StandardEngine的StandardEngineValve.invoke

host.getPipeline().getFirst().invoke(request, response);

//调用StandardHost的StandardHostValve.invoke

context.getPipeline().getFirst().invoke(request, response);

//调用Standardcontext的StandardcontextValve.invoke

wrapper.getPipeline().getFirst().invoke(request, response);

//调用StandardWrapper的StandardWrapperValve.invoke

```
Http11Processor.process(SocketWrapper<Socket> socketWrapper)

adapter.service(request, response);

connector.getService().getContainer().getPipeline().getFirst().invoke(request, response);

//调用StandardEngine的StandardEngineValve.invoke

host.getPipeline().getFirst().invoke(request, response);

//调用StandardHost的StandardHostValve.invoke

context.getPipeline().getFirst().invoke(request, response);

//调用Standardcontext的StandardcontextValve.invoke

wrapper.getPipeline().getFirst().invoke(request, response);

//调用StandardWrapper的StandardWrapperValve.invoke
```