Site icon DataFlair

JSP PageContext Implicit Object and its Need

FREE Online Courses: Elevate Skills, Zero Cost. Enroll Now!

Page Context is one of the most important implicit objects of JSP. In this article, we will learn about JSP PageContext in detail.

JSP PageContext

It is the implicit object from javax.servlet.jsp.PageContext implementation where PageContext is an abstract implementation.

Furthermore various facilities can be given to the author using Page Context method.

The syntax to use page Context is as follows:
Syntax:

pageContext.methodName(“parameter”);

Some useful methods used with pageContext and their syntax are as follows:

a. Object getAttribute (String AttributeName, int Scope)

This method finds/gets the attribute from the suddested scope.

b. void setAttribute(String AttributeName, Object AttributeValue, int Scope)

This method will set the attribute in the scop along with its value.

c. void removeAttribute(String AttributeName, int Scope)

This method will remove the attribute mentioned from referred scope.

d. Object findAttribute (String AttributeName)

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

This method will find the attribute in all the four scopes and when found none, return null value. The difference between get and find is that scope is defined in the get method.

Example 1

myindex.html
<html>
<head><title>PageContext</title>
</head>
<body>
<form action="PageContext1.jsp">
<input type="text" name="username">
<input type="submit" value="go"><br/>
</form>
</body>
</html>
 
PageContext1.jsp
<html>
<head><title>PageContext</title></head>
<body>
<% 
String myname=request.getParameter("username");
out.print("Welcome "+myname);
  pageContext.setAttribute("client",myname,PageContext.SESSION_SCOPE);  %>
  <a href="PageContext2.jsp"> go to second jsp page</a>
 </body>
</html>
 
PageContext2.jsp
<html>
<body>
<% 
String myname=(String)pageContext.getAttribute("client",PageContext.SESSION_SCOPE);
out.print("Here you will see your name "+myname);
  %>
</body>
</html>

Explanation: In this example pageContext1 sets attribute client in Session scope and forwards to PageContext2 where it gets the attribute and shows the result.

Output:

 

Example 2

get.jsp
<html>
<head><title>JSP Page Context</title>
<h3>--DataFlair--</h3>
</head>
<body>
 Value of Topic in Session Scope:
 <%out.println(pageContext.getAttribute("Topic",PageContext.SESSION_SCOPE)); %><br/>
 	Value of Reference in Application Scope:
 	<%out.println(pageContext.getAttribute("Reference",PageContext.APPLICATION_SCOPE)); %><br/>
</body>
</html>
 	
set.jsp
 
<html>
<head><title>JSP Page Context</title>
<h3>--DataFlair--</h3>
</head>
<body>
<%
pageContext.setAttribute("Topic","PageContext",PageContext.SESSION_SCOP
E);
   pageContext.setAttribute("Reference","JSP",PageContext.APPLICATION_SCOPE);
%>
<a href="get.jsp">Go to Second page</a>
</body>
</html>

Explanation: In this example, we are using page Context to set and get the attributes in session and application scope.

Output:

Conclusion

In reference to this article, we came to know about the JSP PageContext Implicit object in detail. It is the implicit object from javax.servlet.jsp.PageContext implementation. This object has the capability to get, set and remove attributes. This object can get information from a JSP page. We saw methods used with it. We also saw its implication using examples.

Exit mobile version