1,1, Web clients to the Servlet container (Tomcat) issued Http request
2, Servlet containers analysis of customer requests for information
3, Servlet containers to create a HttpRequest object, the client requests the information package to the object
4, Servlet containers to create a HttpResponse object
5, Servlet container calls the service method of HttpServlet object, the object HttpRequest and HttpResponse object as a parameter
HttpServlet object to
6, HttpServlet called the method of the HttpRequest object to obtain information Http request
7, HttpServlet called the method of the HttpResponse object, in response to data generated
8, Servlet containers HttpServlet response to the results to Web clients
More than to see this process, we will ask Servlet and HttpServlet container is what kind of agreement based on the interaction of?
HttpServlet how the life cycle of the object?
First of all, we come to understand the API object Servlet
Servlet framework is composed of two Java packages: javax.servlet and javax.servlet.http. Defined in the javax.servlet package of all
The Servlet Class or expansion must be to achieve a common interface and class. Defined in the javax.servlet.http package using Http protocol communications
Class HttpServlet. Servlet is the core of the framework javax.servlet.Servlet interface, all of the Servlet interface must realize this.
In the Servlet interface defines five methods,
Which represents the three methods of the Servlet life cycle:
1, init method: the object is responsible for Servlet initialization.
2, service method: in charge of responding to customer requests.
3, destroy method: When the object from the Servlet life cycle, the occupier is responsible for the release of resources.
Now we look at the following class diagram.
Javax.servlet.Servlet interface in a number of methods do they correspond to the way http requests. Combination of the following class diagram we have to
Describe the life cycle of the object HttpServlet
First, the time to create Servlet object
1, Servlet container startup: web.xml configuration file to read the information in the object structure specified Servlet, ServletConfig object to create,
ServletConfig object at the same time as a parameter to call the init method of Servlet object.
2, after the Servlet container: Servlet clients to request the first time, Servlet container will determine whether there is memory for the specified Servlet
As, if not then create it, and then created based on customer request HttpRequest, HttpResponse object, which calls Servlet
Service method of the object.
3, Servlet class files are updated, re-create Servlet
Servlet containers are automatically created at startup Servlet, it is in the web.xml file for Servlet decision attribute set <load-on-startup>
Of. From which we can see the same type of object in the Servlet containers Servlet in the form of single cases.
Second, the timing of the destruction of the target Servlet
1, Servlet containers to stop or restart: Servlet containers Servlet calls the destroy method to target the release of resources.
Mentioned above is the Servlet life cycle of the object. Servlet containers so How do I know which one to create Servlet object?
How to Configure Servlet object? In fact the information is read through the web.xml configuration file to be achieved.
We look at the Servlet in the web.xml file configuration section object information
-------------------------------------------
<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 following section of the configuration of the above information to resolve
servlet-name: Servlet object name
servlet-class: create Servlet object to call the class
param-name: parameter name
param-value: parameter value
load-on-startup: Servlet containers Servlet loaded at startup of the order of object
servlet-mapping/servlet-name: with the servlet in the servlet-name to configure the contents of the corresponding section
url-pattern: customers visit the relative URL path of Servlet
When the Servlet container to start the time to read section <servlet> configuration information, in accordance with section <servlet-class> configuration information to create Servlet object,
At the same time, in accordance with section <init-param> configuration information to create HttpServletConfig object, and then the implementation of the init method of Servlet object and, in accordance with
Festival <load-on-startup> configuration information to determine the order to create Servlet object, if the section of this configuration information is negative or not configured, then
Start in the Servlet container will not load the Servlet object.
When customers visit the Servlet container, Servlet containers according to the customer to visit the URL address, through the configuration <servlet-mapping> section <url-pattern>
Configuration section to find information Servlet designated object, and call the Servlet's service method of the object.
More than simply a Servlet to Tomcat with the principles of interaction between the object to a general summary, which is in the learning of Java Web applications
The need for a clear understanding of some of the content, if inappropriate, please point out, I will humbly accept. Since these reference孙卫琴prepared
<Tomcat与Java Web技术详解>
Article Source:







