JSP - SPLessons

Chapter 24

JSP Page Context Object

JSP Page Context Object

JSP Page Context Object is used to store and retrieve the page-related information and sharing objects. PageContext is an instance of javax.servlet.jsp.PageContext. PageContext is used to  findAttribute, setAttribute, getAttribute and removeAttribute and it has the following scope.

Methods of PageContext

findAttribute(String AttributeName): This method is used to search the attributes like the page, session, request, application. Return type is an object. If there is no attribute it returns null.

Syntax

pageContext.findAttribute(“name of attribute”);

getAttribute(String AttributeName, int scope):This method is used to get the attribute with specified scope. This is same as findAttribute() method but getAttribute look for specific scope. It returns the object, if there is no attribute, it returns null.
Syntax
Object object = pageContext.getAttribute(“Attribute name”, PageContext.SESSION_CONTEXT);

removeAttribute(String AttributeName, int scope):This method is used to remove the attribute from given scope and there is no return type of this method.
Syntax
pageContext.removeAttribute(“Attribute Name”,PageContext.REQUEST_CONTEXT);

setAttribute(String AttributeName, Object AttributeValue, int scope):This method is used to set the name of the attribute, attribute value and scope of that object. This method has no return type.
Syntax
pageContext.setAttribute(“AttributeName”,”AttributeValue”,
PageContext
APPLICATION_CONTEXT);

Following is an example to JSP Page Context Object.

 indexpagecontext.html

<html>
   <body>
<form action="pageContext.jsp">
             Name:    <input type="text" name="Name"/></br>
             FullName:<input type="text" name="fullName"/></br></br>
                      <input type="submit" value="submit"/>
  </form>
</body>
</html>

Here the developer just created two text boxes such as Name and FullName and also created submit button.
 pageContext.jsp

<html>
<body>
<form action="getPageContext.jsp">
<% 
String Name = request.getParameter("Name"); 
String fullName = request.getParameter("fullName");
 out.println("Hello "+ Name+" "); 
pageContext.setAttribute("Name", Name, PageContext.SESSION_SCOPE); 
pageContext.setAttribute("fullName", fullName, PageContext.SESSION_SCOPE);
 %>
<input type="submit" value="Click Here"/>
</form>
</body>
</html>

request.getParameter() method is used to retrieve the details which are placed in HTML page. pageContext.setAttribute() method is used to set the name of the attribute, attribute value and scope of that object. This method has no return type.
getPageContext.jsp

<html>
<body>
<% 
String Name = (String) pageContext.getAttribute("Name", PageContext.SESSION_SCOPE); 
String fullName = (String) pageContext.getAttribute("fullName", PageContext.SESSION_SCOPE); 
out.println("Hai "+ Name+" "); 
out.println("This is your fullname :"+fullName); 
%>
</body>
</html>

getAttribute(String AttributeName, int scope) method is used to get the attribute with specified scope.

Output
Output will be as follows with two text fields and one submit button.

After enter the text field names, it displays Hello followed by name then click on button that click here.

The final output will be as follows.

Summary

  • JSP Page Context Object is used to set, get and remove attributes from the scope.
  • JSP Page Context Object – Page scope is the default scope.