In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article shares with you the content of a sample analysis of the HTTP request process in Tomcat. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Probe into the HTTP request process of Tomcat
Foreword:
1. As Java developers, most of them are familiar with Tomcat. Technical support and maintenance are provided by the Apache Foundation. Because it is free, open source and easy to use, it is popular in the market as a Web server, so it is necessary to conduct in-depth research on it. This series takes Tomcat 8.5 as the research topic, download address: https://tomcat.apache.org/download-80.cgi
two。 The following figure shows the directory of apache-tomcat-8.5.23.zip after being decompressed by windows.
Here are some key directories after decompression:
* / bin-batch files such as starting and stopping services. (* .sh) file (for Unix system), (* .bat) file (for Windows system) is a functional copy file. Since Win32 command-line began as a single, non-functional component, there are now some extensible features * / conf-configuration files and some related DTD files. The most important thing is server.xml. It is the main configuration file for this container. * / logs-the log file will be printed here * / webapps-this is where your application is deployed.
3. In essence, tomcat is a servlet container. First, take a look at the architecture of Tomcat, as shown below:
Architectural interpretation:
1.Server (server) is the top-level component of Tomcat, and everything is contained in Server. The implementation class StandardServer of Server can contain one or more Services,Service implementation classes that call the container (Container) interface for StandardService, but actually call Servlet Engine (engine), and the StandardService class also indicates the Server to which the Service belongs.
2.Container: engine (Engine), host (Host), Context (Context), and Wraper all inherit from the Container interface, so they are all containers. However, they have a parent-child relationship. In the three types of containers: Host, Context and Engine, the engine is the top-level container, which directly contains the host container, while the host container contains the context container, so the engine, host and context constitute a parent-child relationship in terms of size, although they all inherit from the Container interface.
3. Connector connects Service and Container. First, it needs to register with a Service. Its function is to forward requests from the client to the Container (container), which is why it is called a connector.
From a functional point of view, the Tomcat source code is divided into five sub-modules, which are:
Jsper module: this sub-module is responsible for parsing jsp pages, verifying jsp attributes, and dynamically converting jsp pages into java code and compiling them into class files. In Tomcat source code, all source code that belongs to the org.apache.jasper package and its subpackages belongs to this submodule
Servlet and Jsp modules: the source code of this submodule belongs to the javax.servlet package and its subpackages, such as the familiar javax.servlet.Servlet interface, javax.servet.http.HttpServlet class and javax.servlet.jsp.HttpJspPage in this submodule.
Catalina module: this submodule contains all java source code that starts with org.apache.catalina. The task of this sub-module is to standardize the overall architecture of Tomcat, define the key components such as Server, Service, Host, Connector, Context, Session and Cluster and their implementation. This sub-module makes extensive use of the Composite design pattern. At the same time, it also standardizes the execution process of events such as the start and stop of Catalina. From the perspective of code reading, this sub-module should be the focus of our reading and learning.
Connector module: if the above three submodules implement the Tomcat application server, then this sub-module is the implementation of the Web server. The so-called Connector is a bridge between the client and the application server. It receives the user's request and packages the user's request as a standard Http request (including protocol name, request header Head, request method Get or Post, etc.). At the same time, this sub-module is responsible for sending the response page to the client according to the standard Http protocol. For example, when the request page is not found, connector will send the standard Http 404 error response page to the client browser.
Resource module: this submodule contains some resource files, such as Server.xml and Web.xml configuration files. Strictly speaking, this submodule does not contain java source code, but it is also necessary for Tomcat compilation to run.
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.
From the structure of Server.xml, we can see the architecture / / top-level class elements of Tomcat, which can include multiple Service / / top-level class elements, an Engine and multiple Connecter / / connector class elements. Represents communication interface / / container class elements, handles customer requests for specific Service components, contains multiple Host / / container class elements, and handles customer requests for specific virtual host components Can contain multiple Context / / container class elements to handle all customer requests for a specific Web application
The actual source code is as follows
The architecture of Tomcat can be obtained from the above:
Figure 1: 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.
The process of processing an HTTP request by Tomcat Server
Figure 3: the process of Tomcat Server processing an HTTP request
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.
Thank you for reading! This is the end of the article on "sample Analysis of HTTP request process in Tomcat". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.