2081 10

CSC419 ยท 2081 ยท 5 marks

Explain the life cycle of servlets.

Explain the life cycle of servlets. [5]

Advanced Java ProgrammingChapter 7: Servlets and Java Server pagesWeb Container, Introduction to Servlets, Life cycle of servl

Quick answer

A Servlet is a small Java program that executes on the server side of a Web connection and dynamically extends the functionality of a web server. All servlets must implement the Servlet interface, which defines the life-cycle methods. The servlet life cycle is managed by the Servlet Engine (Container).

Life Cycle of a Servlet

Introduction


Stages of the Servlet Life Cycle

The Servlet Life Cycle has 5 stages in general. Three methods are central to the life cycle: init(), service(), and destroy().


Stage 1: Loading and Instantiation

  • The class loader is responsible for loading the servlet class from the file system or network.
  • After loading, the servlet engine creates an instance of the servlet class.
  • The servlet instance is created only once in the servlet life cycle.
  • The servlet engine can instantiate more than one servlet instance if needed (e.g., for load balancing).

Stage 2: Initialization (init())

  • After instantiation, the servlet container calls the init() method.
  • This method is called only once during the entire life of the servlet.
  • It is used to perform one-time initialization tasks such as opening database connections, loading configuration parameters, etc.
  • Signature:
public void init(ServletConfig config) throws ServletException

Stage 3: Request Handling (service())

  • Once initialized, the servlet is ready to handle client requests.
  • For each incoming request, the servlet container calls the service() method.
  • The service() method determines the type of HTTP request (GET, POST, PUT, DELETE, etc.) and dispatches it to the appropriate handler method (doGet(), doPost(), etc.).
  • This method is called multiple times, once per request.
  • Signature:
public void service(ServletRequest req, ServletResponse res)
    throws ServletException, IOException

Stage 4: Destroying (destroy())

  • When the servlet container decides to remove the servlet (e.g., server shutdown or redeployment), it calls the destroy() method.
  • This method is called only once, at the end of the servlet's life.
  • It is used to perform cleanup tasks such as closing database connections, releasing resources, etc.
  • Signature:
public void destroy()

Stage 5: Garbage Collection

  • After destroy() is called, the servlet instance becomes eligible for garbage collection.
  • The JVM's garbage collector reclaims the memory occupied by the servlet object.

Diagram of Servlet Life Cycle

Client Request
      |
      v
[1. Load & Instantiate Servlet Class]
      |
      v
[2. init()  --> Called ONCE]
      |
      v
[3. service() --> Called per request]
   /    \
doGet() doPost() ...
      |
      v
[4. destroy() --> Called ONCE]
      |
      v
[5. Garbage Collection]

Summary Table

StageMethodCalled
Loading & Instantiation(by class loader)Once
Initializationinit()Once
Request Handlingservice()Per request
Destructiondestroy()Once
Garbage Collection(by JVM)Once

Note: The packages java.x.servlet and java.x.servlet.http provide the interfaces and classes required to implement these life-cycle methods. A server such as Apache Tomcat or GlassFish is needed to run servlets.

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.