Lecture 30: JSP Implicit Objects & Scripting

BMC201 - Web Technology

Mr. Prashant Kumar Nag

2026-03-24

Lecture 30

JSP Implicit Objects & Scripting

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

Learning Objectives


  • Identify all key JSP implicit objects and their purpose
  • Use request, response, session, and application correctly
  • Apply scriptlets, expressions, and declarations with safe scope awareness
  • Compare page/request/session/application attribute scopes
  • Avoid common beginner mistakes while mixing Java and JSP markup

What Are Implicit Objects?


JSP provides built-in objects automatically. You can use them without declaring them.

Most used objects: - request, response, session - application, out, pageContext - config, page, exception

Core Implicit Objects and Use


Object Type Typical Use
request HttpServletRequest Read query/form input
response HttpServletResponse Set headers/status/redirect
session HttpSession Per-user state
application ServletContext App-wide shared data
out JspWriter Write output
pageContext PageContext Access all scopes

Using request and response


<%
  String city = request.getParameter("city");
  if (city == null || city.isBlank()) city = "Lucknow";
%>
<p>Selected city: <%= city %></p>
<% response.setHeader("X-Course", "BMC201"); %>

Using session safely


<%
  session.setAttribute("user", "student01");
  String user = (String) session.getAttribute("user");
%>
<h3>Welcome, <%= user %></h3>
<p>Session ID: <%= session.getId() %></p>

Store only minimal, non-sensitive state in session.

Scriptlet, Expression, Declaration


<%! int visitCount = 0; %>
<% visitCount++; %>
<p>Visits to this JSP instance: <%= visitCount %></p>
  • Declaration (<%! %>) creates class-level members
  • Scriptlet (<% %>) runs Java statements
  • Expression (<%= %>) prints evaluated result

Attribute Scopes in JSP


<%
  pageContext.setAttribute("p", "page-value", PageContext.PAGE_SCOPE);
  request.setAttribute("r", "request-value");
  session.setAttribute("s", "session-value");
  application.setAttribute("a", "app-value");
%>

Use the smallest scope needed for better clarity and performance.

Common Mistakes to Avoid


  • Heavy business logic inside JSP scriptlets
  • Using declaration variables for shared mutable state carelessly
  • Assuming session always exists in every page flow
  • Printing unvalidated input directly to output
  • Overusing scriptlets instead of moving logic to servlet/controller

Classroom Practice Task


Build a small profile.jsp page that:

  1. Reads name and city from request
  2. Stores name in session
  3. Prints values using expression tags
  4. Adds one application attribute for course name

Summary


  • Implicit objects simplify JSP development
  • Scripting elements are useful but should stay minimal
  • Correct scope choice prevents bugs and confusion
  • MVC approach keeps JSP clean and maintainable

Questions

Thank You

Questions or doubts before we move to JSP directives and actions?