This is the third day of my participation in the First Challenge 2022

How HTTP works

  • HTTP is a data transfer protocol between the browser and the server
  • As an application layer protocol, HTTP is based on the underlying TCP/IP protocol for data transmission. The transmitted data includes HTML files, images, query results, etc
  • HTTP protocol does not involve Packet transmission and mainly defines the communication format between browser and server
steps The user The browser The server
1 A user initiates a user request
2 The browser initiates a TCP connection request
3 The server accepts the request and establishes the connection
4 The browser generates the request packet in HTTP format
5 The browser sends a request packet
6 The server parses the request packet in HTTP format
7 The server executes the request
8 The server generates a response packet in HTTP format
9 The server sends a response packet
10 The browser parses the HTTP response packet
11 The browser renders the HTML response to the user
  • The user performs operations through the browser, such as typing a website and entering or clicking on a link, and the browser retrieves the event
  • The browser sends a TCP connection request to the server
  • The server accepts the connection request from the browser and establishes the connection through TCP three-way handshake
  • The browser packages the request data into a packet in HTTP format
  • The browser pushes the request packet onto the network, where it travels and eventually reaches the server
  • After receiving the request packet, the server parses the request packet in HTTP format to obtain the request from the browser
  • The server performs browser request processing, such as serving static files or calling server-side programs to get dynamic results
  • The server packages the results of the response, including HTML and images, in HTTP format
  • The server pushes the response packet onto the network, where it travels and eventually reaches the browser
  • After the browser receives the response packet, it parses the response packet in HTTP format to obtain the data
  • The browser presents the response data to the user on a page
  • HTTP server Tomcat and Jetty are used for:
    • Accept connections
    • Parsing request data
    • Handle the request
    • Send a response

Tomcat architecture

HTTP server request processing

  • Instead of calling the business class directly, the HTTP server hands the request to the container, which calls the business class through the Servlet interface
  • The appearance of Servlet interface and Servlet container achieves the purpose of decoupling HTTP server and business class
  • The specification for Servlet interfaces and Servlet containers is called the Servlet specification
  • Tomcat implements the Servlet container according to the Servlet specification and functions as an HTTP server
  • If you want to implement a new business, you just need to implement a Servlet interface and register it with the Tomcat server, the Servlet container, and Tomcat does the rest of the server operations

Servlet container workflow

  • When a user requests a resource,HTTP encapsulates the user’s request information with a ServletRequest object
  • The Servlet container then gets the request and finds the corresponding Servlet based on the mapping between URL and Servlet
  • If the Servlet has not already been loaded, it is created using reflection and initialized by calling the init() method of the Servlet
  • The Servlet’s Service method is then called to process the request
  • Finally, a ServletResponse object is used to encapsulate the request response information, which the HTTP server sends to the browser

Overall Architecture of Tomcat

  • TomcatThe server needs to implement two core functions:
    • Handles Socket connections and converts network byte streams to Request and Response objects
    • Load and manage servlets, and handle Request requests
  • TomcatDesign two core component connectorsConnectorAnd the containerContainerTo do these two things separately:
    • The Connector is responsible for external communication
    • Container Container is responsible for internal processing

The connector Coyote

  • CoyoteisTomcatThe connector frame, yesTomcatExternal interfaces provided for client access:
    • The client establishes a connection with the server through Coyote, sends a request and receives a response
  • CoyoteEncapsulates the underlying network communication, includingSocketRequest and response processing. forCatalinaContainers provide a unified interface to enableCatalinaContainer and specific request protocols as wellIOFully decoupled operation mode:
    • Coyote converts Socket input into a Request object, which is processed by Catalina
    • After processing,Catalina writes the result to the output stream via the Response object provided by Coyote
  • CoyoteAs an independent module, only responsible for specific protocols andIORelated operations, andServletIs not directly related to the specification implementation of:
    • The Request and Response objects do not implement interfaces corresponding to the Servlet specification
    • Instead, the Request and Response objects are further encapsulated in Catalina as ServletRequest and ServletResponse

