Stages of Servlet Lifecycle:A servlet passes through the following stages in its life.
InitializeWhen the servlet is first created, it is in the initialization stage. The web server invokes the init() method of the servlet in this stage. It should be noted here that init() is only called once and is not called for each request. Since there is no constructor available in Servlet so this urges its use for one time initialization just as the init() method of applet
Initialize stage has the following characteristics and usage
- Executed once, when the servlet gets loaded for the first time
- Not called for each client request
- The above two points make it an ideal place to perform the startup tasks which are done in constructor in a normal class.
ServiceThe service() method is the engine of the servlet, which actually processes the client’s request. On every request from the client, the server spawns a new thread and calls the service() method as shown in the figure below. This makes it more efficient as compared to the technologies that se single thread to respond to requests.
The figure below show both version of the implementation of service cycle. In the upper part of diagram we assume that servlet is made by sub-classing from GenericServlet (Remember, GenericServlet is used for constructing protocol independent servlets.). To provide the desired functionality, service() method is overridden. The client sends a request to the web server, a new thread is created to serve this request followed by calling the service() method. Finally a response is prepared and sent back to the user according to the request.
The second part of the figure illustrates a situation in which servlet is made using HttpServlet class. Now, this servlet can only serves the HTTP type requests. In these servlets doGet() and doPost() are overridden to provide desired behaviors. When a request is sent to the web server, the web server after creating a thread, passes on this request to service() method. The Service() method checks the HTTP requests type (GET, POST) and calls the doGet() or doPost method depending on how the request is originally sent. After forming the response by doGet() or doPost() method, the response is sent back to service() method that is finally sent to the user by the web server.
DestroyThe web server may decide to remove a previously loaded servlet instance, perhaps because it is explicitly asked to do so by the server administrator, or perhaps serlet container shuts down or the servlet is idle for a long time, or may be server is overloaded. Before it does, however it calls the servlets destroy() method. This makes it a perfect spot for releasing the acquired resources.
The following figure can help to summarize the life cycle of the Servlet
The web sever creates a servlet instance. After successful creation, the servlet enters into initialization phase. Here, init() method is invoked for once. In case web server fails in previous two stages, the servlet instance is unloaded from the server. After initialization stage, the Servlet becomes available to serve the clients requests and to generate response accordingly. Finally, the servlet is destroyed and unloaded from web server.