JSP pageContext
The object pageContext is used to access all the information of JSP page. It is an instance of javax.servlet.jsp.PageContext.
The scope of pageContext can be defined explicitly. The object is valid up to the specified scope only. Several layers in which pageContext can be accessed are as follows: -
- SESSION_SCOPE
- REQUEST_SCOPE
- PAGE_SCOPE
- APPLICATION_SCOPE
Methods of pageContext
The various methods of pageContext are as follows: -
- void setAttribute(String attribute_name,Object attribute_value,int scope):- This method is used to set the attribute.
- Object getAttribute(String attribute_name,int scope):- This method return the attribute of object type.
- Object removeAttribute(String attribute_name,int scope):- This method is used to remove the attribute.
- Object findAttribute(String attribute_name):- This method search the attribute passed within its bracket.
Example of pageContext
This is a simple example that defines the way of set and get the various attributes.
index.jsp
<html> <head> <title>Tutorial and Example</title> </head> <body> <h1 align="center">Tutorials:</h1> <h2 align="center"> <% pageContext.setAttribute("Tutorial1","HTML",PageContext.SESSION_SCOPE); pageContext.setAttribute("Tutorial2","Servlet",PageContext.REQUEST_SCOPE); pageContext.setAttribute("Tutorial3","JSP",PageContext.PAGE_SCOPE); pageContext.setAttribute("Tutorial4","JavaScript",PageContext.APPLICATION_SCOPE); String name1=(String)pageContext.getAttribute("Tutorial1",PageConteOutput xt.SESSION_SCOPE); String name2=(String)pageContext.getAttribute("Tutorial2",PageContext.REQUEST_SCOPE); String name3=(String)pageContext.getAttribute("Tutorial3",PageContext.PAGE_SCOPE); String name4=(String)pageContext.getAttribute("Tutorial4",PageContext.APPLICATION_SCOPE); out.println(name1); out.println("<br>"+name2); out.println("<br>"+name3); out.println("<br>"+name4); %> </h2> </body> </html>Output: