by Dinesh Thakur Category: introduction to Servlet

* Every software and non-software objects life cycle (considering all the operations that are taken place from object birth to object death)

* Our servlet class object life cycle will be managed by servlet container

* Event is an action performed/raised on the object  


  • Servlet container raises the following life cycle events in the life cycle of our servlet program. They are:
  1. Instantiation Event (raises when servlet container creates our servlet class object)
  2. RequestArrival Event (raises when servlet container takes the browser window generated request)
  3. Destruction Event (raises when servlet container is about to destroy our servlet class object)

When container raises these events on our servlet class object it looks to call certain methods automatically so these methods are called as life cycle methods or container callback methods. They are:

init (ServletConfig cg)

Service (ServletRequest req, ServletResponse res)

Destroy ()

Prototype of servlet life cycle methods

Public void service (ServletRequest req, ServletResponse res) throws ServletException, IOException

Public void init (ServletConfig cg) throws ServletException

Public void destroy ()

* For one life cycle event only one life cycle method will be called by servlet container so init (), service (HttpServletRequest, HttpServletResponse), doXxx () methods are not life cycle methods.

* Servlet container calls init (-) life cycle method for instantiation event.                               

* Servlet container calls service (-,-) life cycle method for RequestArrival event.

* Servlet container calls destroy () life cycle method for destruction event

* The method that is called by underlying container automatically based on the event that is raised on the object is called container callback methods.

* Programmer never calls life cycle methods manually; they will be called by underlying container automatically.

* Servlet API as supplied life cycle methods:

  1. To allow programmer to place his choice logics in the execution of servlet programs.
  2. To supply servlet container created objects to servlet program (also for programmers as parameters of life cycle methods) (like request object, response object, ServletConfig object and etc)

* While overriding super class method in subclass, the method can have same modifier or access modifier

* While overriding protected service () method of predefined HttpServlet class in its subclass we can take either protected or public modifiers for that method.

Public class TestSrc extends HttpServlet


      Public void service (here protected method is overridden) (HttpServletRequest,   HttpServletResponse) throws ServletException, IOException







Service () method-1

Public void service (ServletRequest req, ServletResponse res) throws ServletException, IOException

Service () method-2

Protected void service (HttpServletRequest,   HttpServletResponse) throws ServletException, IOException

* Servlet container is responsible to raise various life cycle events on our servlet class objects so logics placed in life cycle methods are not responsible to raise these events but they are responsible to process these events by executing programmers supplied logic.

Example:- Code which is written in init () life cycle method never creates our servlet class object but programmer places this code as instantiation logic to execute for instantiation event raised by servlet container by creating our servlet class object.

* Life cycle method and its logic are not capable of raising life cycle events on the object but when underlying container raises life cycle events on the objects it automatically calls a relevant life cycle methods.

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.