In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how Tomcat handles Http requests". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Servlet technology is what we java back-end engineers must master. Here, we can roughly summarize the java web technology route into the following process:
Because tomcat implements the Servlet specification, we need to know what Servlet is. What is the Servlet specification?
What is Servlet?
Servlet is a kind of JavaEE specification, mainly to expand the function of Java as a Web service and unify the interface. The functions of web are implemented by other internal vendors such as tomcat,jetty. For example, a http request arrives: the container encapsulates the request as a HttpServletRequest object in servlet, calls init (), service () and other methods to output response, and the container wraps it as a process returned to the client by httpresponse.
What is the Servlet specification?
In terms of the Jar package, the Servlet specification is two Jar files. Servlet-api.jar and jsp-api.jar,Jsp are also a kind of Servlet.
In terms of package, it is the javax.servlet and javax.servlet.http packages.
In terms of interface, it standardizes Servlet interface, Filter interface, Listener interface, ServletRequest interface, ServletResponse interface and so on. The class diagram is as follows:
Why do we call tomcat a Web container or a Servlet container?
We use a picture to show the relationship between them:
Simple understanding: start a ServerSocket and listen on port 8080. The Servlet container is used to hold the Servlet we developed.
Introduction to tomcat Architecture
Tomcat architecture diagram
The architecture diagram is compared with the content in the server.xml under conf in tomcat:
Comparing the architecture diagram with the server.xml content, server.xml is the xml version of the architecture diagram, so we can guess that there should be corresponding classes in our java code.
For example: Listener class, Service class, host class, Engine class, etc., which will be analyzed in detail later, here is just a guess about the implementation in our java code.
For those of you who have read the previous Mybatis source code analysis article, you should also be able to guess how the server.xml configuration file is parsed and how to store the configuration information.
When tomcat starts, it loads each corresponding component by reading the parameters of the server.xml configuration file. At the same time, the relevant controllable parameters of tomcat are configured in this file. Most of the optimization work of tomcat in the actual project is the parameter adjustment in this configuration file.
Introduction of tomcat components
Server
With regard to the relationship between server and tomcat, it can be understood that starting a tomcat is starting a server.
As the outermost core component of Tomcat, the main functions of Server components are as follows.
Provides a listener mechanism for handling different events throughout the Tomcat lifecycle
Provides a global named resource implementation for the Tomcat container
Listen on a port to receive SHUTDOWN commands
Service
A Service represents a collection of one or more Connector that share the same Container to process their requests. You can have any number of Tomcat instances within the same Service instance, which are independent of each other.
ConnectorConnector is used to accept requests and encapsulate them into Request and Response, which are then handed over to Container for processing. After Container processing, they are returned to Connector to the client.
Container
Container is used to encapsulate and manage Servlet and specifically handle Request requests, including four request processing components: engine (engine), virtual host and context (context) components. Container is the parent interface of the container, which is used to encapsulate and manage Servlet and specifically handle Request requests. The container is designed with a typical responsibility chain design pattern, which is composed 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. Contains only one engine.
Engine
Represents the entire Servlet engine. In Tomcat, Engine is the highest-level container object. Although Engine is not a container for processing requests directly, it is an entrance to get the target container. The engine represents a runnable servlet engine instance of Catalina and contains the core functionality of the servlet container. There can be only one engine in a service. At the same time, as a true container, the Engine element can contain one or more virtual host Host.
Host
Represents a site, which can also be called a virtual host, and can be added by configuring Host. Host container is a sub-container of Engine container. As mentioned above, Host is managed by Engine container, which refers to a virtual host. For example, when we visit a specific jsp page, URL is a virtual host, and its function is to run multiple applications and manage these applications. The sub-container is Context, and a host also stores the relevant information of the host.
Context
Context, as a kind of container, is used to represent Servletcontext. In Servlet specification, a Servletcontext represents an independent Web application. Represents an application, corresponding to a set of programs normally developed, or the WEB-INF directory and the following web.xml file.
WapperWapper is used as a kind of container to represent the Servlet defined in the Web application, and each Wrapper encapsulates this Servlet.
Component relationship
Two core components of tomcat
Connector: mainly responsible for handling Socket connections and the conversion of Request to Response.
Container: including Engine, Host, Context and Wrapper, mainly responsible for internal processing and Servlet management
Tomcat processing Http request flow
Now that we've finished talking about the overall architecture of tomcat, let's talk about it. Let's say we type it on the browser.
Http://localhost:8080/my-web-mave/index.jsp
How this request flow is handled in tomcat:
Our request is sent to native port 8080 and is obtained by the Coyote HTTP/1.1 Connector listening there.
Connector hands the request to the Engine of the Service where it is located and waits for a response from Engine.
Engine gets the request localhost/my-web-maven/index.jsp, which matches all the virtual host Host it owns, and our virtual host is configured with localhost by default in server.xml.
The Engine matches the Host of the name=localhost (even if it doesn't match, the request is handed over to the Host for processing, because the Host is defined as the default host for the Engine).
Localhost Host gets the request / my-web-maven/index.jsp, which matches all the Context it owns.
The Host matches to the Context with the path / my-web-maven (if there is no match, the request is passed to the Context with the path name "" to be processed).
The Context of path= "/ my-web-maven" gets the request / index.jsp and looks for the corresponding servlet in its mapping table.
Context matches to the servlet whose URL PATTERN is * .jsp, corresponding to the JspServlet class.
Construct the HttpServletRequest object and the HttpServletResponse object, and call the doGet or doPost method of JspServlet as parameters.
Context returns the HttpServletResponse object after execution to Host.
Host returns the HttpServletResponse object to Engine.
Engine returns the HttpServletResponse object to Connector.
Connector returns the HttpServletResponse object to the customer browser. Flow chart: a little blurry.
That's all for "how Tomcat handles Http requests". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.