Servlet Lifecycle
The Servlet lifecycle can be defined as the entire process from creation until destruction. The following is the process a Servlet follows:
- The Servlet is initialized, then init () method.
- The Servlet calls service() method to handle the client's request.
- The Servlet is destroyed, then destroy() method.
- Finally, the Servlet is garbage-collected by the JVM's garbage collector.
Now let's discuss the lifecycle methods in detail.
The 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 on subsequent user requests. Therefore, it is used for one-time initialization, just like the Applet's init method.
A Servlet is created when the user first invokes the URL corresponding to that Servlet, but you can also specify that the Servlet be loaded when the server first starts.
When a user invokes a Servlet, a Servlet instance is created. Each user request produces a new thread, which is handed off to the doGet or doPost method as appropriate. The init() method simply creates or loads some data that will be used throughout the Servlet's lifecycle.
The init method is defined as follows:
public void init() throws ServletException {
// Initialize code...
}
The service() Method
The service() method is the main method that performs the actual task. The Servlet container (i.e. the Web server) calls the service() method to handle requests from the client (browser) and writes the formatted response back to the client.
Each time the server receives a Servlet request, it produces a new thread and invokes the service. The service() method checks the HTTP request type (GET, POST, PUT, DELETE, etc.) and calls doGet, doPost, doPut, doDelete, and other methods as appropriate.
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, and the service method calls doGet, doPost, doPut, doDelete, and other methods as appropriate. So 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.
The doGet() Method
A GET request comes from a normal request for a URL, or from an HTML form that does not specify a METHOD; it is handled by the doGet() method.
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
// Servlet code
}
The doPost() Method
A POST request comes from an HTML form that specifically specifies METHOD as POST; it is handled by the doPost() method.
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
// Servlet code
}
The destroy() Method
The destroy() method is called only once, at the end of the Servlet's lifecycle. The destroy() method lets your Servlet close database connections, stop background threads, write the cookie list or hit counter 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 follows:
public void destroy() {
// Termination code...
}
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 handles multiple requests generated by multiple threads, each thread executing the service() method of a single Servlet instance.