Nearby lessons

9 of 30

JSP - Scopes (page, request, session, application)

📌 What You Will Learn
  • Understand the 4 JSP scopes and their lifecycles
  • Know which scope object manages each scope
  • Use findAttribute() to search across all scopes

JSP Scopes determine how long an attribute lives and which components can access it. JSPs have 4 scopes — more than Servlets' 3 — and the pageContext object manages them all.

4 JSP Scopes

ScopeManaged ByAvailable ToLifecycle
PagepageContextCurrent JSP onlySingle request
RequestrequestCurrent request (all forwarded/included JSPs)Single request
SessionsessionAll requests from the same clientUntil session expires
ApplicationapplicationAll components in the web appUntil server shutdown

Scope Lifecycle

  • Request scope: starts at request creation, ends when request processing completes. Attributes are destroyed after service() finishes.
  • Session scope: starts when session is created, ends on logout (invalidate()) or timeout.
  • Application scope: starts at deployment/server startup, ends at undeployment/shutdown.
  • Page scope: starts and ends with the current JSP page (most restricted).

Attribute Management Methods

All scope objects support the same 4 methods:

Example03
JCode Cell
1 
2public void setAttribute(String name, Object value)
3public Object getAttribute(String name)
4public void removeAttribute(String name)
5public Enumeration getAttributeNames()
6

PageContext Scope Constants

The PageContext class defines scope constants for use with its methods:

Example04
JCode Cell
1 
2PageContext.PAGE_SCOPE 1
3PageContext.REQUEST_SCOPE 2
4PageContext.SESSION_SCOPE 3
5PageContext.APPLICATION_SCOPE 4
6

PageContext Attribute Methods

PageContext provides extra methods to manage attributes in any scope by specifying the scope constant:

Example05
JCode Cell
1 
2public void setAttribute(String name, Object value, int scope)
3public Object getAttribute(String name, int scope)
4public void removeAttribute(String name, int scope)
5

findAttribute() — The All-Rounder

pageContext.findAttribute("name") searches all 4 scopes in order:

  1. Page scope
  2. Request scope
  3. Session scope
  4. Application scope

Returns the value of the first match found, or null if not found in any scope.

Demo Program

Setting attributes in all 4 scopes and reading them:

Example07
JCode Cell
1 
2first.jsp:
3<%
4 pageContext.setAttribute("p", "page");
5 request.setAttribute("r", "request");
6 session.setAttribute("s", "session");
7 application.setAttribute("a", "application");
8 pageContext.forward("second.jsp");
9%>
10 
11second.jsp:
12Page Scope Attribute: <%= pageContext.getAttribute("p") %><br/>
13Request Scope Attribute: <%= pageContext.getAttribute("r", 2) %><br/>
14Session Scope Attribute: <%= pageContext.getAttribute("s", 3) %><br/>
15Application Scope Attribute: <%= pageContext.getAttribute("a", 4) %>
16

findAttribute() Demo

Setting the same attribute name in all scopes — findAttribute() returns the page scope value:

Example08
JCode Cell
1 
2<%
3 pageContext.setAttribute("a", "page");
4 request.setAttribute("a", "request");
5 session.setAttribute("a", "session");
6 application.setAttribute("a", "application");
7%>
8<h1>Find Attribute value: <%= pageContext.findAttribute("a") %></h1>
9
📝 Key Takeaways
  • Page scope is most restricted; application scope is least restricted
  • findAttribute() searches page → request → session → application in order
  • PageContext is the single point of contact for attribute management

🧠 Test Your Knowledge

3 Questions
Progress: 0 / 3