Learn the engine and host container of Tomcat (4)

Time:2021-10-20

In the previous article, we introduced the connector of Tomcat. The connector will listen to the specified port, convert the received message into HttpServletRequest and httpservletresponse, and submit it to a servlet container for processing. Tomcat’s servlet containers are divided into four types: engin container / host container / context container / wrapper container. These four containers have a parent-child relationship. The engine container contains the host container, the host container contains the context, and the context contains the wrapper container. This article will introduce the ngin container and host container in the Tomcat container, and the context container and wrapper container in the next article.

Function of container

The container in Tomcat is used to process the request and response processed by the connector. The four containers in Tomcat inherit from the container interface. Among them, there is only one English container globally, which is the container’s external access to handle request and response. Host container is a sub container of engin container. An engin container can contain multiple host containers, and each host container represents a virtual host (described in detail below). After receiving the request, the engin container will map the request to the corresponding host container according to the configuration of the virtual host.

Container包含关系

Structure of container

As shown in the figure below, the four containers in Tomcat have the same structure, including the following key components: request processing valve chain pipeline, basic valve basevalve and log components.

  1. Pipeline: used for information in flow processing request. Each pipeline can contain multiple valves, and each valve has the same methodinvoke(Request request,Response response)
pattern="%h %l %u %t "%r" %s %b" />
  1. Basevalve: basic valve, the same method as the valve interface in pipe:invoke(Request request,Response response)However, the function is different from the valve in the pipe. It is mainly used to pass the request to the next container or the corresponding servlet component.
  2. Other components such as loggers and lifecycle management are not described in detail.

Container容器结构

Engine container

Engine容器结构

As shown in the above figure, each Tomcat has only one engin container. After the connector in Tomcat accepts and parses the message, it will transfer the message to the engin container. Users can add various custom valves to the pipeline of the engin container, and the engin container will call the valves in the pipeline one by one. Basevalve of the ngin container isStandardEngineValve, this valve will read the host information in the request and route the request to the corresponding host container.

final class StandardEngineValve extends ValveBase {

    public StandardEngineValve() {
        super(true);
    }

    @Override
    public final void invoke(Request request, Response response)
        throws IOException, ServletException {

        // Ignore some code here.
        // Select the Host to be used for this Request
        Host host = request.getHost();
        
        // Ignore some code here.

        // Ask this Host to process this request
        host.getPipeline().getFirst().invoke(request, response);
    }
}

Host container

The host container is a sub container of the engine container. Each host container is a virtual host, corresponding to different domain names. Starting from 1.1, HTTP protocol supports adding the host field in the request header to represent the domain name of the request. During DNS domain name resolution, different domain names can be resolved to the same IP or host. The BaseValve of the Engine container reads the Host in Request, and then calls the PipeLine corresponding to the Host container to process the message.

Host路由原理

What is a virtual host

If we need to support three domain names in a Tomcat at the same time:

We need to add multiple host tags under the engine tag in the server.xml file, as shown below, where name represents the domain name and appbase represents the directory of the virtual host. When we enter in the browser http://www.ramki.com After that, the corresponding domain name will be requested to Tomcat. Tomcat reads and searches server.xml to find the virtual host host corresponding to www.ramki.com, and then uses the found host to process the request.

When the browser requests, the request header information is as follows. Here we focus on the host header.

GET /appA/servletA/some-url HTTP/1.1 
Host: www.ramki.com 
Proxy-Connection: keep-alive 
User-Agent: Mozilla/5.0 (Windows NT 6.2) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11 
Accept: text/html,application/xhtml+xml,application/xml;q=0.9
Accept-Encoding: gzip,deflate,sdch 
Accept-Language: en-US,en;q=0.8

Context container

A host container in Tomcat can contain multiple context containers. Usually, a context container identifies an application, which corresponds to a project under the wabapp directory. I will introduce the context container in detail in my next blog.

I am the fox fox. Welcome to my WeChat official account: wzm2zsd

qrcode_for_gh_83670e17bbd7_344-2021-09-04-10-55-16

This article is first released to WeChat official account, all rights reserved, no reprint!

Recommended Today

SQL statement of three-level linkage of provinces, cities and counties

The first is the table creation statement Copy codeThe code is as follows: CREATE TABLE `t_address_province` ( `id` INT AUTO_ Increment primary key comment ‘primary key’,`Code ` char (6) not null comment ‘province code’,`Name ` varchar (40) not null comment ‘province name’)Engine = InnoDB default charset = utf8 comment = ‘province information table’; CREATE TABLE […]