Reprint Please specify source: http://blog.csdn.net/linxdcn/article/details/73382570 1 Preface
Tomcat's source code structure is very clear, before reading the source code it is necessary to understand the entire Tomcat framework, and then into the various components of the source code. Overview of components and frameworks, and start, stop, and lifecycle management are described previously.
This is the final article in an overview of how Tomcat handles an HTTP request and what roles each component plays in the process. The following article will analyze the source code of each component in depth. 2 Connector
In Tomcat9, the protocols supported by Connector are HTTP and AJP, The protocol processing classes correspond to Org.apache.coyote.http11.Http11NioProtocol and Org.apache.coyote.http11.Http11AprProtocol respectively (the bio mode has been removed).
The connector consists of three modules: Http11nioprotocol, Mapper, and the process of Coyoteadapter,http request in connector is as follows:
The flow of HTTP requests in connector
Acceptor is a listener thread, calling serversocketaccept () blocking, essentially calling Serversocketchannel.accept () Acceptor adds the received socket to a poller in the Poller pool poller the socket into a socketprocessor socketprocessor call GetHandler () through the worker thread Get the corresponding Connectionhandler Connectionhandler the socket to http11processor processing, parse HTTP header and body Http11processor call service () The packaged request and response passed to Coyoteadapter Coyoteadapter will be sent through the mapper to the corresponding session, servlet and other related, ready to pass to container 3 Container
From the previous article know that there are 4 container, the use of the design model of the chain of responsibility, each container defined a pipeline, each pipeline also defined a number of valve, representing the task to be handled. Pipeline is like the logical bus for each container, loading each valve in the order of configuration on the pipeline. Through the pipeline to complete the call between the various valve, each valve implementation of specific application logic.
With the concept of a responsible chain design pattern, the HTTP request is forwarded from connector to container, and the process in container is clearer, as follows:
The flow of HTTP requests in container
The Coyoteadapter in the connector in the previous section invokes invoke () to pass the request and response to Container,container in turn to each valve, each of which functions as follows: The only valve in the Standardenginevalve:standardengine, which is primarily used to select the host container of its host mapping from the request Standardhost The first valve in the accesslogvalve:standardhost, used primarily for logging information after the execution of the pipe errorreportvalve:standardhost the valve immediately following the Accesslogvalve, Used primarily for pipeline execution, get exception information from the request object and encapsulate it in response to expose the problem to the last valve in the visitor standardhostvalve:standardhost, The context container that is used primarily to select its context map from request standardcontext and to access the session in the request to update the last access time of the conversation The only valve in the standardcontextvalve:standardcontext, the primary function is to prohibit any redirect access to resources in the Web-inf or Meta-inf directory, the implementation of the application thermal deployment function, Obtain the only valve in the Standardwrapper standardwrappervalve:standardwrapper from the request, The main functions include invoking the Loadservlet method of Standardwrapper to generate the servlet instance and calling Applicationfilterfactory to generate the filter chain
Finally, the response is returned to connector to complete the HTTP request. 4 References
Http://tomcat.apache.org/tomcat-9.0-doc/architecture/requestProcess/request-process.png
http://blog.csdn.net/beliefer/article/details/51894747
Reprint Please specify source: http://blog.csdn.net/linxdcn/article/details/73382570