IO model and network protocol

  • inCoyoteIn the,TomcatSupport for multipleI/OModel and application layer protocol:
    • TomcatTo support theIOModel: The default isNIO
      • NIO: Non-blocking I/O, implemented using the Java NIO class library
      • NIO2: Asynchronous I/O, using JDK 7 NIO2 class library implementation
      • APR: Implemented by Apache Portable runtime, is a native library written in C/C++. If you use APR as the I/O model, you need to install the APR library separately
    • TomcatSupported application layer protocols:
      • HTTP1.1: The access protocol used by most Web applications
      • AJP: For integration with Web servers such as Apache to optimize static resources and cluster deployment. Currently, AJP1.3 is supported
      • HTTP2: HTTP2 significantly improves Web performance
  • TomcatTo achieve support for multipleI/OModel and application layer protocols, a container may interconnect with multiple connectors:
    • Individual connectors or containers cannot provide services to the outside world. They must be assembled together to work. The whole assembly is called a Service component
    • A Service simply assembles the connector and container together
    • For flexibility, Tomcat is usually configured with multiple services so that different applications deployed on the same machine can be accessed using different port numbers

Connector assembly

  • Endpoint:
    • Coyote communication endpoint, or communication listening interface
    • Is the specific Socket request receiving and sending processor, is the transport layer abstraction, used to implement TCP/IP protocol
    • TomcatThere is nothing in theEndpointInterface, but defines an abstract classAbstractEndpoint,This abstract class defines two inner classes:
      • Acceptor
        • Used to listen for Socket connection requests
      • SocketProcessor
        • Used to process received Socket requests
        • Implement Runnable interface, in the Run method called the protocol processing component Processor for processing
        • To increase processing power, the socketProcessor is submitted for execution to the thread pool, which is the Tomcat extension’s native Java thread pool called Executor
  • Processor:
    • Coyote protocol processing interface is an abstraction of application layer protocol
    • Processor is used to implement HTTP
    • The Processor receives Socket data from the Endpoint, reads the byte stream, and resolves it into An HttpRequest object
    • The HttpRequest is then converted into a ServletRequest through the Adapter and submitted to the container for processing
  • ProtocolHandler:
    • Coyote protocol interface, which implements protocol-specific processing capabilities through the Endpoint and Processor
    • TomcatProvided in the6Five implementation classes:
      • AjpAprProtocol
      • AjpNioProtocol
      • AjpNio2Protocol
      • Http11AprProtocol
      • Http11NioProtocol
      • Http11Nio2Protocol
    • When configuring tomcat/conf/server.xml, specify a specific ProtocolHandler and also specify the protocol name. If APR is installed, Http11AprProtocol will be used, otherwise Http11NioProtocol will be used
  • Adapter:
    • The Request information transmitted by clients varies with protocols. Tomcat defines a Request class to store the Request information
    • The ProcotolHandler interface is responsible for parsing the Request and generating the Request class, but the Request is not a standard ServletRequest, so you cannot use Tomcat’s custom Request as a parameter to call the container for data processing
    • Using the adapter pattern, CoyoteAdapter is introduced to solve this problem: The connector calls the CoyoteAdapter’s Service method, passing in the Tomcat Request object, and the CoyoteAdapter takes care of transferring Tomcat The Request is converted to a ServletRequest, and finally the container’s Service method is called

Container Catalina

  • Tomcat is a Web container made up of a series of configurable components
  • Catalina is Tomcat’s servlet container
  • Catalina is a Servlet container implementation that integrates Coyote ina loosely coupled way to read data according to the request protocol
  • Hierarchy diagram of Tomcat module:

  • TomcatIt’s essentially oneServletContainer, soCatalinaisTomcatThe rest of the modules are forCatalinaProviding service support:
    • The connector Coyote provides connection communication
    • JSP engine Jasper provides the JSP engine
    • The expression language JavaEL provides support for EL expressions
    • Naming service Providing the JNDI service
    • Server log Juli provides the log service

Catalina structure

  • Catalina manages the Server, which stands for the entire Server
  • A Server has multiple services
  • Each Service Service contains a Coyote implementation of Connector components and a Container component
  • When Tomcat starts, an instance of Catalina is initialized
