How Tomcat handles HTTP requests

How Tomcat handles HTTP requests

Reprinted from: https://www.cnblogs.com/small-boy/p/8042860.html

1. What is Tomcat?

Tomcat is a web application server and also a Servlet/JSP container. As a servlet container, Tomcat is responsible for processing client requests, transmitting the request to the servlet, and returning the servlet's response to the client.

2. Tomcat's architecture

Tomcat is a component-based server, and its constituent components are all configurable. Its various components are configured in the ../conf/server.xml file in the Tomcat installation directory.

The source code of the server.xml file is as follows:

copy code
<?xml version="1.0" encoding="UTF-8"?>
<!--顶层类元素,可以包含多个Service-->
<Server port="8005" shutdown="SHUTDOWN">  
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
  <GlobalNamingResources>
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>
  <!-- Top-level class element, which can contain one Engine (container), multiple connectors --> 
  < Service name ="Catalina" > 
  <!-- Connector class element, representing the communication interface --> 
    < Connector port =" 8080" protocol ="HTTP/1.1" 
               connectionTimeout ="20000" 
               redirectPort ="8443"  /> 
    < Connector port ="8009" protocol ="AJP/1.3" redirectPort ="8443"  /> 
    <!-- Container class element, Handle client requests for a specific service component --> 
    <Engine name="Catalina" defaultHost="localhost" > 
        < Realm className ="org.apache.catalina.realm.UserDatabaseRealm" 
               resourceName ="UserDatabase" /> 
        </ Realm > 
        <!-- Container class element that handles client requests for a specific virtual host component-- > 
      < Host name ="localhost"   appBase ="webapps" 
            unpackWARs ="true" autoDeploy ="true" > 
        < Valve className ="org.apache.catalina.valves.AccessLogValve" directory ="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t "%r" %s %b" />
      </Host>
    </Engine>
  </Service>
</Server>
copy code

From the above source code, the architecture of Tomcat can be concluded as follows:

tomcat

As can be seen from the above figure, the heart of Tomcat is two core components: Connector and Container. One of the Containers can select multiple Connectors. These two components are explained in detail below.

3. Two core components of Tomcat: Connector and Container
1. Connector component

A Connector component will listen for client requests on a specified port, receive tcp connection requests sent by the browser, create a Request and a Response object to exchange data with your terminal, and then generate a thread to Process the request and pass the generated Request and Response objects to the Engine, get the response from the Engine and return it to the client. Tomcat has two classic Connectors, one directly listens for HTTP requests from browsers, and the other listens for requests from other WebServers. The Cotote HTTP/1.1 Connector listens on port 8080 for HTTP requests from client browsers, and the Coyote JK2 Connector listens on port 8009 for Servlet/JSP requests from other WebServers. The most important function of Connector is to receive connection requests and then assign threads to Containers to process the request, so this must be multi-threaded, and multi-threaded processing is the core of Connector's design.

2. Container component

The architecture of the Container component is as follows:

container

Container

Container is the parent interface of the container. The design of the container is based on the typical chain of responsibility design pattern. It consists of four self-container components, namely Engine, Host, Context, and Wrapper. These four components are responsible for the relationship, and there is a containment relationship. Usually, a Servlet class corresponds to a Wrapper. If there are multiple Servlets, define multiple Wrappers. If there are multiple Wrappers, define a higher Container, such as Context. Context is defined in the parent container Host, of which Host is not necessary, but to run the war program, you must have Host, because there must be a web.xml file in the war, and the parsing of this file requires Host, if there are multiple Hosts It's time to define a top container Engine. And Engine has no parent container, an Engine represents a complete Servlet engine.

Engine

The Engine container is relatively simple, it only defines some basic relationships. The Host container

Host

Host is the word container of Engine. A Host represents a virtual host in Engine. The function of this virtual host is to run multiple applications. It is responsible for installing and expanding these applications, and identifying this application to be able to distinguish them. Its sub-container is usually Context. In addition to the associated sub-container, it also stores the information that a host should have.

Context

Context represents the Context of the servlet, which has the basic environment for the servlet to run. In theory, as long as there is a Context, the servlet can run. Simple Tomcat can have no Engine and Host. The most important function of Context is to manage Servlet instances in it. Servlet instances appear as Wrappers in Context. Another point is how Context can find the correct Servlet to execute it? Tomcat5 used to be managed by a Mapper class. After Tomcat5, this function was moved to the request. In the previous sequence diagram, it can be found that the acquisition of sub-containers is allocated through the request.

Wrapper

Wrapper represents a servlet, which is responsible for managing a servlet, including servlet loading, initialization, execution, and resource recycling. Wrapper is the bottom-most container, it has no sub-containers, so calling its addChild will report an error. The implementation class of Wrapper is StandardWrapper. StandardWrapper also implements ServletConfig with a Servlet initialization information. It can be seen that StandardWrapper will directly deal with various information of Servlet.

Note: In addition to the above components, there are other important components in Tomcat, such as security component security, logger log component, session, mbeans, naming and other components. These components together provide the necessary services for Connector and Container.
4. The process of Tomcat processing an HTTP request

The process of Tomcat Server processing an HTTP request is shown in the following figure:tomcatserver

1. The user enters the URL localhost:8080/test/index.jsp in the browser, the request is sent to the local port 8080, and is obtained by the Coyote HTTP/1.1 Connector listening there;

2. The Connector hands the request to the Engine (Container) of the Service where it is located for processing, and waits for the Engine's response;

3.Engine obtains the request localhost/test/index.jsp, matching all virtual hosts Host;

4. The Engine matches the Host named localhost (even if it does not match, the request is handed over to the Host, because the Host is defined as the default host of the Engine), and the Host named localhost obtains the request /test/index.jsp , which matches all Contexts it owns. Host matches the Context with the path /test (if it does not match, the request will be handed over to the Context with the path name " " for processing);

5. The Context of path=“/test” obtains the request /index.jsp, and finds the corresponding Servlet in its mapping table. Context matches the Servlet whose URL pattern is *.jsp, corresponding to the JspServlet class;

6. Construct HttpServletRequest object and HttpServletResponse object, call doGet() or doPost() of JspServlet as parameters, execute business logic, data storage, etc.;

7. Context returns the HttpServletResponse object after execution to the Host;

8.Host returns the HttpServletResponse object to the Engine;

9.Engine returns the HttpServletResponse object to Connector;

10.Connector returns the HttpServletResponse object to the client Browser.


Reference article ( http://blog.csdn.net/sky_100/article/details/77541968 ).

Tags: Tomcat , Servlet

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325817312&siteId=291194637