Servlet Lifecycle
The Servlet lifecycle can be defined as the entire process from creation to destruction. The following is the process a Servlet follows:
- After Servlet initialization, call theinit ()method.
- The Servlet calls theservice()method to handle client requests.
- Before Servlet destruction, call thedestroy()method.
- Finally, the Servlet is garbage collected by the JVM's garbage collector.
Now let's discuss the lifecycle methods in detail.
init() method
The init method is designed to be called only once. It is called when the Servlet is first created, and is not called again for each subsequent user request. Therefore, it is used for one-time initialization, just like the init method of Applet.
The Servlet is created when a user first calls the URL corresponding to the Servlet, but you can also specify that the Servlet is loaded when the server is first started.
When a user calls a Servlet, a Servlet instance is created, and each user request produces a new thread, which is handed over to the doGet or doPost method at the appropriate time. The init() method simply creates or loads some data that will be used for the entire lifecycle of the Servlet.
The definition of the init method is as follows:
public void init() throws ServletException {
// 初始化代码...
}
service() method
The service() method is the main method for performing actual tasks. The Servlet container (i.e., the Web server) calls the service() method to handle requests from clients (browsers) and writes the formatted response back to the clients.
Each time the server receives a Servlet request, the server creates a new thread and calls the service. The service() method checks the HTTP request type (GET, POST, PUT, DELETE, etc.) and calls methods such as doGet, doPost, doPut, doDelete at the appropriate time.
The following are the characteristics of this method:
public void service(ServletRequest request,
ServletResponse response)
throws ServletException, IOException{
}
The service() method is called by the container. The service method calls methods such as doGet, doPost, doPut, doDelete at appropriate times. Therefore, you don't need to do anything with the service() method; you only need to override doGet() or doPost() according to the request type from the client.
The doGet() and doPost() methods are the most commonly used methods in each service request. The following are the characteristics of these two methods.
doGet() method
GET requests come from a normal request to a URL, or from an HTML form that does not specify a METHOD, and are handled by the doGet() method.
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
// Servlet 代码
}
doPost() method
POST requests come from an HTML form that specifically specifies METHOD as POST, and are handled by the doPost() method.
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
// Servlet 代码
}
destroy() method
The destroy() method is called only once, at the end of the Servlet lifecycle. The destroy() method allows your Servlet to close database connections, stop background threads, write cookie lists or click counters to disk, and perform other similar cleanup activities.
After the destroy() method is called, the servlet object is marked for garbage collection. The destroy method is defined as shown below:
public void destroy() {
// 终止化代码...
}
Architecture Diagram
The following diagram shows a typical Servlet lifecycle scenario.
- The first HTTP request arriving at the server is delegated to the Servlet container.
- The Servlet container loads the Servlet before calling the service() method.
- The Servlet container then processes multiple requests generated by multiple threads, each thread executing the service() method of a single Servlet instance.