JSP PageContext Implicit Object and its Need

FREE Online Courses: Dive into Knowledge for Free. Learn More!

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

JSP PageContext

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

  • This object has the capability to get, set and remove attributes
  • It contains information Of directives, buffer information, error page URL
  • It has four scopes namely: page, request, session and application scopes
  • This holds reference to request and response as well
  • It can support over 40 methods that are inherited from ContextClass
  • It is even useful to get info of a complete JSP page

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

  • An API to manage the many namespaces
  • APIs to access many public objects
  • A procedure to get the JspWriter for the output
  • A process to manage sessions that a page will use
  • Exposing attributes of the page directive to the scripting environment
  • Forwarding requests to other pages
  • Handling error page exception

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.

  • Object obj1 = pageContext.getAttribute(“name”, PageContext.SESSION_CONTEXT);
  • Object obj2 = pageContext.getAttribute(“address”, PageContext. REQUEST_CONTEXT);
  • Object obj3= pageContext.getAttribute(“number”, PageContext. PAGE_CONT EXT);
  • Object obj4 = pageContext.getAttribute(“data”, PageContext. APPLICATION_CONTEXT);

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

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

  • Object obj1 = pageContext.setAttribute(“name”, “Kajal”, PageContext.SESSION_CONTEXT);

c. void removeAttribute(String AttributeName, int Scope)

This method will remove the attribute mentioned from referred scope.

  • Object obj1 = pageContext.removeAttribute(“name”, PageContext.SESSION_CONTEXT);

d. Object findAttribute (String AttributeName)

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.

  • Object obj2 = pageContext.findAttribute(“address”);

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:

 

pagecontext in jsp

PageContext in jsp

PageContext in jsp

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:

pagecontext

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.

Your 15 seconds will encourage us to work even harder
Please share your happy experience on Google

follow dataflair on YouTube

Leave a Reply

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