Unit III Block 2: Session Tracking and JSP

Lectures 21-24 - cookies, HttpSession, JSP basics, and JSP tags

Block Overview

This block completes Unit III by covering state management in web applications and the basics of Java Server Pages. Students move from raw servlet handling into view generation and reusable server-side page components.

Course Outcome: CO-3 (K3 - Application)
Lectures Covered: 21-24
Theme: Sessions and JSP programming

TipQuick Practice Quizzes (Lectures 21-24)
NoteLecture Materials

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


Lecture 21: Session Tracking using Cookies

NoteLecture Materials

Why Cookies Matter

  • HTTP is stateless, so the server needs a way to recognize repeat visits
  • Cookies store small pieces of data on the client side
  • Typical uses include remembering preferences and lightweight identifiers

Example

Cookie userCookie = new Cookie("username", "student1");
userCookie.setMaxAge(3600);
response.addCookie(userCookie);

Cautions

  • Do not store sensitive data directly in cookies
  • Browser settings can block or clear cookies

Lecture 22: Session Tracking with HttpSession

NoteLecture Materials

Why HttpSession Matters

  • Keeps state on the server instead of exposing data in the browser
  • Useful for authentication, carts, dashboards, and user workflow state

Example

HttpSession session = request.getSession();
session.setAttribute("loggedInUser", "student1");
String user = (String) session.getAttribute("loggedInUser");

Compare with Cookies

  • Cookie data is stored client-side
  • Session data is stored server-side and identified via session ID

Lecture 23: JSP Introduction & Implicit Objects

NoteLecture Materials

What JSP Adds

  • Embeds server-side logic into page rendering
  • Reduces the need to print HTML manually from servlet code
  • Works well as the view layer with servlets/controllers

Common Implicit Objects

  • request
  • response
  • session
  • application
  • out
  • pageContext

Example

<h2>Welcome, <%= request.getParameter("name") %></h2>
<p>Session user: <%= session.getAttribute("loggedInUser") %></p>

Lecture 24: JSP Scripting, Directives, Actions & Custom Tags

NoteLecture Materials

Key JSP Constructs

  • Directives: configure the page or include resources
  • Scriptlets and expressions: embed Java directly in JSP
  • Standard actions: <jsp:include>, <jsp:forward>, <jsp:useBean>
  • Custom tags: reusable logic and view helpers

Example

<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="header.jsp" %>
<jsp:include page="menu.jsp" />

Design Advice

  • Keep business logic in servlets/services
  • Use JSP mainly for rendering output
  • Prefer tags and EL over large scriptlets where possible

Key Takeaways

  • Cookies and HttpSession solve different state-tracking needs
  • JSP is the view-side companion to servlet processing
  • Directives and actions support reusable page composition
  • This block completes Unit III and prepares the move into Spring

Next Block

Unit IV begins with Spring Core, dependency injection, bean life cycle, and configuration. - Dependency Injection - Spring Beans and application context