VOOZH about

URL: https://www.geeksforgeeks.org/java/jsp-pagecontext-implicit-objects/

⇱ JSP PageContext - Implicit Objects - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

JSP PageContext - Implicit Objects

Last Updated : 16 May, 2026

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:

👁 jsp_pagecontext
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.

Step 2: Create welcome.jsp

Store user data using pageContext object.

  • Fetch input using request.getParameter().
  • Store value in session scope.

Step 3: Create second.jsp

Retrieve stored data using pageContext.

  • Use getAttribute() method.
  • Specify correct scope (session).

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.

👁 Image

B. JSP page along with details page link.

👁 Image

C. User Credentials display page that we have moved from html page to this page by pageContext instance.

👁 Image
Comment
Article Tags:
Article Tags: