servlet development standards, the servlet has its own small framework, a standard servlet program to have a complete lifecycle, what is the lifecycle? Let's take a look at the standard servlet development.
Servlet life cycle
Lifecycle is the cycle of a program's existence, and the servlet is managed by the container, so the container determines the lifecycle.
A servlet program is a Java program running on the server side whose lifecycle is controlled by a Web container that includes loading, initializing, servicing, destroying, uninstalling five parts
The lifecycle is as shown in the figure above, and it is a cycle of a program.
1. Load servlet
The Web container is responsible for loading the servlet, the container is Tomcat, and the container is responsible for creating the servlet instance when the Web container is started or when the servlet is used for the first time. However, the user must specify the location of the servlet (the package in which the servlet resides) through the deployment descriptor (Web.xml), and after the successful loading, the Web container instantiates the servlet in a reflective manner.
2. Initialize
When a servlet is instantiated, the container invokes the Init () method to initialize the object so that the Servlet object completes some initialization before processing the client request, and if the initialization fails, the servlet is downloaded directly
3. Processing services
When a request is submitted, the servlet invokes the Service () method (commonly used doget () and Dopost ()) for processing. In the service () method, the servlet can either accept the customer's request through ServletRequest or set the response information using Servletresponse
4. Destruction
When the Web container is closed or a servlet is detected to be removed from the container, the Destroy () method is automatically called to allow the instance to release the resource that it occupies