Open In App

JSP PageContext – Implicit Objects

PageContext extends JspContext to contribute helpful context details while JSP technology is applied in a Servlet environment. A PageContext is an instance that gives access to all the namespaces related to a JSP page, gives access to some page attributes and a layer over the application details. Implicit objects are connected to the pageContext consequently.



  1. page – Scope: PAGE_CONTEXT
  2. request – Scope: REQUEST_CONTEXT
  3. session – Scope: SESSION_CONTEXT
  4. application – Scope: APPLICATION_CONTEXT

Note: Page scope is the default scope in JSP.

Syntax:



public abstract class PageContext
extends JspContext

Syntax: To use pageContext

pageContext.methodName(“name of attribute”, “scope”);

Adhering forward, now let us discuss the methods used in pageContext implicit object. Below several methods are proposed that are used in pageContext object of which most frequently been involved are discussed below individually to a depth followed by clean java program to illustrate the implementation of implicit objects in JSP PageContext class.

Remember: It supports over 40 methods which are inherited from ContextClass. 

Method 1: getAttribute (String AttributeName, int Scope) 

The getAttribute method finds an attribute in the described scope. For example, the statement given below the getAttribute method finds the attribute “GeeksforGeeks” in the scope of Session (Session layer). If it finds the attribute then it will assign the attribute to Object obj otherwise it will return Null.

Syntax: 

Object obj = pageContext.getAttribute("GeeksforGeeks", PageContext.SESSION_CONTEXT);

Correspondingly, this method can be used for more other scopes also, like as follows:

Method 2: findAttribute (String AttributeName)

The findAttribute() method finds the described attribute in all four levels in the following order listed below. At any level, if no attribute is found then it will return NULL. 

Page --> Request --> Session and Application

Method 3: void setAttribute(String AttributeName, Object AttributeValue, int Scope)

This method sets down an attribute in a given scope. For example, consider the statement given below will save an Attribute “data” in the scope of application with the value “This is data”.

Syntax:

pageContext.setAttribute(“data”, “This is data”, PageContext. APPLICATION_CONTEXT);

Correspondingly, this method will design an attribute named attr1 in the scope of Request with the value “Attr1 value” is as follows:

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

Method 4: void removeAttribute(String AttributeName, int Scope) 

In order to remove an attribute from a given scope, this method is used. For example, consider the JSP statement given below will remove an Attribute “Attr” from page scope.

Syntax:

pageContext.removeAttribute(“Attr”, PageContext. PAGE_CONTEXT);

Lastly, let us implement via illustrating HTML code samples of pageContext implicit object

Example 1: index.html

Page 1: In this HTML page, we are simply asking the user to enter the name.




<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>GeeksforGeeks</title>
</head>
<body>
  
<form action="welcome.jsp">  
<input type="text" name="uname">  
<input type="submit" value="go"><br/>  
</form>
  
</body>
</html>

Page 2: It is a JSP page, we are saving the user’s name using pageContext implicit object with the session scope, means we will be able to access the details until the user’s session is active.

Example 2: welcome.jsp 




<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
  
<%   
  
String name=request.getParameter("uname");  
out.print("Welcome "+name);  
    
pageContext.setAttribute("user",name,PageContext.SESSION_SCOPE);   
    
%> 
  
<a href="second.jsp">second jsp page</a
  
</body>
</html>

Page 3: In this JSP page, we are retrieving the saved attributes using the getAttribute method. The point which is to be considered here is that we have saved the attributes with session scope so we should need to state scope as a session so as to retrieve those attribute’s value.

Example 3: second.jsp




<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
  
<%   
   
String name=(String)pageContext.getAttribute("user",PageContext.SESSION_SCOPE);  
out.print("Hello "+name);  
    
%> 
  
</body>
</html>

Output: 

An HTML page where we are receiving the user’s name.

B JSP page along with details page link.

C User Credentials display page that we have moved from html page to this page by pageContext instance.


Article Tags :