The servlet life cycle is divided into three phases:
1, Initialize phase call init () method
2, call the service () method in response to the customer request phase
3, Terminate phase call Destroy () method
Servlet initialization phase:
The servlet container loads the servlet at the following times:
The 1,servlet container starts automatically when certain servlets are loaded to implement it only on the web. Add the following code between <Servlet></Servlet> in the XML file:
<loadon-startup> 1 </loadon-startup> |
2, after the servlet container is started, the customer first sends a request to the servlet
After the 3,servlet class file is updated, remount the servlet
After the servlet is loaded, the servlet container creates a servlet instance and invokes the servlet's init () method for initialization. The init () method is called only once throughout the servlet's life cycle.
Servlet response phase: When a servlet receives and responds to a customer request, the client first sends a request.
Service () is defined in the Javax.servlet.Servlet interface, which is implemented in Javax.servlet.GenericServlet. And Doget/dopost is realized in the Javax.servlet.http.HttpServlet, Javax.servlet.http.HttpServlet is Javax.servlet.GenericServlet Sub-class. All this is understood, in fact all requests are processed first by service (), and in the Javax.servlet.http.HttpServlet Service () method, the main thing to do is to determine whether the request type is Get or Post, and then call The corresponding Doget/dopost is executed.
The life cycle of the Java----servlet