Content output Source:Pull education Java high salary boot camp

1. Process for the browser to access the server

The browser uses Http to access the server. Http is an application-layer protocol that defines the format of data communication. Specific data transmission uses TCP/IP.

2. Overall Tomcat system architecture

Tomcat is an Http server (it can accept and process Http requests, so Tomcat is an Http server).

2.1. General process of Tomcat processing requests

The HTTP server receives the request and passes it to the Servlet container for processing, which invokes the business class through the Servlet interface.

The whole set of Servlet interfaces and Servlet containers is called the Servlet specification.

The Servlet container acts as a decouple, avoiding direct coupling between the Http server and the business class.

Because Tomcat implements the Servlet container according to the Servlet specification and functions as an Http server, Tomcat has two important identities:

  • The Servlet container
  • The Http server

2.2. Tomcat Servlet Container Processing process

When a user requests a URL resource:

    1. The Http server encapsulates the Request information into a Request object, which is then converted into a ServletRequest object and passed into the Servlet container.
    1. The Servlet containerUpon receipt of the request, according toURLandServletTo find the mapping of theServlet;
    1. ifServletNot loaded yet, useReflection technologyTo create thisServletAnd callServlettheinitMethod to complete initialization;
    1. callServlettheserviceMethod to process the request and encapsulate the result of the request processing intoServletResponseObject, returned toThe Http server;
    1. The Http server converts the received ServletResponse object into a Response object and returns it to the client.

2.3. Overall Architecture of Tomcat system

Tomcat designs two core components, Connector and Container, to fulfill the two core functions of Tomcat:

  • Connector: responsible for external communication, processing Socket connection, responsible for network byte stream and Request/Response object conversion;
  • Container: Responsible for internal processing, loading and managing servlets, and handling specific requests.

3. Tomcat connector Coyote

3.1 introduction of Coyote

Coyote is the name of the Tomcat connector component and is the external interface. Through Coyote, the client establishes a connection with the server, sends a request, and receives a response.

Coyote is responsible for specific protocols (application layer) and IO (transport layer) :

  • Coyote encapsulates the underlying network communication (Socket request and response processing)
  • Coyote completely decouples Catalina containers (container components) from specific request protocols and IO operations
  • Coyote converts the Socket input into a Request object, which is then sent to Catalina for processing. After processing, Catalina writes the result into the output stream through the Response object provided by Coyote

**Tomcat (Coyote) ** Supports multiple application layer protocols and I/O models:

Before version 8.0, the default Tomcat I/O model was BIO. After version 8.0, it was NIO.

3.2. Coyote internal components and processes

Functions of Coyote internal components:

component role
EndPoint EndPointisCoyoteThe communication endpoint, that is, the interface on which the communication listens, is concreteSocketThe processor that receives and sends, rightTransport layer abstraction. soEndPointUsed to implementTCP/IP protocol
Processor ProcessorisCoyoteProtocol processing interface, used to implementThe HTTP protocol.ProcessorReceive fromEndPointtheSocket, read byte stream resolution intoTomcat RequestandThe Response object, and through theAdapterSubmit it to the container for processing,ProcessorIs an abstraction of application-layer protocols
ProtocolHandler CoyoteProtocol interface, passEndpointandProcessorTo achieve protocol-specific processing capability.TomcatSix implementation classes are provided by protocol and I/O:AjpNioProtocol.AjpAprProtocol.AjpNio2Protocol.Http11NioProtocol.Http11Nio2Protocol.Http11AprProtocol
Adapter CoyoteAdapterResponsible for theTomcat RequestintoServletRequest, and then call the container

4. Tomcat Servlet container Catalina

4.1. Hierarchical structure diagram of Tomcat modules and the status of Catalina

Tomcat is a Web container made up of a series of configurable (conf/server.xml) components, and Catalina is Tomcat’s Servlet container.

Tomcat is essentially a Servlet container because Catalina is at the heart of Tomcat and all other modules support Catalina. For example, Coyote module provides link communication, Jasper module provides JSP engine, Naming provides JNDI service, and Juli provides logging service.

4.2. Structure of the Servlet container Catalina

We tend to think that Tomcat is an example of Catalina, because Catalina is at the heart of Tomcat.

Tomcat/Catalina instances:

When Tomcat starts, it initializes the instance. The Catalina instance creates other instances by adding server. XML, creating and managing a server. A Server creates and manages multiple services, each of which can have multiple Connectors and a Container.

  • Catalina

    Responsible for parsing the Tomcat configuration file (server.xml) to create and manage server components

  • Server

    Responsible for assembling and starting Servlaet engine,Tomcat connector. Server provides an elegant way to start and shut down the entire system by implementing the Lifecycle interface

  • Service

    A Service is an internal component of a Server. A Server contains multiple services. It binds several Connector components to a Container

  • Container

    Container, the module that handles the user’s servlet requests and returns objects to the Web user

4.3. Structure of Container Components

There are several specific components under the Container component: Engine, Host, Context, and Wrapper. These four components (containers) are parent-child relationships. Tomcat provides the Servlet container with great flexibility through a layered architecture.

  • Engine

    The Servlet Engine of Catalina is used to manage multiple virtual sites. A Service can have at most one Engine, but an Engine can have multiple hosts

  • Host

    A virtual host, or site, can be configured with multiple virtual host addresses for Tomcat, and a virtual host can contain multiple contexts

  • Context

    Represents a Web application, and a Web application can contain multiple wrappers

  • Wrapper

    Represents a Servlet. Wrapper is the bottom layer of the container and cannot contain child containers