Tomcat is a Web application server and a Servlet/JSP Container. as a servlet container, Tomcat is responsible for processing customer requests, sending requests to the servlet, and sending the servlet response back to the customer. servlet is a component running on a server that supports the Java language. the most common purpose of servlet is to extend the Java Web server function and provide a safe, portable, and easy-to-use CGI alternative. next we will describe how Tomcat and Servlet work. First, let's look at the sequence diagram below.
1. 1. The Web Client sends an HTTP request to the servlet container (Tomcat)
2. The servlet container analyzes the customer's request information
3. The servlet container creates an httprequest object to encapsulate the information requested by the customer into this object.
4. Create an httpresponse object in the servlet container
5. The servlet container calls the service method of the httpservlet object and takes the httprequest object and httpresponse object as parameters.
Send to httpservlet object
6. httpservlet calls the relevant methods of the httprequest object to obtain the HTTP Request Information
7. httpservlet calls the relevant methods of the httpresponse object to generate response data
8. The servlet container sends the HTTP servlet response result to the Web Client.
When we see the above process, we will ask what conventions the servlet container interacts with httpservlet based on?
What is the lifecycle of an httpservlet object?
First, let's take a look at the servlet object API
The servlet framework consists of two Java packages: javax. servlet and javax. servlet. HTTP. Define all
All Servlet classes must implement or extend the common interfaces and classes. The javax. servlet. http package defines
Httpservlet class. The core of the servlet framework is the javax. servlet. servlet interface, which must be implemented by all servlets.
Five methods are defined in the servlet interface,
The three methods represent the servlet lifecycle:
1. init method: initializes the servlet object.
2. service method: responds to customer requests.
3. Destroy method: When the servlet Object exits the lifecycle, it is responsible for releasing the occupied resources.
Next let's look at the class diagram below.
There are some do methods in the javax. servlet. servlet interface, which correspond to the HTTP request method. Next we will combine the class diagram
Describes the lifecycle of an httpservlet object.
I. Creation of servlet objects
1. When the servlet container is started: Read the information in the web. xml configuration file, construct the specified servlet object, and create the servletconfig object,
At the same time, the servletconfig object is used as a parameter to call the init method of the servlet object.
2. After the servlet container is started: when the client sends a request to the servlet for the first time, the servlet container determines whether a specified servlet pair exists in the memory.
If no, create it, and then create the httprequest and httpresponse objects according to the customer's request to call the Servlet
Object Service method.
3. After the servlet class file is updated, re-create the Servlet
The servlet container automatically creates the servlet at startup. This is determined by the <load-on-startup> attribute set for the servlet in the web. xml file.
. We can also see that the servlet object of the same type exists as a singleton In the servlet container.
Ii. Time to destroy servlet objects
1. Stop or restart the servlet container: The servlet container calls the destroy method of the servlet object to release the resource.
The above is the lifecycle of the servlet object. How does the servlet container know which servlet object to create?
How to configure servlet objects? In fact, this information is implemented by reading the Web. xml configuration file.
Let's take a look at the configuration section of the servlet object in the web. xml file.
-------------------------------------------
<Servlet>
<Servlet-Name> action <servlet-Name>
<Servlet-class> org. Apache. Struts. Action. actionservlet </servlet-class>
<Init-param>
<Param-Name> config </param-Name>
<Param-value>/WEB-INF/struts-config.xml </param-value>
</Init-param>
<Init-param>
<Param-Name> detail </param-Name>
<Param-value> 2 </param-value>
</Init-param>
<Init-param>
<Param-Name> debug </param-Name>
<Param-value> 2 </param-value>
</Init-param>
<Load-on-startup> 2 </load-on-startup>
</Servlet>
<Servlet-mapping>
<Servlet-Name> action </servlet-Name>
<URL-pattern> *. DO </url-pattern>
</Servlet-mapping>
--------------------------------------------
The configuration section information above is parsed below
Servlet-Name: name of the servlet object
Servlet-class: class to be called to create a servlet object
Param-Name: Parameter Name
Param-value: Parameter Value
Load-on-startup: servlet object loading sequence when the servlet container starts
Servlet-mapping/servlet-Name: corresponds to the servlet-name configuration section in the servlet.
URL-pattern: the relative URL of the servlet accessed by the customer
When the servlet container starts, read the <servlet> Configuration section information and create the servlet object according to the <servlet-class> Configuration section information,
Create an httpservletconfig object according to the <init-param> Configuration section, and then execute the init method of the servlet object.
<Load-on-startup> Configure the section information to determine the sequence of creating servlet objects. If the configuration section information is negative or not configured
This servlet object is not loaded when the servlet container is started.
When the customer accesses the servlet container, the servlet container uses the <URL-pattern>
In the configuration section, locate the specified servlet object and call the service method of the servlet object.
The above content is a general summary of the Interaction principle between Tomcat and Servlet objects. This is what we are learning about Java Web application technology.
Some content that needs to be clearly understood. If there is any improper content, please point out that I will accept it modestly. These content can be found in
<Tomcat and Java Web technical details>