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