Lecture 29: JSP Introduction & JSP Overview

BMC201 - Web Technology

Mr. Prashant Kumar Nag

2026-03-24

Lecture 29

JSP Introduction & JSP Overview

Week 10 | Unit III: JSP
BMC201 - Web Technology
Mr. Prashant Kumar Nag, Assistant Professor

Learning Objectives


  • Define JSP and explain why it is used in Java web applications
  • Understand how a JSP is translated and executed by a servlet container
  • Differentiate JSP from servlets for presentation and controller responsibilities
  • Use basic JSP elements: directives, scriptlets, expressions, and declarations
  • Recognize common implicit objects used inside JSP pages
  • Follow beginner-safe practices for JSP coding in exam and lab scenarios

Why JSP Was Introduced


Writing full HTML in servlets quickly becomes difficult to read and maintain.

JSP solves this by allowing HTML-first pages with embedded Java when needed.

  • Servlet focus: request handling and business logic
  • JSP focus: rendering UI/view

What is JSP?


JSP (JavaServer Pages) is a server-side technology used to generate dynamic web pages.

  • File extension: .jsp
  • Processed on server, output sent as HTML to browser
  • Runs inside servlet containers like Tomcat

JSP Lifecycle in Container


flowchart LR
  A[Browser requests JSP] --> B[Container translates JSP to servlet]
  B --> C[Compiles servlet class]
  C --> D[Creates servlet instance]
  D --> E[jspInit called once]
  E --> F[_jspService called per request]
  F --> G[jspDestroy at unload]

Translation + compilation happens first time (or after change), then normal request handling continues.

JSP vs Servlet


Aspect JSP Servlet
Main role View/presentation Controller/request processing
Authoring style HTML with JSP tags/code Java class with response writing
Maintainability for UI Better Harder for large HTML
Compilation Auto-translated to servlet Already Java servlet class

Core JSP Elements


Common JSP syntax forms:

  • Directive: <%@ ... %>
  • Scriptlet: <% ... %>
  • Expression: <%= ... %>
  • Declaration: <%! ... %>

In modern apps, use EL/JSTL where possible and keep Java code minimal in JSP.

Page Directive Example


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.time.LocalDate" %>
<%@ page errorPage="error.jsp" %>

The page directive configures the JSP page-level behavior.

Scriptlet and Expression Example


<%
  String name = request.getParameter("name");
  if (name == null || name.isBlank()) name = "Student";
%>

<h2>Welcome, <%= name %></h2>
<p>Today is <%= java.time.LocalDate.now() %></p>

Scriptlet runs Java code; expression prints evaluated value into HTML output.

Important JSP Implicit Objects


Object Type Use
request HttpServletRequest Read form/query input
response HttpServletResponse Set headers/status
session HttpSession Per-user session state
application ServletContext App-wide shared context
out JspWriter Write output stream
pageContext PageContext Access scoped attributes

Servlet + JSP MVC Flow


String name = request.getParameter("name");
request.setAttribute("userName", name);

RequestDispatcher rd = request.getRequestDispatcher("welcome.jsp");
rd.forward(request, response);
<h2>Hello, ${userName}</h2>

Controller servlet prepares data; JSP renders it.

Best Practices for Beginners


  • Keep business logic in servlets/services, not in JSP pages
  • Prefer EL/JSTL over heavy scriptlets
  • Never print sensitive data directly in responses
  • Validate request parameters before use
  • Use forwarding from servlet to JSP for clean MVC structure

Common Mistakes to Avoid


  • Writing full database logic inside JSP
  • Using scriptlets everywhere instead of EL/JSTL
  • Forgetting null checks for request.getParameter(...)
  • Mixing redirect and forward without clear control flow
  • Ignoring character encoding (UTF-8) in page setup

AKTU Exam-Oriented Checklist


Prepare these high-probability questions:

  1. Define JSP and explain why it is needed
  2. Explain JSP lifecycle steps in container
  3. Differentiate JSP and servlet with examples
  4. List and explain JSP implicit objects
  5. Explain directive, scriptlet, expression, declaration

Summary


You are now ready to:

  • explain JSP fundamentals clearly
  • write basic JSP pages with proper structure
  • connect servlet controller flow to JSP views
  • avoid common beginner mistakes in labs and exams