June 21, 2011

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 requestresponse 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 pageContextvariable:

pageContext.forward ("other.jsp"); 

Implicit ObjectClass or InterfacePurpose, Function, or Uses
pagejava.lang.ObjectRefers 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 () %>
pageContextjavax.servlet. jsp.PageContextUsed 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 {
     ... 
    }
}

No comments:

Post a Comment

I'm certainly not an expert, but I'll try my hardest to explain what I do know and research what I don't know.

My Favorite Site's List

#update below script more than 500 posts