What is difference between page and pageContext in JSP pages?
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.
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. For example, to forward a request to another resource from in a JSP page, we can do that by using the pageContext
variable:pageContext.forward ("other.jsp");
Implicit Object | Class or Interface | Purpose, Function, or Uses |
---|---|---|
page | java.lang.Object | Refers to the generated Servlet class. Since page refers to the generated class and the class implements the Servlet interface, it is a valid cast it to Servlet . And the page variable could also be cast to JspPage orHttpJspPage since these two interfaces are derived from the Servlet interface and are implemented by the generated servlet class. For example,<%= ((Servlet)page).getServletInfo () %> |
pageContext | javax.servlet. jsp.PageContext | Used for storing and retrieving page-related information and sharing objects within the same translation unit and same request. Also used as a convenience class that maintains a table of all the other implicit objects. For example, public void _jspService ( HttpServletRequest request, HttpServletResponse response ) throws java.io.IOException, ServletException { ... try { ... application = pageContext.getServletContext(); config = pageContext.getServletConfig(); session = pageContext.getSession(); out = pageContext.getOut(); ... } catch (Throwable t) { ... } finally { ... } } |
Difference Between Page and PageContext?
page and pageContext are implicit variables.
Page : 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 :
PageContext is of type javax.servlet.jsp.PageContext.
pageContext class is an abstract class.
it do following things;
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("kiran.jsp");
3.store the references to implicit objects
No comments:
Post a Comment