JSP implicit object "pagecontext"


 

JSP implicit object "pagecontext"

In this Section,we will discuss about "pagecontext" implicit object and their uses with an example.

In this Section,we will discuss about "pagecontext" implicit object and their uses with an example.

JSP IMPLICIT OBJECT  pagecontext

A PageContext instance provides access to all the namespaces associated with a JSP page, provides access to several page attributes, as well as a layer above the implementation details.

The PageContext provides a number of facilities to the page/component author and page implementor, including:

  • a single API to manage the various scoped namespaces
  • a number of convenience API's to access various public objects
  • a mechanism to obtain the JspWriter for output
  • a mechanism to manage session usage by the page
  • a mechanism to expose page directive attributes to the scripting environment
  • mechanisms to forward or include the current request to other active components in the application
  • a mechanism to handle "errorpage" exception processing

Difference between page & pagecontext

page : The page and pageContext are implicit JSP Objects. The page object represents the generated servlet instance itself, i.e., it is same as the "this" keyword used in a Java file. As a result, you do not typically know who the super class is, and consequently do not normally make use of this object or its methods.

 page is of class java.lang.Object and it refers to instance of generated servlet.It is declared as

Object page this;

// this refers to the instance of this servlet

page cannot be used to directly call the servlet methods.

1) < page.getServletInfo() >

Error bcoz page is java.lang.Object type

2) < ((servlet)page).getServletInfo() > <----OK:typecast

3) < this.getServletInfo() > <-------OK

pageContext :

The pageContext object represents the environment for the page, containing useful information like page attributes, access to the request, response and session objects, as well as the JspWriter referenced by out. This object also has methods for including another URL's contents, and for forwarding or redirecting to another URL.

pageContext is of type 'javax.servlet.jsp.PageContext.'

pageContext class is an abstract class.

The key things it do are:

1.provide convenience methods to get and set attributes in diff scopes.

2.provide convenience methods for transfering requests to other resources in the web application

void include(String relativeURL) & void forward(String relativeURL)

Ex; pageContext.forward( ankit.jsp );

3.store the references to implicit objects.

Example using methods "setAttribute" & "getAttribute" of "pagecontext"

<b><font color="purple">

<%

// Check if attribute has been set

Object o = pageContext.getAttribute("com.mycompany.name1", PageContext.PAGE_SCOPE);

if (o == null) {

out.println("'com.mycompany.name1' is not 'null'");

}

// Save data

0

pageContext.setAttribute("com.mycompany.name0", "value0"); // PAGE_SCOPE is the default

pageContext.setAttribute("com.mycompany.name1", "value1", PageContext.PAGE_SCOPE);

pageContext.setAttribute("com.mycompany.name2", "value2", PageContext.REQUEST_SCOPE);

1

pageContext.setAttribute("com.mycompany.name3", "value3", PageContext.SESSION_SCOPE);

pageContext.setAttribute("com.mycompany.name4", "value4", PageContext.APPLICATION_SCOPE);

out.println("<br>");

2

out.println("<br>");

out.println("SHOWING VALUES OF ATTRIBUTE :-");

%>

3

<br>

<%-- Show the values --%>

<%= pageContext.getAttribute("com.mycompany.name0") %> <%-- PAGE_SCOPE --%>

4

<br>

<%= pageContext.getAttribute("com.mycompany.name1", PageContext.PAGE_SCOPE) %>

<br>

5

<%= pageContext.getAttribute("com.mycompany.name2", PageContext.REQUEST_SCOPE) %>

<br>

<%= pageContext.getAttribute("com.mycompany.name3", PageContext.SESSION_SCOPE) %>

6

<br>

<%= pageContext.getAttribute("com.mycompany.name4", PageContext.APPLICATION_SCOPE) %>

</font></b>

7

OUTPUT :

Download Source Code

8

Ads