BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

pageContext Implicit Object in JSP with examples

Last Updated: July 27, 2022 by Chaitanya Singh | Filed Under: JSP tutorial

In this guide, you will learn pageContext implicit object in JSP. It is an instance of javax.servlet.jsp.PageContext. JSP pageContext implicit object can be used to get attribute, set attribute or remove attribute at any of the following scopes.

  1. JSP Page – Scope: PAGE_CONTEXT
  2. HTTP Request – Scope: REQUEST_CONTEXT
  3. HTTP Session – Scope: SESSION_CONTEXT
  4. Application Level – Scope: APPLICATION_CONTEXT

Quick links: This guide is long. If you want to skip the following method descriptions list and want to quickly refer the pageContext implicit object example and project screenshot, use the following jump links.

  • pageContext implicit object complete example
  • Screenshots of the example’s output

Methods of pageContext Implicit Object

  1. Object findAttribute (String AttributeName): This method searches for the specified attribute in all four levels in the following order: Page, Request, Session and Application. It returns NULL when no attribute found at any of the level.
  2. Object getAttribute (String AttributeName, int Scope): It looks for an attribute in the specified scope. This method is similar to findAttribute() method, the only difference is that findAttribute() looks in all the four levels in a sequential order while getAttribute() looks in a specified scope. For example, in the following statement the getAttribute() method would search for the attribute “BeginnersBook” in Session scope (or Session level/layer). If it finds the attribute it would assign it to Object obj else it would return null.
    Object obj = pageContext.getAttribute("BeginnersBook", PageContext.SESSION_CONTEXT);

    Similarly the method can be used for other scopes as well as shown below:

    Object obj = pageContext.getAttribute("BeginnersBook", PageContext. REQUEST_CONTEXT);
    Object obj = pageContext.getAttribute("BeginnersBook", PageContext. PAGE_CONTEXT);
    Object obj = pageContext.getAttribute("BeginnersBook", PageContext. APPLICATION_CONTEXT);
  3. void removeAttribute(String AttributeName, int Scope): This method is used to remove an attribute from a given scope. For example: The following JSP statement would remove an attribute “MyAttr” from page scope.
    pageContext.removeAttribute(“MyAttr”, PageContext. PAGE_CONTEXT);
  4. void setAttribute(String AttributeName, Object AttributeValue, int Scope): It writes an attribute in a given scope. Example: The following statement would store an attribute “mydata” in application scope with the value “This is my data”.
    pageContext.setAttribute(“mydata”, “This is my data”, PageContext. APPLICATION_CONTEXT);

    Similarly this would create an attribute named attr1 in Request scope with value “Attr1 value”.

    pageContext.setAttribute(“attr1”, “Attr1 value”, PageContext. REQUEST_CONTEXT);

JSP pageContext Implicit Object Example

index.html
Here, user is asked to enter user id and password. Once the details are entered, user clicks the Login button.

<html>
<head>
<title> User Login Page – Enter details</title>
</head>
<body>
<form action="validation.jsp">
Enter User-Id: <input type="text" name="uid"><br>
Enter Password: <input type="text" name="upass"><br>
<input type="submit" value="Login">
</form>
</body>
</html>

validation.jsp

In this page we are storing user’s credentials using pageContext implicit object with the session scope. This means we will be able to access the details till the user’s session remains active. We can also store the attribute using other scope parameters such as page, application and request.

<html>
<head> <title> Validation JSP Page</title>
</head>
<body>
<% 
String id=request.getParameter("uid");
String pass=request.getParameter("upass");
out.println("hello "+id);
pageContext.setAttribute("UName", id, PageContext.SESSION_SCOPE);
pageContext.setAttribute("UPassword", pass, PageContext.SESSION_SCOPE);
%>
<a href="display.jsp">Click here to see what you have entered </a>
</body>
</html>

display.jsp

In this JSP page we are fetching the stored attributes using getAttribute() method. The point to note here is that we have stored the attributes with session scope so we must need to specify scope as session in order to fetch those attribute’s value.

<html>
<head>
<title>Displaying User Details</title>
</head>
<body>
<%
String username= (String) pageContext.getAttribute("UName", PageContext.SESSION_SCOPE);
String userpassword= (String) pageContext.getAttribute("UPassword", PageContext.SESSION_SCOPE);
out.println("Hi "+username);
out.println("Your Password is: "+userpassword);
%>
</body>
</html>

Screenshots of the example’s output:

This is the Login page where user is asked to enter User-Id and Password.

pageContext Implicit Object in JSP
Once user clicks the Login button on index.html page. This page is displayed.
pageContext Implicit Object in JSP
Once user clicks the link in the validation.jsp page, the following page is displayed. This page is showing the details that we have entered in the login page. These details were stored in the session using pageContext implicit object. We were able to get this data using getAttribute() method of pageContext object.
pageContext Implicit Object in JSP

❮ JSP outJSP exception ❯

Top Related Articles:

  1. Java Server Pages (JSP) Life Cycle
  2. JSP Scriptlet Tag (Scripting elements)
  3. Config Implicit Object in JSP with examples
  4. Session Implicit Object in JSP with examples
  5. Request Implicit Object in JSP with examples

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. Varun Tiwari says

    April 4, 2014 at 4:50 AM

    Sir thanks a lot for your example of pageContext, I tried many times but I had confusion about that, but after reading your example it is now clear to me.

    Thanks

    Reply
    • Chaitanya Singh says

      April 6, 2014 at 3:13 PM

      Thanks Varun :)
      I am happy that it helped you.

      Reply
  2. srinivas says

    July 15, 2014 at 5:54 PM

    good explanation..

    Reply
  3. phani says

    October 21, 2014 at 4:07 PM

    thank you so much sir ………it’s a good explanation …..

    Reply
  4. Krishnakanth says

    November 21, 2014 at 4:05 AM

    What is the difference between pageContext and page…and I heard that that PageContext.SESSION_CONTEXT not SESSION_SCOPE

    Reply
  5. Mathan says

    February 14, 2015 at 3:02 AM

    Good Explanation sir, now my doubts got cleared thank u so much

    Reply
  6. SAI KUMAR REDDY says

    March 2, 2016 at 7:13 AM

    We already have request and session implicit object. But again why we are using pagecontext implicit object

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

JSP Tutorial

  • Basics of JSP
  • Life cycle of JSP
  • JSP in Eclipse IDE

Scripting Elements

  • Scriptlet Tag
  • Expression tag
  • Declaration tag

Implicit Objects

  • Implicit Objects
  • JSP Request
  • JSP Response
  • JSP Config
  • JSP Application
  • JSP Session
  • JSP Out
  • JSP pageContext
  • JSP Exception
  • Validate session

JSP directives

  • JSP Directives
  • Include Directive

JSP Exception

  • Exception handling

JSP Action

  • Action tags
  • Include action
  • Forward action
  • useBean, setProperty & getProperty

Expression language

  • Expression language

JSP Custom Tags

  • Custom Tags
  • Custom tag example
  • JSP Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap