Overall structure
We want to learn a framework, we must first understand what it is, Tomcat we all know, is used to treat over Socket connection request. Tomcat will then have two functions:
-
- Foreign processing connections, will receive byte stream into Request and Response object you want
- Internal processing Servlet, corresponding Request distribute requests to the Servlet corresponding
Then we came out the overall framework, Tomcat in fact divided into two parts, one part is a connector (Connnector) processing Servelet for external connection and the container (Container) of internal management.
The diagram is generally as follows:
description:
The outermost layer of the large box is to represent a Tomcat service, a service may correspond to multiple Tomcat Service. Each Service has connectors and the container.
The corresponding relationship we can also open the configuration file in the Tomcat directory server.xml
seen it.
<Server port="8006" shutdown="SHUTDOWN"> <Service name ="Catalina"> <Connector port ="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443"/> <Connector port="8010" protocol="AJP/1.3" redirectPort="8443"/> <Engine name="Catalina" defaultHost="localhost"> <Realm className="org.apache.catalina.realm.LockOutRealm"> <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/> </Realm> <Host name="localhost" appBase="webapps"></Host> </Engine> </Service> </Server>
Here we can see the connector in fact Connector
, a Service can have multiple connectors, the container is in fact correspond Engine
.
Is such a correspondence between the overall architecture of Tomcat is simple. Next we brief the overall architecture of the overall structure and container connectors.
Connector
We can see the figure above the connector is passed to a container ServletRequest
object, the container is passed to the connector ServletResponse
objects, which are certainly not in network transfer, the byte stream as transmitted in the transmission network.
So the functional requirements of the connector we would be able to sum up the following points.
-
- Socket connection
- Network byte stream read request
- (Http / AJP) parsing a byte stream according to a corresponding protocol, generates unified
TomcatRequest
t Object - The
TomcatReques
pass container - Container return
TomcatResponse
objects - The
TomcatResponse
object is converted to a byte stream - The byte stream is returned to the client
In fact, the above segments can be summarized in the following three points
-
- Telecommunication
- Analytical application layer protocol
- Tomcat is
Request/Response
theServletRequest/ServletResponse
conversion of objects
And it is also used in Tomcat three classes to implement the above three functions, respectively, correspond to the following
-
- EndPoint
- Processor
- Adapter
With the figure shows their relationship is so, then
container
Container, by definition is to hold things utensil, then this is what the Tomcat container installed it? Actually, the key is installed Servlet's.
Then the container is how to design it? Tomcat container design is actually a combination of design patterns (you can see a combination of design patterns do not understand my previous articles do not learn many - combined mode).
In fact, from Server.xml
that we can see their relationship.
<Engine name="Catalina" defaultHost="localhost"> <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"></Host> </Engine>
In it we can only see a container of two modules, one top-level module Engine
, and the other is Host
,
In fact, there are two modules:
One is the Context
correspondence of our webapp inside each application folders, each folder that corresponds to one Context
,
There is also a module Wrapper
corresponds to us Context
all in the servlet, Wrapper
manage the access correspondence relationship with a specific Servlet's. Below this is represented by FIG.
Tomcat container all modules implements Container
the interface, but the significance is that the combination patterns using a single user objects and combinations of objects uniformly,
即无论添加多少个 Context
其使用就是为了找到其下面的Servlet,而无论添加多少个Host也是为了找个下面的Servlet。
而在容器中设计了这么多的模块,一个请求过来Tomcat如何找到对应的Servlet进行处理呢?
请求如何定位
我们就举个最简单的例子,我们本机应用上启动了一个Tomcat,webapp下有我们部署的一个应用 buxuewushu
。
我们在浏览器上输入 http://localhost:8080/buxuewushu/add.do
是如何找到对应Servlet进行处理呢?
在我们启动Tomcat的时候,连接器就会进行初始化监听所配置的端口号,这里我们配置的是8080端口对应的协议是HTTP。
-
- 请求发送到本机的8080端口,被在那里监听的HTTP/1.1的连接器Connector获得
- 连接器Connector将字节流转换为容器所需要的
ServletRequest
对象给同级Service
下的容器模块Engine进行处理 - Engine获得地址
http://localhost:8080/buxuewushu/add
。匹配他下面的Host主机 - 匹配到名为localhost的Host(就算此时请求为具体的ip,没有配置相应的Host,也会交给名为localhost的Host进行处理,因为他是默认的主机)
- Host匹配到路径为
/buxuewushu
的Context,即在webapp下面找到相应的文件夹 - Context匹配到URL规则为*.do的servlet,对应为某个Servlet类
- 调用其
doGet
或者doPost
方法 - Servlet执行完以后将对象返回给Context
- Context返回给Host
- Host返回给Engine
- Engine返回给连接器Connector
- 连接器Connector将对象解析为字节流发送给客户端