component role
Catalina Parse the Tomcat configuration file

Create the Server Server according to the configuration file, and manage the Server Server according to the command
Server Server Server represents the Catalina Servlet container and other related components

Server Server is responsible for assembling and starting the Servlet engine and Tomcat connector

Server By implementing the Lifecycle interface, Server provides an elegant way to start and shut down the entire system
Service A Service is an internal component of a Server Server. A Server Server can contain multiple Service services

The Service Service binds multiple Connector connectors to a Container
Connector The connector

Used to handle communication with clients

Receives the client request, forwards it to the appropriate container for processing, and returns the result of the response to the client
Container The container

Handles the user’s Servlet request and returns an object to the Web user module

The Container structure

  • TomcatIn the containerContainercontains4Types of components:
    • Engine
    • Host
    • Context
    • Wrapper
  • These four components are hierarchical parent-child relationships
  • Tomcat provides Servlet containers with greater flexibility through a layered architecture

Container components meaning
Engine Represents the entire Catalina Servlet engine

To manage multiple virtual sites, a Service can have at most one Engine, but an Engine can have multiple hosts
Host Represents a host or a virtual site

Tomcat can be configured with multiple virtual host addresses. One virtual host address can contain multiple contexts
Context Represents a Web application

A tail-end application can contain multiple wrappers
Wrapper Represents a Servlet container

Wrapper is the lowest level in a container and cannot contain child containers
  • Tomcat adopts a componentized design and components are configurable. Tomcat uses server. XML to configure containers
  • TomcatUse the composite pattern to manage these containersContainer,These containers have parent-child relationships and form a tree structure. The concrete implementation method is as follows:
    • All Container components implement the Container interface
    • The composite pattern enables consistent use of single container objects and composite container objects
      • Single-container object: The Wrapper at the bottom level
      • Composite container objects: refers to the parent containers Context,Host, and Engine

  • The Container interface inherits LifeCycle interface, which is used to centrally manage the LifeCycle of each component

Tomcat Startup Process

  • To start Tomcat, you need to start the startup script in the bin directory. In the startup script, the Catalina script is invoked
  • In the Catalina script, the main() method in Bootstrap is called
  • The init() method is called in the main() method of Bootstrap to create Catalina and initialize the classloader
  • The load() method is called in the main() method of Bootstrap and the Load () method of Catalina
  • In Catalina’s Load () method, initialization is required and a Digister object needs to be constructed to parse the XML
  • Create a Server in Catalina and call the init() method of the following components: Server, Service, Engine, Host, Context
  • The Executor component is then initialized by calling the init() method in the Service, the Connector is initialized by calling the Init () method in the Service, and the ProtocolHandler component’s init() method is called
  • After initialization, the start() method is called in the main() method of Bootstrap to start the subsequent components in turn
  • Call the start() method of Catalina, Server, Service, Engine, Host, and Context
  • The Start start() method is then called in the Service to start the Executor component, the Connector’s init() method is then called in the Service to initialize the Connector, and the ProcotolHandler component’s start() method is finally called
  • After the Tomcat process is started, the Tomcat configuration file is loaded, the container is initialized, and the port number is monitored to receive client requests

Lifecycle

  • Because all components of Tomcat have Lifecycle methods such as initialization, start and stop, a Lifecycle management interface is abstracted based on Lifecycle management when Tomcat is designed
  • Servercomponent,Servicecomponent,Containercomponent,ExecutorThe component andConnctorComponents implement a life cycleLifecycleInterface, thus having core methods in the lifecycle:
    • Init () : Initializes the component
    • Start () : starts the component
    • Stop () : Stops the component
    • Destory () : Destroys the component

