A servlet is an application server located in the Java EE presentation layer.
The Servlet understands the article
1) First, the servlet is a program, followed by a program running on the server, and again, a program that processes client requests to run on the server. The main function is to interactively browse and modify data to generate dynamic Web content.
2) servlet program is composed of two Java packages, Javax.servlet, javax.servlet.http. Where classes and interfaces are defined in Javax.servlet, Javax.servlet.http defines the HttpServlet class that communicates with the HTTP protocol
3) All servlet programs must implement the Servlet interface, as shown in the following diagram:
1,servlet is instantiated into memory, calling the Init method;
2, the client requests and calls the Init method accordingly;
3. Release the memory and close the service.
servlet life cycle The servlet program execution process is a life cycle namely the initialization phase, the run phase, the destruction phase.
1) Initialize the servlet container to create the servlet instance and call the Init () method to initialize.
Initialization purpose: The Servlet object completes some initialization work before processing the client request, establishing a database connection, obtaining configuration information, and so on. Details Note: 1,servlet container complex loading and instantiation servlet;2, creating a Servlet object, the init () method is only called once, 3, the initialization phase throws a Servletexception exception, and the exception servlet is not executed.
2) The run -time Servlet object accepts the request, creates the ServletRequest and Servletresponse objects, and then invokes the service method.
Details Note: Before the 1,service () method call, the init () method must succeed, i.e. initialize successfully, and 2, through the ServletRequest object to obtain the client's related request information, Servletresponse set the corresponding information.
3) After the destruction phase of the servlet container calls the Destroy () method, the container releases the servlet instance, which is processed by the garbage collection mechanism. Detail NOTE: 1, call the Destroy () method when the Servlet container terminates, or reloads a new instance. 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 entire process involves these three methods.