Friday, December 27, 2024
Google search engine
HomeLanguagesJavaJSP PageContext – Implicit Objects

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.

  • The PageContext class is an abstract class that is formed to be extended to give application-dependent applications whereof by compatible JSP engine runtime environments. In JSP, pageContext is an instance of javax.servlet.jsp.PageContext.
  • The entire JSP page is represented by the PageContext object. This object is considered as a method to obtain detail about the page while keeping away from most of the execution information.
  • For each request, the credentials to the response and request objects are saved by this pageContext object. By accessing attributes of the pageContext object, the out, session, config, and application objects are obtained.
  • This pageContext object further holds information regarding the directives provided to the JSP page, together with the page scope, buffering information, and the errorPageURL.
  • By using the pageContext object you can set attributes, get attributes and remove attributes that are present in the different scopes like as page, request, session, and application scopes which are given below as follows:
  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 “Lazyroar” 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("Lazyroar", PageContext.SESSION_CONTEXT);

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

  • Object obj = pageContext.getAttribute(“Lazyroar”, PageContext. REQUEST_CONTEXT);
  • Object obj = pageContext.getAttribute(“Lazyroar”, PageContext. PAGE_CONTEXT);
  • Object obj = pageContext.getAttribute(“Lazyroar”, PageContext. APPLICATION_CONTEXT);

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.

HTML




<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Lazyroar</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 

HTML




<%@ 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

HTML




<%@ 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.

RELATED ARTICLES

Most Popular

Recent Comments