Updates
  • Starting New Weekday Batch for Full Stack Java Development on 27 September 2025 @ 03:00 PM to 06:00 PM
  • Starting New Weekday Batch for MERN Stack Development on 29 September 2025 @ 04:00 PM to 06:00 PM

PageContext

pageContext is the master implicit object that allows access to other implicit objects in JSP.
You can manage the scopes of JSP using this object.

Managing attributes using pageContext:

You can store attributes directly in requests, sessions, and applications using the setAttribute() method.
You cannot store attributes directly on a page because they are of type java.lang.Object.
In JSP, you can store and access the attributes using a master implicit object called pageContext.

You can use the following methods of PageContext to manage the attributes:

void setAttribute(String name,Object val)
void setAttribute(String name,Object value,int scope)
Object getAttribute(String)
Object getAttribute(String,int scope)
void removeAttribute(String)
void removeAttribute(String,int scope)
Enumeration getAttributeNamesInScope(int scope)
Object findAttribute(String name)

Files Required For Program

1. test.jsp
2. test1.jsp
3. test2.jsp
4. test3.jsp
5. web.xml

1. test.jsp

            
<!DOCTYPE html> 
<html>
   <body>
      <h3> I am test.jsp </h3>
      <% 
         pageContext.setAttribute("A", "10",PageContext.PAGE_SCOPE); 
         pageContext.setAttribute("AA", "100"); //Default is Page
         pageContext.setAttribute("B", "20",PageContext.REQUEST_SCOPE); 
         request.setAttribute("BB", "200"); 
         pageContext.setAttribute("C", "30",PageContext.SESSION_SCOPE); 
         session.setAttribute("CC", "300"); 
         pageContext.setAttribute("D", "40",PageContext.APPLICATION_SCOPE); 
         application.setAttribute("DD", "400"); 
         %> 
      <h3>--------- Before Removing---------- </h3>
      <h3> 1) Page Scope </h3>
      <h3> A => <%= pageContext.getAttribute("A") %></h3>
      <h3> AA => <%= pageContext.getAttribute("AA",PageContext.PAGE_SCOPE) %> </h3>
      <h3> 2) Request Scope </h3>
      <h3> B => <%= request.getAttribute("B") %></h3>
      <h3> BB => <%= pageContext.getAttribute("BB",PageContext.REQUEST_SCOPE) %> </h3>
      <h3> 3) Session Scope </h3>
      <h3> C => <%= session.getAttribute("C") %></h3>
      <h3> CC=> <%= pageContext.getAttribute("CC",PageContext.SESSION_SCOPE) %> </h3>
      <h3> 4) Application Scope </h3>
      <h3> D => <%= application.getAttribute("D") %></h3>
      <h3> DD=> <%= pageContext.getAttribute("DD",PageContext.APPLICATION_SCOPE) %> </h3>
      <% 
         pageContext.removeAttribute("A",PageContext.PAGE_SCOPE); 
         pageContext.removeAttribute("B",PageContext.REQUEST_SCOPE); pageContext.removeAttribute("C",PageContext.SESSION_SCOPE); 
         pageContext.removeAttribute("D", PageContext.APPLICATION_SCOPE); 
         %> 
      <h3>--------- After Removing---------- </h3>
      <h3> 1) Page Scope </h3>
      <h3> A => <%= pageContext.getAttribute("A") %></h3>
      <h3> AA => <%= pageContext.getAttribute("AA",PageContext.PAGE_SCOPE) %> </h3>
      <h3> 2) Request Scope </h3>
      <h3> B => <%= request.getAttribute("B") %></h3>
      <h3> BB => <%= pageContext.getAttribute("BB",PageContext.REQUEST_SCOPE) %> </h3>
      <h3> 3) Session Scope </h3>
      <h3> C => <%= session.getAttribute("C") %></h3>
      <h3> CC=> <%= pageContext.getAttribute("CC",PageContext.SESSION_SCOPE) %> </h3>
      <h3> 4) Application Scope </h3>
      <h3> D => <%= application.getAttribute("D") %></h3>
      <h3> DD=> <%= pageContext.getAttribute("DD",PageContext.APPLICATION_SCOPE) %> </h3>
      <h3>--------findAttribute()-----------------</h3>
      <h3> AA => <%= pageContext.findAttribute("AA") %> </h3>
      <h3> BB => <%= pageContext.findAttribute("BB") %> </h3>
      <h3> CC => <%= pageContext.findAttribute("CC") %> </h3>
      <h3> DD => <%= pageContext.findAttribute("DD") %> </h3>
      <h3> MSg => <%= pageContext.findAttribute("MSG") %> </h3>
   </body>
</html>
        
            

