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:
- page – Scope: PAGE_CONTEXT
- request – Scope: REQUEST_CONTEXT
- session – Scope: SESSION_CONTEXT
- application – Scope: APPLICATION_CONTEXT
Note: Page scope is the default scope in JSP.
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.
Object obj = pageContext.getAttribute("GeeksforGeeks", PageContext.SESSION_CONTEXT);
Correspondingly, this method can be used for more other scopes also, like as follows:
- Object obj = pageContext.getAttribute(“GeeksforGeeks”, PageContext. REQUEST_CONTEXT);
- Object obj = pageContext.getAttribute(“GeeksforGeeks”, PageContext. PAGE_CONTEXT);
- Object obj = pageContext.getAttribute(“GeeksforGeeks”, 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”.
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.
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.
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 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
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.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.