Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to parse the internal structure and request process of Tomcat

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

How to parse the internal structure and request process of Tomcat, aiming at this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

Overview

Tomcat is a JSP/Servlet container. As a Servlet container, it has three working modes: independent Servlet container, in-process Servlet container and out-of-process Servlet container.

The organizational structure of Tomcat

Tomcat is a component-based server, its components are configurable, of which the outermost is the Catalina servlet container, and other components are configured in this top-level container according to certain format requirements.

The various components of Tomcat are configured in the / conf/server.xml file under the Tomcat installation directory.

Looking at the Architecture of Tomcat from the structure of Server.xml

The architecture of Tomcat can be obtained from the above:

The architecture of Tomcat

As can be seen from the image above, the heart of Tomca consists of two components: Connecter and Container. A Container can select multiple Connecter, and multiple Connector and a Container form a Service. Service can provide services, while the Server server controls the entire lifecycle of Tomcat.

Lifeline "Lifecycle" of the component

Service and Server manage the life cycle of the components below it.

The life cycle of components in Tomcat is controlled through the Lifecycle interface, as long as the component inherits this interface and implements the methods in it, it can be uniformly controlled by the component that owns it, so that the life cycle of all components in Tomcat can be controlled by a top-level component, the highest component is Server, and the one that controls Server is Startup, that is, you start and shut down Tomcat.

Two major components of Tomca: Connecter and Container

Connecter component

A Connecter will listen for customer requests on a specified port, receive tcp connection requests from the browser, create a Request and Response objects to exchange data with the requester, and then generate a thread to process the request and pass the resulting Request and Response objects to the processing Engine (part of the Container), get the response from the Engine and return to the customer.

There are two classic Connector in Tomcat, one listening directly for HTTP requests from Browser and the other from other WebServer requests. Cotote HTTP/1.1 Connector listens for HTTP requests from customer Browser at port 8080, and Coyote JK2 Connector listens for Servlet/JSP requests from other Web Server at port 8009.

The most important function of Connector is to receive a connection request and assign a thread to Container to process the request, so it must be multithreaded, and multithreaded processing is the core of Connector design.

Container component

The architecture of Container is as follows:

The architecture of Container

Container is the parent interface of the container, which is designed with a typical responsibility chain 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 an inclusion relationship. Usually a Servlet class corresponds to a Wrapper. If there are multiple Servlet to define multiple Wrapper, if there is more than one Wrapper, define a higher Container, such as Context.

Context can also be defined in the parent container Host, Host is not required, but to run war programs, you must have Host, because there must be a web.xml file in war, and the parsing of this file requires Host. If you want to have multiple Host, you need to define a top container Engine. While Engine has no parent container, an Engine represents a complete Servlet engine.

Engine container

The Engine container is relatively simple. It only defines some basic relationships.

Host container

Host is the word container of Engine, and 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 deploying these applications, and identifying the applications so that they can be distinguished. Its child container is usually Context, which not only associates the sub-container, but also stores the information that a host should have.

Context container

Context stands for Servlet's Context, which has the basic environment in which Servlet runs. In theory, as long as you have Context, you can run Servlet. A simple Tomcat can be without Engine and Host. The most important function of Context is to manage the Servlet instances in it. Servlet instances appear as Wrapper in Context, and how can Context find the right Servlet to execute it? Tomcat5 was previously managed by a Mapper class. After Tomcat5, this function has been moved to request. In the previous timing diagram, you can find that the acquisition child containers are allocated through request.

Wrapper container

Wrapper represents a Servlet, which is responsible for managing a Servlet, including Servlet loading, initialization, execution, and resource recovery. Wrapper is the lowest-level container, and it has no child containers, so calling its addChild will result in an error.

The implementation class of Wrapper is that StandardWrapper,StandardWrapper also implements a ServletConfig with Servlet initialization information, which shows that StandardWrapper will directly deal with all kinds of information of Servlet.

Other components in Tomcat

Tomcat also has other important components, such as security components security, logger logging components, session, mbeans, naming and other components. Together, these components provide the necessary services for Connector and Container.

The process of processing an HTTP request by Tomcat Server

The process of processing an HTTP request by Tomcat Server

The process of processing an HTTP request by Tomcat Server

1. The user clicks on the content of the web page, and the request is sent to local port 8080, which is obtained by the Coyote HTTP/1.1 Connector listening there.

2. Connector hands the request to the Engine of the Service where it is located and waits for a response from Engine.

3. Engine gets the request localhost/test/index.jsp, which matches all virtual host Host.

4. The Engine matches to the Host named localhost (even if it fails to match, the request is handed over to the Host for processing, because the Host is defined as the default host of the Engine), and the Host named localhost gets the request / test/index.jsp, matching all the Context it owns. The Host matches to the Context with the path / test (if there is no match, the request is passed to the Context with the path name "" to be processed).

5. The Context of path= "/ test" gets the request / index.jsp and finds the corresponding Servlet in its mapping table. Context matches to the Servlet whose URL PATTERN is * .jsp, corresponding to the JspServlet class.

6. Construct HttpServletRequest object and HttpServletResponse object, and call doGet () or doPost () of JspServlet as parameters. Execute business logic, data storage and other programs.

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

8. Host returns the HttpServletResponse object to Engine.

9. Engine returns the HttpServletResponse object to Connector.

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

The answer to the question about how to analyze the internal structure of Tomcat and the request process is shared here. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report