pageContext is an implicit object of type PageContext class.
It can be used to get,set and remove attributes from these scopes-
Why pageContext??
Popular Methods of implicit object pageContext-
1.Object findAttribute (String AttributeName)
It searches for the attribute at all the level in the order specified below
2.Object getAttribute (String attributename, int scope)
This one is also kind of specific version of above one.
It only simply checks in the specified scope.
String username = (String)pageContext.getAttribute("user", PageContext.SESSION_CONTEXT);
in place of PageContext.SESSION_CONTEXT,we can specify other scopes as well.
3. void removeAttribute(String attributename, int scope)
This is used to remove an attribute from the specified scope.
pageContext.removeAttribute(“user”, PageContext. APPLICATION_CONTEXT);
4. void setAttribute(String attributename, Object attributevalue, int scope)
Again as name suggests,it sets the attribute for the specified scope.
pageContext.setAttribute(“user”, “Prince”, PageContext. APPLICATION_CONTEXT);
Some Key Points
void forward (String url)-Forwards the request to another resource defined by the relativeURL.
example-
pageContext.forward ("anyOtherResource.jsp");
It can be used to get,set and remove attributes from these scopes-
- page
- request
- session
- application
Why pageContext??
- a single API to manage the various scoped namespaces
- a mechanism to obtain the JspWriter for output
- a mechanism to manage session usage by the page
- mechanisms to forward or include the current request to other active components in the application
- a mechanism to handle errorpage exception processing
Popular Methods of implicit object pageContext-
1.Object findAttribute (String AttributeName)
It searches for the attribute at all the level in the order specified below
- page
- request
- session
- application
2.Object getAttribute (String attributename, int scope)
This one is also kind of specific version of above one.
It only simply checks in the specified scope.
String username = (String)pageContext.getAttribute("user", PageContext.SESSION_CONTEXT);
in place of PageContext.SESSION_CONTEXT,we can specify other scopes as well.
3. void removeAttribute(String attributename, int scope)
This is used to remove an attribute from the specified scope.
pageContext.removeAttribute(“user”, PageContext. APPLICATION_CONTEXT);
4. void setAttribute(String attributename, Object attributevalue, int scope)
Again as name suggests,it sets the attribute for the specified scope.
pageContext.setAttribute(“user”, “Prince”, PageContext. APPLICATION_CONTEXT);
Some Key Points
- It stores referece to the implicit objects.
... application = pageContext.getServletContext (); config = pageContext.getServletConfig (); session = pageContext.getSession (); out = pageContext.getOut (); ...
- Provides convenience methods to get and set attributes in different scopes.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <%-- Saving the data into various scopes --%> <% pageContext.setAttribute("attribute1", "value1"); // PAGE_SCOPE is the default pageContext.setAttribute("attribute2", "value2", PageContext.PAGE_SCOPE); pageContext.setAttribute("attribute3", "value3", PageContext.REQUEST_SCOPE); pageContext.setAttribute("attribute4", "value4", PageContext.SESSION_SCOPE); pageContext.setAttribute("attribute5", "value5", PageContext.APPLICATION_SCOPE); %> <%-- Retrieving the data from scopes --%> Arrribute1 is <%= pageContext.getAttribute("attribute1") %> Arrribute2 is <%= pageContext.getAttribute("attribute2", PageContext.PAGE_SCOPE) %> Arrribute3 is <%= pageContext.getAttribute("attribute3", PageContext.REQUEST_SCOPE) %> Arrribute4 is <%= pageContext.getAttribute("attribute4", PageContext.SESSION_SCOPE) %> Arrribute5 is <%= pageContext.getAttribute("attribute5", PageContext.APPLICATION_SCOPE) %> </body> </html>output screen
- Transferring requests to other resources in your application:
void forward (String url)-Forwards the request to another resource defined by the relativeURL.
example-
pageContext.forward ("anyOtherResource.jsp");