< Prev
Next >

Lifecycle of a Servlet

There are four stages in the lifecycle of a Servlet -

Loading and Initialization of a Servlet

When a user request is received by the Web Server/Servlet Container. It finds the required Servlet class using the deployment descriptor file(web.xml) and tries to load it using the regular java class loading mechanism. After the successful loading of the Servlet class, Servlet Container instantiate the object of this Servlet class using its no-argument constructor.

The number of instances created by Servlet Container depends on -

Initialization of a Servlet object

After the successful loading and initialization of the Servlet class by the Servlet Container, the Servlet Container immediately calls the init(ServletConfig) method of Servlet class to initialize the Servlet object.

The init(ServletConfig) method is called just once and it is passed the reference of ServletConfig object, which gives an access to Servlet's configuration data or its initialized parameters. Only after the successful initialization of Servlet object, the Servlet Container changes the state of Servlet to active state, making this Servlet ready to handle user requests.

Request Handling by a Servlet

After the successful initialization of the required Servlet object by the Servlet Container, either of the two steps are performed by the Servlet Container -

What is the use of creating these request and response objects?

These request objects(ServletRequest or HttpServletRequest) are required to get the request for a resource made by the client or to get the information sent by the client along the request(could be form filled by the user) and eventually the correct response back is sent to the client using response object (ServletResponse or HttpServletResponse).

After successfully creating the request and response objects, the Servlet Container calls the service(ServletRequest, ServletResponse) method of Servlet, passing the created request and response objects.

Whether the Servlet class has implemented the SingleThreadModel or not?

Do you know the service() may throw an exception?

The service() method may throw a ServletException or or IOException and in this case an appropriate error response is sent back to the cilent. Otherwise, after the successful call to the service() method, an appropriate response is sent back by the Servlet to the Servlet Container, which ultimately sends the response back to the client.

Destroying a Servlet

At the end, the Servlet Container may call the destroy() method on the Servlet class when an exception is thrown during the execution of service() method, which says the Servlet is permanently unavailable.

What happens when a Servlet decides to destroy the instance of Servlet

When a Servlet Container decides to destroy an instance of Servlet, it stops taking any new user request for this particular Servlet. Meanwhile, the Servlet Container waits until all the threads currently executing the service() method finish their job and eventually the Servlet Container calls the destroy() method. Calling destroy method makes sure there are no references to the object of this Servlet and thus making it eligible for garbage collection.

Please share this article -

< Prev
Next >
< Introduction to Servlets
Lifecycle methods of Servlet >
Please subscribe our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next
C++ and Python