Life Cycle of the Servlet.

Hello Friends,

This is one of my tutorials on Servlet. As we all know, servlets are managed by the web container and among the various responsibilities of the container, life cycle of servlet is the most important one. The life cycle defines that how the servlet is loaded, instantiated and initialized, how it handles requests from clients and how it is taken out of service. The servlet’s life cycle methods are defined in the javax.servlet.Servlet interface which is implemented by every Servlet class by extending the abstract classes – “GenericServlet or HttpServlet”.

The servlet life cycle consists of mainly three methods init(), service() and destroy(). The life cycle starts when container instantiates the object of servlet class and calls the init() method, and ends when the container calls destroy() method. The service() method is the one that actually services the client’s request and processes it.

Following are the four main stages in the life cycle of the servlet:-

1) Loading and Instantiation: The container loads the servlet during startup or when the first client request is made. The loading of the servlet depends on the attribute <load-on-startup> of web.xml file. If the attribute <load-on-startup> has a positive value then the servlet is loaded with loading of the container otherwise it loads when the first request comes for service. After loading of the servlet, container creates the instances of that servlet.

2) Initialization: After creating the instances, the servlet container calls the init() method and passes the servlet initialization parameters to it. The init() must be called by the servlet container before the servlet can service any request so that the servlet object is created. Note that, there are not multiple instances for one servlet. The initialization parameters persist till the servlet is destroyed. The init() method is called only once throughout the life cycle of the servlet.

The servlet will be available for service if it is loaded successfully otherwise the servlet container unloads the servlet.

3) Servicing the Request: Only after the initialization process, the servlet will be ready for service. Servlet creates separate threads for each request, but still there is only a single instance for each servlet. The container calls the service() method for servicing the request. The service() method determines the kind of request and calls the appropriate method (doGet() or doPost()) for handling the request and sends response to the client using the methods of the response object.

4) Destroying the Servlet: If the servlet is no longer needed for servicing any request, the servlet container calls the destroy() method . Like as init(), this method is also called only once throughout the life cycle of the servlet. Calling the destroy() method indicates to the container not to sent any request for service and hence the servlet releases all the resources associated with it.

Note : There is only one main state in life cycle of servlet i.e. Initialized. If the servlet isn’t initialized then it is being initialized by running the constructor or init() method, or it is being destroyed by calling destroy() method.

Below is the pictorial representation of the flow or stages of the servlet through which it has to pass throughout its life.

Hope this will help you.

Thanks,

admin@code2java.com

Related Posts

  • Abstract Class In JAVA

    Hello Friends, This tutorial is for all the Java followers. One of the best feature that is widely used is the term ‘Abstract’. This term can be used as either class or a simple method. An abstract method is any method that is just declared but not instantiated. In other words one can just create…

  • Threads in Java.

    Hello Friends, This is the tutorial for the java developers. One of the most significance feature of core java is Threading. Threading deals with the processing of Threads in a single java program. Let us learn what actually are Threads. *What are Threads? Threads are independently running processes that are isolated from each other upto…

  • Collections In Java.

    Hello friends, Welcome to another tutorial for java followers. You all may have heard about Collections, it is one of the amazing feature in java. Collections are the object for the group of elements, these elements are nothing but the different data structures like as Array Lists, Linked Lists, Vectors, Hash tables,Hash List, Trees, Hash…

  • Maven Installation.

    Hello Friends, This is one of my tutorial for installation of Maven on windows. Maven is a software project management tool. Based on the concept of a project object model (POM), Maven can manage a project’s build, reporting and documentation from a central piece of information. Maven is an open source community and is a…

  • Jira Plugin Deployment.

    Hello Friends, This is one of the most important tutorial for all the jira developers. You may have heard about the plugins in jira which is a simple Java Archive File with .jar extension. This JAR file contains the class files and auxiliary resources associated with the applications. In Jira we can use Eclipse IDE…

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.