How Tomcat works-6. tomcat processes requests, tomcatworks
Tomcat has been started. How does one process the request? How did we get to the servlet we wrote?
Directory
- Http11ConnectionHandler
- Http11Processor
- CoyoteAdapter
- StandardEngineValve
- StandardWrapperValve
- Summary
Http11ConnectionHandler
After tomcat is started, socket is used. after receiving the request, accept calls its processSocket to process the request. In this method, enable a SocketProcessor thread and call Http11ConnectionHandler in the internal class run method. the process is as follows:
The actual execution is the super class AbstractConnectionHandler. process method, mainly used:
- Obtain processor from connections
- If processor is null, an attempt is made to obtain a processor from the queue. This queue is a RecycledProcessors class inherited from concurrent1_queue. It is a thread-safe queue because multiple threads get processor at the same time.
- If processor is still null (indicating that it has not been created or used up), create a new processor and call Http11ConnectionHandler. createProcessor, this method will create a new Http11Processor (but it will not be immediately added to the queue mentioned above, but will be added to the queue after the request is processed)
- Call Http11Processor. process
Http11Processor
The processor thread will be started after connector is completed. The processor class diagram is as follows:
Different protocols have different processor. In AbstractProcessor, there are Request and Response, but org. apache. under the coyote package, this is the connector at the connector layer and is primitive.
The actual execution is the super class AbstractHttp11Processor. process. The main functions are as follows:
- Obtain the input and output streams of the socket.
- Parsing request header, method, requestURI
- Set request filters and content-length headers
- Call CoyoteAdapter. service
CoyoteAdapter
The connector ctor and processor transition class to the container. The adapter mode is used to adapt the container to the processor. The main method is CoyoteAdapter. service:
- Obtain org. apache. connector. Request and Response. After the two classes pass through the facade mode, they are the request and response used in the servlet.
- If the newly created processor, request and response are null, call connector. createRequest and createResponse to create a new one, and then set it to the note of coyote. request.
- Call postParseRequest to add the wring between wrapper and servlet (used later when loading servlet). parseSessionId parses sessionId
- Connector. getService (). getContainer (). getPipeline (). getFirst (). invoke (request, response), in sequence: Connector, StandardService, StandardEngine, StandardPipeline, StandardEngineValve
- After execution, the request is completed in this method: request. finishRequest (the processor mentioned above is recycled here), response. finishResponse (the request is returned to the client, outputStream)
- Finally, recycle request and response clears all request and response information.
StandardEngineValve
This is the basis Valve of StandardEngine (each container has a pipeline, and each pipeline has a base valve to call servlet). In the adapter, The StandardEngineValve is finally called. invoke method, which mainly performs the following operations
- Request. getHost: Get host
- Host. getPipeline (). getFirst (). invoke (request, response): StandardHost, StandardPipeline, StandardHostValve
Next, containers are called sequentially, and the next is StandardHostValve:
context.getPipeline().getFirst().invoke(request, response);
Next is NonloginAuthencator.
context.invokeNext(request,response)
Next is StandardContextValve
wrapper.getPipeline().getFirst().invoke(request, response);
The next step is StandardWrapperValve, which has done a lot of work here.
StandardWrapperValve
This is the basic Valve of StandardWrapper. It is used to obtain the servlet instance and call filterChain. The main method is invoke:
- Get the wrapper container and assign a servlet instance through wrapper
- Use ApplicationFilterFactory to create filterChain
- FilterChain. doFilter calls the filter chain. No additional filter is configured here. There is only one default WsFilter (supported by websocket). After all filterChain calls are completed, the servlet is called. service method, start to enter the servlet we wrote
- After the preceding call is complete, release the filterChain (leave the filter empty) and release the servlet (which will be in the instantPool)
The execution process is as follows. The last UserServlet is my custom simple servlet. After arriving at the custom servlet, the requests in turn are at the deepest layer, followed by layer-by-layer fan return and some cleanup work (of course, there are some persistent connection maintenance and so on ).
Summary
From receiving a socket listener to processing a custom servlet request, it is a complete request process. To illustrate the whole process of the white request, many details are omitted, such: three requests (coyote. request, connector. request, RequestFacade), session management, and so on.