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.
- JSP Page – Scope: PAGE_CONTEXT
- HTTP Request – Scope: REQUEST_CONTEXT
- HTTP Session – Scope: SESSION_CONTEXT
- 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.
Methods of pageContext Implicit Object
- 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.
- 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 thatfindAttribute()
looks in all the four levels in a sequential order whilegetAttribute()
looks in a specified scope. For example, in the following statement thegetAttribute()
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);
- 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);
- 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.
Once user clicks the Login button on index.html page. This page is displayed.
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.
Varun Tiwari says
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
Chaitanya Singh says
Thanks Varun :)
I am happy that it helped you.
srinivas says
good explanation..
phani says
thank you so much sir ………it’s a good explanation …..
Krishnakanth says
What is the difference between pageContext and page…and I heard that that PageContext.SESSION_CONTEXT not SESSION_SCOPE
Mathan says
Good Explanation sir, now my doubts got cleared thank u so much
SAI KUMAR REDDY says
We already have request and session implicit object. But again why we are using pagecontext implicit object