JavaServer Pages (JSP) provides the pageContext implicit object to access all page-related data. It acts as a central object that gives access to different scopes and implicit objects. It helps manage attributes and share data across JSP components.
- pageContext is an instance of PageContext class.
- It provides access to page, request, session, and application scopes.
- It allows setting, getting, and removing attributes easily.
Types of Scopes
- Page Scope (
PAGE_SCOPE): Data is accessible only within the current JSP page - Request Scope (
REQUEST_SCOPE): Data is available throughout a single HTTP request - Session Scope (
SESSION_SCOPE): Data is available across multiple requests for the same user session - Application Scope (
APPLICATION_SCOPE): Data is shared across the entire application
Methods of PageContext
There are four type of jsp PageContext:

getAttribute()
Retrieves an attribute from a specified scope.
- Returns value if found, otherwise returns
null. - Scope must be specified (page, request, session, application).
Syntax:
Object obj = pageContext.getAttribute("name", PageContext.SESSION_SCOPE);
findAttribute()
Searches an attribute across all scopes automatically.
- Searches in order: Page -> Request -> Session -> Application.
- Returns first matched value or
null.
Syntax:
Object obj = pageContext.findAttribute("name");
setAttribute()
Stores an attribute in a specified scope
- Used to share data between JSP pages.
- Scope defines how long the data will persist.
Syntax:
pageContext.setAttribute("name", value, PageContext.SESSION_SCOPE);
removeAttribute()
Removes an attribute from a given scope.
- Helps manage memory by clearing unused data.
- Scope must match where attribute was stored.
Syntax:
pageContext.removeAttribute("name", PageContext.PAGE_SCOPE);
Step-by-Step Implementation
Below are the steps of implementing Jsp PageContext.
Step 1: Create index.html
Create a form to take user input.
- Use input field to enter name.
- Submit data to JSP page.
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>GeeksforGeeks</title>
</head>
<body>
<form action="welcome.jsp">
<input type="text" name="uname">
<input type="submit" value="go"><br/>
</form>
</body>
</html>
Step 2: Create welcome.jsp
Store user data using pageContext object.
- Fetch input using request.getParameter().
- Store value in session scope.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
String name=request.getParameter("uname");
out.print("Welcome "+name);
pageContext.setAttribute("user",name,PageContext.SESSION_SCOPE);
%>
<a href="second.jsp">second jsp page</a>
</body>
</html>
Step 3: Create second.jsp
Retrieve stored data using pageContext.
- Use getAttribute() method.
- Specify correct scope (session).
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
String name=(String)pageContext.getAttribute("user",PageContext.SESSION_SCOPE);
out.print("Hello "+name);
%>
</body>
</html>
Step 4: Run the Application
Execute project and verify output.
- Enter name and navigate between pages.
- Data persists due to session scope.
Output:
A. HTML page where we are receiving the user's name.
B. JSP page along with details page link.
C. User Credentials display page that we have moved from html page to this page by pageContext instance.