Default implementation of the Tomcat component interface

  • Tomcat provides default implementations of the Server, Service, Engine, Host, and Context interfaces
  • forEndpointIn terms of components,TomcatThere is no corresponding inEndpointInterface, but with an abstract classAbstractEndpoint,Includes three implementation classes:
    • NioEndpoint – NIO
    • Nio2Endpoint – NIO2
    • AprEndpoint – APR
  • In Tomcat 8.5, NioEndpoint is implemented by default

  • ProtocolHandler:
    • Coyote protocol interface
    • Encapsulate the Endpoint and Processor to implement protocol-specific processing functions
    • TomcatIn accordance with the agreement andIOprovides6In theProtocolHandlerImplementation. The default protocol used isHttp11NioProtocol :
      • HTTPProtocol:
        • Http11NioProtocol – Nio. Tomcat default protocol
        • Http11Nio2Procotol – NIO2
        • Http11aprprocotol-apr. needs to rely on the APR library
      • AJPProtocol:
        • AjpNioProcotol – NIO
        • AjpNio2Procotol – NIO2
        • Ajpaprprocotol-apr. needs to rely on the APR library

conclusion

  • TomcatThe startup process is a standardized startup process:
    • Lifecycle is uniformly started according to the definition of the Lifecycle management interface
    • The init() method is called to initialize the component step by step
    • The start() method is then called to start it
  • In addition to completing its own processing flow, each level of Component in Tomcat is responsible for invoking child components in response to the lifecycle management approach
  • Components are loosely coupled to each other, making it easy to make configuration changes and replacements to Tomcat components through configuration files

Tomcat request processing process

Tomcat Request Process

  • The Mapper component class is used to complete the request processing process in Tomcat
  • MapperThe function of a component is to request it from the userURLLocate to aServlet,MapperHow components work:
    • MapperComponentWebApplication configuration information, that is, mapping between container components and access paths. This configuration information can be viewed as a multi-layerMap,Include:
      • Host Specifies the domain name configured in the Host container
      • The path of the Web application in the Context container
      • The path to the Servlet map in the Wrapper container
    • When there is a request, the Mapper component can locate a Servlet and request the execution method in the Servlet by resolving the domain name and path in the URL and looking for the corresponding path in the configuration information of the corresponding Web. A request URL can be located to only one Wrapper, a Servlet

  • The Acceptor in the Connector component Endpoint listens for client Socket connections and receives sockets. The connection is then passed to Executor processing to begin the request response task
  • The Processor reads the message and parses the request line, request body, and request header. Encapsulate as a Request object
  • The Mapper component processes requests based on the URL value of the request line and the Host value of the request header matching the Host container,Context container, and Wrapper container
  • The CoyoteAdapter component is responsible for associating the Connector component with the Engine container, passing the generated Reuqest object and Response object into the Engine container, and calling Pipeline
  • The Engine container pipe starts the process. The pipe contains several valves, each responsible for part of the processing logic. After the Valve is executed, the basic StandardEngineValve is executed, which calls the Host container Pipeline
  • The Pipeline of the Host container starts the process. The Pipeline contains several valves, each responsible for part of the processing logic. After executing the Valve, the basic StandardHostValve is executed, which calls the Pipeline of the Context container
  • The Pipeline of the Context container starts the process. The Pipeline contains several valves, each responsible for part of the processing logic. After executing the Valve, the basic StandardContextValve is executed, which calls the Pipeline of the Wrapper container
  • The Wrapper container pipe starts the process and contains several valves, each responsible for part of the processing logic. Once the Valve is executed, the basic StandardWrapperValve executes the Servlet object of the Wrapper container object

  • In the overall architecture of Tomcat, each component is loosely coupled to ensure the scalability and extensibility of the overall architecture
  • Each Container within Tomcat uses the chain of responsibility mode to handle specific requests to ensure flexibility and scalability
  • inTomcatDefined in thePipelineandVavleTwo interfaces:
    • Pipeline: Build responsibility chain
    • Valve: Represents each processor in the chain of responsibility
    • A basic Valve, StandardXxxValve, is maintained in Pipeline. Always at the end of the Pipeline and executed at the end, encapsulating the specific request processing and output response process
    • The addVavle() method can be called to add custom valves to the Pipeline, which precede the base Valve at the end and are executed in the order they are added
    • The Pipeline initiates the execution of the entire responsibility chain by acquiring the first Valve