Servlets Life Cycle

A servlet life cycle can be characterized as the whole procedure from its creation till the destruction. Coming up next are the ways followed by a servlet.

  • The servlet is introduced by calling the init() method.

  • The servlet calls service() method to process a client's request.

  • The servlet is ended by calling the destroy() method.

  • Finally, servlet is garbage collected by the garbage collector of the JVM.

Now let us examine the life cycle methods in detail.

The init() Method

The init method is called just once. It is called just when the servlet is made, and not required any client asks for a short time later. Along these lines, it is utilized for one-time initializations, similarly as with the init method for applets.

The servlet is typically made when a client initially summons a URL relating to the servlet, however you can likewise determine that the servlet be stacked when the server is first started.

When a client conjures a servlet, a solitary occurrence of each servlet gets made, with every client request for bringing about another string that is given off to doGet or doPost as fitting. The init() method basically makes or loads a few information that will be utilized for the duration of the life of the servlet.

The init method definition resembles this −

public void init() throws ServletException { 

//Initialization code... 


The service() Method

The service() method is the fundamental method to play out the genuine assignment. The servlet holder (for example web server) calls the service() method to handle with solicitations originating from the client( programs) and to compose the designed reaction back to the client.

Each time the server gets a request for a servlet, the server brings forth another string and calls service. The service() method checks the HTTP request for sort (GET, POST, PUT, DELETE, and so forth.) and calls doGet, doPost, doPut, doDelete, and so on methods as appropriate.

Here is the signature of this method −

public void service(ServletRequest request, ServletResponse response) 

throws ServletException, IOException { 


The service () method is called by the holder and service method conjures doGet, doPost, doPut, doDelete, and so on methods as fitting. So you don't have anything to do with service() method however you abrogate either doGet() or doPost() contingent upon what sort of request you get from the client.

The doGet() and doPost() are most much of the time utilized methods with in each service ask. Here is the signature of these two methods.

The doGet() Method

A GET request for results from an ordinary request for a URL or from a HTML structure that has no METHOD determined and it ought to be taken handled of by doGet() method.

public void doGet(HttpServletRequest request, HttpServletResponse response) 

throws ServletException, IOException { 

//Servlet code 


The doPost() Method

A POST request for results from a HTML structure that explicitly records POST as the METHOD and it ought to be dealt with by doPost() method.

public void doPost(HttpServletRequest request, HttpServletResponse response) 

throws ServletException, IOException { 

//Servlet code 


After the crush() method is called, the servlet object is set apart for garbage gathering. The demolish method definition resembles this −

public void destroy() { 

//Finalization code... 


Architecture Diagram

The following figure portrays a normal servlet life-cycle scenario.

  • First the HTTP asks for going to the server are assigned to the servlet container.

  • The servlet compartment stacks the servlet before conjuring the service() method.

  • Then the servlet compartment handles numerous solicitations by bringing forth various strings, each string executing the service() method for a solitary occasion of the servlet.

© Javacodegeeks 2018 -
All Right Reserved and you agree to have read and accepted our term and condition.

Python 3