2. test1.jsp

            
<!DOCTYPE html>
<html>
   <body>
      <h3> I am test1.jsp </h3>
      <% 
         pageContext.setAttribute("A", "10",PageContext.PAGE_SCOPE); 
         pageContext.setAttribute("AA", "100"); //Default is Page 
         pageContext.setAttribute("B", "20",PageContext.REQUEST_SCOPE); 
         request.setAttribute("BB", "200"); 
         pageContext.setAttribute("C", "30",PageContext.SESSION_SCOPE); 
         session.setAttribute("CC", "300"); 
         pageContext.setAttribute("D", "40",PageContext.APPLICATION_SCOPE); 
         application.setAttribute("DD", "400"); 
         %> 
      <h3>--------- Before Removing---------- </h3>
      <h3> 1) Page Scope </h3>
      <h3> A => <%= pageContext.getAttribute("A") %></h3>
      <h3>
      AA => <%= pageContext.getAttribute("AA",PageContext.PAGE_SCOPE) %> 
      <h3> 2) Request Scope </h3>
      <h3> B => <%= request.getAttribute("B") %></h3>
      <h3> BB => <%= pageContext.getAttribute("BB",PageContext.REQUEST_SCOPE) %> </h3>
      <h3> 3) Session Scope </h3>
      <h3> C => <%= session.getAttribute("C") %></h3>
      <h3> CC=> <%= pageContext.getAttribute("CC",PageContext.SESSION_SCOPE) %> </h3>
      <h3> 4) Application Scope </h3>
      <h3> D => <%= application.getAttribute("D") %></h3>
      <h3> DD=> <%= pageContext.getAttribute("DD",PageContext.APPLICATION_SCOPE) %> </h3>
      <% 
         pageContext.removeAttribute("A",PageContext.PAGE_SCOPE); 
         pageContext.removeAttribute("B",PageContext.REQUEST_SCOPE); 
         pageContext.removeAttribute("C",PageContext.SESSION_SCOPE); 
         pageContext.removeAttribute("D", PageContext.APPLICATION_SCOPE); 
         %> 
      <h3>--------- After Removing---------- </h3>
      <h3> 1) Page Scope </h3>
      <h3> A => <%= pageContext.getAttribute("A") %></h3>
      <h3> AA => <%= pageContext.getAttribute("AA",PageContext.PAGE_SCOPE) %> </h3>
      <h3> 2) Request Scope </h3>
      <h3> B => <%= request.getAttribute("B") %></h3>
      <h3> BB => <%= pageContext.getAttribute("BB",PageContext.REQUEST_SCOPE) %> </h3>
      <h3> 3) Session Scope </h3>
      <h3> C => <%= session.getAttribute("C") %></h3>
      <h3> CC=> <%= pageContext.getAttribute("CC",PageContext.SESSION_SCOPE) %> </h3>
      <h3> 4) Application Scope </h3>
      <h3> D => <%= application.getAttribute("D") %></h3>
      <h3> DD=> <%= pageContext.getAttribute("DD",PageContext.APPLICATION_SCOPE) %> </h3>
      <jsp:forward page="test2.jsp"/>
   </body>
</html>
        
            

3. test2.jsp

            
<%@ page import="com.jtcindia.jsp.Course"%> 
<!DOCTYPE html> 
<html>
   <body>
      <h3> I am test2.jsp </h3>
      <h3> 1) Page Scope </h3>
      <h3> AA => <%= pageContext.getAttribute("AA",PageContext.PAGE_SCOPE) %> </h3>
      <h3> 2) Request Scope </h3>
      <h3> BB => <%= pageContext.getAttribute("BB",PageContext.REQUEST_SCOPE) %> </h3>
      <h3> 3) Session Scope </h3>
      <h3> CC=> <%= pageContext.getAttribute("CC",PageContext.SESSION_SCOPE) %> </h3>
      <h3> 4) Application Scope </h3>
      <h3> DD=> <%= pageContext.getAttribute("DD",PageContext.APPLICATION_SCOPE) %> </h3>
   </body>
</html>
        
            

4. test1.jsp

            
<!DOCTYPE html> 
<html>
   <body>
      <h3> I am test3.jsp </h3>
      <h3> 1) Page Scope </h3>
      <h3> AA => <%= pageContext.getAttribute("AA",PageContext.PAGE_SCOPE) %> </h3>
      <h3> 2) Request Scope </h3>
      <h3> BB => <%= pageContext.getAttribute("BB",PageContext.REQUEST_SCOPE) %> </h3>
      <h3> 3) Session Scope </h3>
      <h3> CC=> <%= pageContext.getAttribute("CC",PageContext.SESSION_SCOPE) %> </h3>
      <h3> 4) Application Scope </h3>
      <h3> DD=> <%= pageContext.getAttribute("DD",PageContext.APPLICATION_SCOPE) %> </h3>
   </body>
</html>
        
            

5. test1.jsp

            
<?xml version="1.0" encoding="UTF-8"?> 
<web-app …>
   <display-name>Lab13</display-name>
   <welcome-file-list>
      <welcome-file>test.jsp</welcome-file>
   </welcome-file-list>
</web-app>