Unit III Block 1: Servlet Foundations

Lectures 17-20 - servlet architecture, lifecycle, request handling, and dispatching

Block Overview

This block starts Unit III and moves from client-side pages to server-side request processing. The focus is on what a servlet is, how the container manages it, and how requests are received, processed, and routed.

Course Outcome: CO-3 (K3 - Application)
Lectures Covered: 17-20
Theme: Core servlet programming

TipQuick Practice Quizzes (Lectures 17-20)
NoteLecture Materials

This week uses upcoming lecture decks. Use the Lectures Index for all available slides.


Lecture 17: Servlet Overview & Architecture

NoteLecture Materials

Core Ideas

  • What a servlet container does in a Java web application
  • How browser requests reach a servlet
  • Role of web.xml, annotations, and deployment descriptors
  • Basic request-response flow in server-side applications

Typical Request Flow

Browser -> Web Server -> Servlet Container -> Servlet -> Response

Focus Points

  • Difference between static page delivery and dynamic response generation
  • Why servlets are used for business logic and request processing
  • Where JSP fits relative to servlets

Lecture 18: Servlet Interface & Life Cycle

NoteLecture Materials

Lifecycle Methods

  • init() for one-time startup work
  • service() for request handling
  • destroy() for cleanup before shutdown

Lifecycle Sequence

Load class -> Create instance -> init() -> service() -> destroy()

Why It Matters

  • Explains when servlet objects are created
  • Helps avoid repeated setup work inside request processing
  • Introduces thread-safety concerns in shared servlet instances

Lecture 19: Handling HTTP GET & POST Requests

NoteLecture Materials

Request Handling

  • doGet() for safe reads and page fetches
  • doPost() for form submission and state-changing operations
  • Parameters read through request.getParameter()

Example

protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
  String username = request.getParameter("username");
  response.getWriter().println("Welcome " + username);
}

Comparison

  • GET data is visible in URL and suitable for retrieval
  • POST sends data in request body and is used for submit/update flows

Lecture 20: Redirecting Requests & Request Dispatching

NoteLecture Materials

Forward vs Redirect

  • forward() keeps processing on the server and usually preserves request data
  • sendRedirect() tells the browser to make a fresh request
  • Redirect changes the URL; forward usually does not

Example

if (validUser) {
  response.sendRedirect("home.jsp");
} else {
  request.setAttribute("error", "Invalid credentials");
  request.getRequestDispatcher("login.jsp").forward(request, response);
}

Decision Rule

  • Use redirect after successful submit flows
  • Use forward when the next page needs the current request attributes

Key Takeaways

  • A servlet container manages creation, execution, and cleanup of servlets
  • doGet() and doPost() solve different request scenarios
  • Request dispatching controls how the user moves between server resources
  • This block prepares the shift to session management and JSP

Next Block

Unit III continues with cookies, HttpSession, JSP basics, and JSP tags.