A servlet is an application server located in the Java EE presentation layer.
servlet Understanding Chapter
1 First, the servlet is a program, followed by a program running on the server, and again, the process of handling client requests running on the server. The main function is to interactively browse and modify data to generate dynamic Web content.
2 The servlet program is composed of two Java packages, Javax.servlet, javax.servlet.http. The class and interface are defined in Javax.servlet, Javax.servlet.http defines the HttpServlet class with HTTP protocol communication
3 All servlet programs must implement the Servlet interface, the implementation process as shown in the following figure:
1,servlet instantiation to memory, invoking the Init method;
2, the client requests and invokes the Init method accordingly;
3, Free the memory, turn off the service.
servlet life cycle The servlet program execution process is a lifecycle that is the initialization phase, the running phase, the destruction stage.
1 The initialization phase servlet container creates a servlet instance and invokes the Init () method for initialization.
Initialization purpose: The Servlet object completes some initialization work before processing the client request, establishes the database connection, obtains the configuration information and so on. Details Note: The 1,servlet container is loaded and instantiated servlet;2, creating a Servlet object, the init () method is invoked only once, 3, the initialization phase throws a Servletexception exception, and an exception servlet is not executed.
2 The runtime servlet object accepts requests, creates ServletRequest and Servletresponse objects, and then invokes the service method.
Detail Note: Before the 1,service () method invocation, the init () method must be successfully executed to initialize successfully, 2, the ServletRequest object to obtain the client's request information, Servletresponse set the appropriate information.
3 The destruction phase the servlet container calls the Destroy () method, the container releases the servlet instance, and the garbage collection mechanism handles it. Detail NOTE: 1, when the servlet container terminates running, or when the new instance is reloaded, the Destroy () method is invoked. Summary in the servlet lifecycle, the initialization and destruction of the servlet occurs only once, that is, the init () method and the Destroy () method are executed once, and the number of times the servlet () method is executed depends on the number of times it is accessed. The whole process involves these three methods.