Advanced Java and Web Technologies for JNTUK
Blog providing beginner tutorials on different web technologies like HTML, CSS, Javascript, PHP, MYSQL, XML, Java Beans, Servlets, JSP and AJAX
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.
AJWT » Servlets » Life Cycle of a Servlet
Suryateja Pericherla Categories: Servlets. No Comments on Life Cycle of a Servlet
5
(1)

Introduction

 

Life cycle of a servlet contains the following stages:

  • Instantiation
  • Initialization
  • Servicing
  • Destruction

 

Following figure illustrates the life cycle of a servlet:

 

servlet-life-cycle

 


Subscribe to our monthly newsletter. Get notified about latest articles, offers and contests.


Instantiation

 

In this stage the servlet container searches the web.xml file for servlet. If the servlet is found, it will create an object for the corresponding servlet class in the memory. Servlet container can search for the servlet file in the local disk (in server) or in a remote location.

 

Initialization

 

After instantiation of the servlet, the servlet container initializes the servlet object. Servlet container invokes the init(ServletConfig) method on the servlet object. This stage may raise ServletException or UnavailableException.

 

Servicing

 

After initialization, servlet container creates the objects of HTTPServletRequest and HTTPServletResponse and invokes the service(request, response) method to serve the request of the client. This stage may raise UnavailableException or IOException.

 

Destruction

 

A servlet is destroyed (unloaded) in the following cases:

  • When the context is unloaded (server shutdown).
  • When the service() method throws UnavailableException.

 

When the container decides to destroy the servlet, it allows all the current running threads to complete and stop any further requests. Finally destroy() method is invoked.

 

Note: In the servlet life cycle, init() and destroy() execute only once and service() method executes multiple times.

 

Video: http://www.startertutorials.com/ajwt/servlet-life-cycle.html

Above video demonstrates life cycle of a servlet using Eclipse IDE.

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.

Leave a Reply

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

Facebook
Twitter
Pinterest
Youtube
Instagram