The hierarchy of classes related to servlet and the life cycle of HttpServlet

Source: Internet
Author: User
Tags abstract garbage collection http request
A: Hierarchy of classes related to Servlet <1> One interface, two classes of relationships:
<2> Details:1 Servlet (interface)
(1) Function Overview: Develop a specification. The original intention is to develop a specification that can be implemented by its implementation class to achieve a specific variety of transport protocols.
(2) 5 methods:
The most important methods for the first three and life cycle related.
void init (servletconfig config) 
void service (ServletRequest req, servletresponse res) 
void Destroy () 
String getservletinfo ()         //Gets information related to itself, such as version information
servletconfig getservletconfig ()//Get and start related configuration information


(3) The order of invocation of the life cycle method established in the interface:
1 construct the servlet and initialize it using the Init method.
2 handles all calls to the service method from the client.
3 Remove the servlet from the service, then use the Destroy method to destroy it, and finally garbage collection and terminate it.


2 Genericservlet (abstract class)
(1) Feature Overview: Implement a servlet interface that defines a generic, protocol-independent servlet.
Genericservlet makes it easier to write Servlets. It provides a simple version of the life cycle method Init and destroy,
and a simple version of the method in the ServletConfig interface. To write a generic servlet, simply rewrite the abstract service method.
(2) contains some important methods: (Note that only the service method is an abstract method)
void init () 
void init (servletconfig config)  
abstract  Void Service (ServletRequest req, servletresponse Res ) 
void Destroy () 
servletconfig getservletconfig ()  
string Getservletname ()  
string Getservletinfo ()  


3 HttpServlet (abstract class)
(1) Function Overview: Inherit Genericservlet. Of course, the abstract method of the parent class is implemented by the service method.

(2) Some important methods are included:
protected  void Doget (HttpServletRequest req, HttpServletResponse resp) 
protected  void DoPost ( HttpServletRequest req, HttpServletResponse resp)
protected  void Service (HttpServletRequest req, HttpServletResponse resp) public 
void  Service (servletrequest req, servletresponse Res) 

<3> Deep Understanding:There is no abstract method in the **httpservlet class for why it is defined as an abstract class.
Abstract class is to provide us with a generic template, HttpServlet is abstract, but its inside there is no abstract method,
That means he doesn't force us to do anything, we just have to choose, and rewrite some of the parent's methods.
This makes the code more concise and more reasonable. General rewrite Doget, or Dopost method.




**httpservlet Why there are two service methods, and what is the connection to the Doget,dopost method.
1) Two service methods are:
protected  void Service (HttpServletRequest req, HttpServletResponse resp) public 
void  
One is protected inherited permissions, and one is public permissions. There are also differences in the parameter types.
2) public void Service (ServletRequest req, servletresponse res);
This method is an implementation of the abstract Service () method in the parent class Genericservlet.
It converts Resquest,response objects into HttpServletRequest and HttpServletResponse objects, respectively.
and call the following overloaded service () method. i.e. protected void Service (HttpServletRequest req, HttpServletResponse resp)
Method.
3) protected void Service (Httpservirequest req,httpservletresponse resp);
This method uses the Servletrequest,servletresponse object as a parameter. and is
public void Service (ServletRequest req, servletresponse Res) method call,
HttpServlet implements this method and becomes the distributor of an HTTP request,
It proxies the request to Doget (), DoPost () ..... Doxxxx () method, so the service method should not be overloaded.
4) Use contact with two service methods and the Doxxx () method.
When a request is received, the Web container calls the public service method after the parameter is converted to Httpservirequest,httpservletresponse.
This public method calls the protected service method, which, according to the type of the HTTP request method, calls one of the doxxxx () methods of the protected service method.


* * Why is it common to override the Doget or Dopost method, not the service method, and what happens if the service method is replicated in the subclass.
1) from the above analysis know: By default, either a GET request or a POST request submitted, will be processed by the service () method.
The Doxxxx () method is then called by the protected Service method, depending on the request type. In other words, by default the service method is turned,
The Doxxxx () method is then processed logically (this is why subclasses rewrite the Doget or Dopost methods. ).
2) If we overwrite the service method in the HttpServlet subclass, then the service method is not used to turn,
Instead of dealing with the business, now whether your client is using POS or get to request this servlet, the service method is executed and the service method can only be executed.
Do not perform dopost or Doget methods. So it is generally overriding the Doget or Dopost method, which does not control the service method.
3) Extension: Do not rewrite the service method for the nuisance class. There is another essential reason: the service method itself implements the mechanism of cache negotiation, if we rewrite it,
Instead, the good mechanism was removed. In-depth discussion of this point see blog: Click to open the link

Second: The life cycle of HttpServlet

1. When a client requests a servlet for the first time, the Web server (such as Tomcat) finds the class of this servlet based on the configuration information of XML, creates the object for the servlet, and creates a ServletConfig object based on the configuration information. The Servlet object's init (ServletConfig) method is then called to initialize. 2. The server parses the request information, creates the HttpServletRequest object, encapsulates the request information into the HttpServletRequest object, and then creates an empty HttpServletResponse object.
3. The server invokes the service method of the Servlet object and passes the HttpServletRequest object and the HttpServletResponse object to the parameters of the service method.
4. The service method calls the appropriate method (Doget/dopost) According to the request mode (Get/post).

5. Call the service method to process the request at a later time when this request is received.

6. When the server is closed, remove the servlet from the service, then use the Destroy method to destroy it, and finally garbage collection and terminate it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.