Lecture 18: Servlet Interface & Life Cycle

BMC201 - Web Technology

Mr. Prashant Kumar Nag

2026-02-24

Lecture 18

Servlet Interface & Life Cycle

Week 5 | Unit III: Web App Development (Servlets + JSP)
BMC201 - Web Technology
Mr. Prashant Kumar Nag, Assistant Professor

Learning Objectives


  • Describe Servlet interface and HttpServlet hierarchy
  • Explain init, service, destroy lifecycle phases
  • Apply lifecycle hooks safely

Prerequisites


  • Java basics: classes, methods, objects, and exception handling
  • HTTP request-response fundamentals from Unit I/II web context
  • Revision of Lecture 17 before moving into Servlet Interface & Life Cycle

Syllabus Mapping


  • Unit III topic focus: Servlet Interface & Life Cycle
  • CO alignment: implementation understanding + architecture reasoning
  • Assessment alignment: short definitions + long implementation/design questions

Agenda


  • 5-minute recap from previous lecture
  • Concept deep dive: Servlet Interface & Life Cycle
  • Code/configuration walkthrough and output analysis
  • Debug checklist and exam-oriented summary

Introduction


Lifecycle understanding is key to resource-safe servlet development.

Think About It


What fails if heavy initialization runs per request?

Servlet Life Cycle


Load -> init() once -> service() many -> destroy() once

Code Walkthrough


public class DemoServlet extends HttpServlet {
  public void init() { System.out.println("init"); }
  protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
    res.getWriter().println("service request");
  }
  public void destroy() { System.out.println("destroy"); }
}

Memory Booster


Syllabus memory points for Servlet Interface & Life Cycle:

  • Core recall: init(), service(), destroy()
  • Exam compare: One-time init work vs per-request service work
  • Practical anchor: Implement lifecycle logging and verify invocation order

Live Demo


Live implementation for Servlet Interface & Life Cycle:

?? Open Demo: Lecture 18 - Servlet Interface & Life Cycle

Demo checklist: - implement the key flow for this lecture - verify expected output for one success case - trigger one failure case and explain the fix

Resources & References


Structured Debug Checklist


  1. verify the primary API usage for Servlet Interface & Life Cycle is correct (imports, method names, config)
  2. check request/bean/session flow and object lifecycle assumptions
  3. inspect server logs for the first exception (not just the final symptom)
  4. reproduce one failing case and one passing case before finalizing fixes

Exam Preparation Questions: Short


  • Define Servlet Interface & Life Cycle with one practical use case.
  • Write/identify the key API or construct: init(), service(), destroy().
  • Differentiate: One-time init work vs per-request service work.
  • Mention one common implementation error and correction.

Exam Preparation Questions: Long


  • Explain Servlet Interface & Life Cycle with architecture/flow and implementation steps.
  • Write a structured answer comparing two approaches used in this topic.
  • Discuss debugging strategy for this topic with likely failure points.

Practice Task


  • Implement: Implement lifecycle logging and verify invocation order.
  • Add console/log output to validate flow step-by-step.
  • Document one bug you encountered and the exact fix.

Checklist


Can you:

  • explain Servlet Interface & Life Cycle in your own words?
  • implement a basic example end-to-end?
  • identify and fix one common runtime issue?

Next Lecture


  • Topic: Lecture 19 - Handling HTTP GET & POST Requests
  • Preparation required: revise this lecture summary and code walkthrough

Questions?

Next: Lecture 19