JSP - SPLessons
boy2 Chapter 23

JSP Page Object

JSP - SPLessons

JSP Page Object

shape Description

JSP Page Object generates Servlet Instance itself. The page instance of the JSP page process by a servlet for the current request. It is same as “this” keyword in Java. This page object is of variable type java.lang.object. If one wants to access any methods of the Servlet through the page, it must type cast to the Servlet. This page is used for the entire JSP page.This page object is very rarely used.The page object is not good to use as it consumes large memory.
The Page object denotes the JSP page, used for calling any instance of a Page’s servlet. It has page scope. First type cast the servlet before accessing any method of the servlet through the page.

PageObject

shape Example

Following is the best example of JSP Page Object.

pageobject.jsp

     <html>
        <body>
        <strong>HttpServlet Name</strong>:<%=page.getClass().getName() %>
        </body>
     </html>

Here getclass() used to retrieve the class JSP file and getName() used to get the name of the file.

Output:

Output will be as follows, where Http servlet Name generated with the class name and server link.

PageContext is used to access page attributes and also to access all the namespaces associated with a JSP page. It provides a single API to manage the various scoped attributes. PageContext also provides access to several page attributes like including some static or dynamic resource. It does the following things.

  • provide convenience methods to get and set attributes in diff scopes.
  • provide convenience methods for transfering requests to other resources in the web application
    void include(String relativeURL) & void forward(String relativeURL)
  • Store the references to implicit objects

Summary

shape Key Points

  • JSP Page Object – Page object is like synonym of this object.
  • JSP Page Object – Page object is an implicit object and bearing to the instance of the page.
  • page is of class java.lang.Object,and it refers to instance of generated servlet.