pageContext Implicit Object in JSP by R4R Team

pageContext Implicit Object in JSP with examples:
It is an instance of javax.servlet.jsp.PageContext. Using this object you can find attribute, get attribute, set attribute and remove attribute at any of the below levels –

JSP Page – Scope: PAGE_CONTEXT
HTTP Request – Scope: REQUEST_CONTEXT
HTTP Session – Scope: SESSION_CONTEXT
Application Level – Scope: APPLICATION_CONTEXT


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 that findAttribute looks in all the four levels in a sequential order while getAttribute looks in a specified scope. For e.g. – In the below statement the getAttribute method would search for the attribute “ycd.com” 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("ycd.com", PageContext.SESSION_CONTEXT);
Similarly the method can be used for other scopes too :

Object obj = pageContext.getAttribute("ycd", PageContext. REQUEST_CONTEXT);

Object obj = pageContext.getAttribute("ycd", PageContext. PAGE_CONTEXT);

Object obj = pageContext.getAttribute("ycd", PageContext. APPLICATION_CONTEXT);
void removeAttribute(String AttributeName, int Scope): This method is used to remove an attribute from a given scope. For example – The below 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 – Below 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);
pageContext Implicit Object Example:

index.html:

Here we are simply asking user to enter login details.

<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, which means we will be able to access the details till the user’s session is 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>

Leave a Comment:
Search
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!