Servlet life cycle

Source: Internet
Author: User

a lifecycle is the process by which a container creates a servlet instance, allocates resources, calls its methods, destroys an instance
Phase One: Instantiate (that is, create a Servlet object, call the constructor)
Object instantiation occurs in the following two scenarios.
First case:
When the request arrives at the container, the container finds whether the Servlet object exists and if it does not, the instance is created.
second case:
When the container is started, or when an application is newly deployed, it checks whether the servlet has a Load-on-starup configuration in Web. Xml. If so, the servlet instance is created.
the lower the Load-on-starup parameter value, the higher the priority (the minimum value is 0, the highest priority).
 
Experiment 1: Set up the constructor to observe multiple accesses and service method execution
Lab 2: Add Load-on-starup to the configuration file to observe the timing of the instantiation

Phase two: Initialize
assigning resources to the servlet, calling init (servletconfig config);
the method config object can be used to access the initialization parameters of the servlet.
initialization parameters are parameters that are configured with Init-param.
Init can override.
The experiment 1:override the Init method and observed the actual (and the upper experimental mates)
Experiment 2: Add <init-param>, and pass the value config
<init-param>
<param-name>name</param-name>
<param-value>zhangsan</param-value>
</init-param>



ServletConfig Config=getservletconfig ();
String add=config.getinitparameter ("address");
、 ---------------------------------------------
If it's a ginseng init, then don't get servletconfig.
Name=config.getinitparameter ("name");

Phase three: Ready/Call
There is a request to reach the container, and the container invokes the Servlet object's service () method.
The service () method of HttpServlet calls the Doget () or DoPost () method according to the requested method.
However, these two do methods, by default, throw an exception and require subclasses to override.

Phase IV: Destruction
The container removes the Servlet objects that are no longer needed, based on its own algorithm.
The Destroy () method of the Servlet object is called before it is deleted.
The Destroy () method is used to release resources.
Throughout the servlet's lifecycle, Init,destroy executes only once, and the service method executes multiple times.

Servlet life cycle

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.