File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes pageContext vs request vs page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "pageContext vs request vs page" Watch "pageContext vs request vs page" New topic
Author

pageContext vs request vs page

Renata fonseca
Ranch Hand

Joined: Mar 15, 2002
Posts: 48
pageContext, request and page are implicit objects... I can use these objects to share objects between pages, but when it's a good idea to use pageContext, request or page? What's the difference?
boyet silverio
Ranch Hand

Joined: Aug 28, 2002
Posts: 173
hi Renata,
maybe you could get something from the following.
the pageContext has ways in getting a handle of different scope container objects which is useful when creating tag handler classes (THC ) for custom tags. In THCs, the 'request', 'session', and 'application' objects are not readily available. So if one wants to make use of these in the THC, one could do the ff:
pageContext.getRequest()
pageContext.getSession()
pageContext.getServletContext().
for the request object, i think it's the only scope container which could capture parameters (not just attributes) passed from a previous page by using the request.getParameter(...) method.
as for the page, which represents an instance of the the jsp'servlet, this is rarely used because it could be omitted in a jsp as follows
e.g.
<% long lng = ((HttpServlet)page).getLastModified(request); %>
which is the same as
<% long lng = this.getLastModified(request); %>
or
<% long lng = getLastModified(request); %>
hope this helps.
Rama Raghavan
Ranch Hand

Joined: Aug 22, 2001
Posts: 116
pageContext, request and page are implicit objects...
Yes..these are implicit objects, which means you can reference respective objects by name instead of calling a (get) method.
However, in order to share objects between pages you must look at the scope of objects.
Use request, page, session or application implicit objects as required.
request: lasts for the life of that (and only that) request. A second request from client (browser) or a sendRedirect is a new request.
page: lasts for the life of that page. Makes more sense when objects are shared from within included pages
session: survives multiple requests from the same client, but not visible to other clients
application: lasts for the life of the application, and accessible by all client requests.
pageContext has convenience methods to retrieve an object from any of the above scopes (see API). You do not bind objects to pageContext object.
Rama
[ February 21, 2003: Message edited by: Rama Raghavan ]

Rama Raghavan<br />SCJP2<br />SCWCD
Renata fonseca
Ranch Hand

Joined: Mar 15, 2002
Posts: 48
Thanks.. but I'm still confused...
1- I'd like to know when I have to use "page" instead of "request".
2 - By the way... Is "page" an instance of pageContext or this?
3 - If I use page.setAttribute("xx",...), can I retrieve the attribute from another page, in the same request by using page.getAttribute("xx")?
[ February 21, 2003: Message edited by: Renata fonseca ]
boyet silverio
Ranch Hand

Joined: Aug 28, 2002
Posts: 173
hi Renata,
maybe my examples above are a little too soon based on your subsequent query. Anyway, let's see if i can clarify.

1- I'd like to know when I have to use "page" instead of "request".

the implicit object 'page' is different from 'request'. 'page' is of reference type Object class. But it is an instance of the jsp/servlet so you can cast it to HttpServlet and use HttpServlet's methods. 'request' on the other hand is usually of type HttpServletRequest, which extends ServletRequest. You can get an initial idea of when to use the objects, and their differences by looking at each of the type's (HttpServlet vs. HttpServletRequest and ServletRequest) methods. E.g. for request, request.getParameter...for page, page.getLastModified, etc.

2 - By the way... Is "page" an instance of pageContext or this?

A jsp is translated by the servlet engine (e.g. tomcat) into a servlet. There you will see the 'page' declared as follows:
Object page=this;

which means 'page' refers to 'this', where 'this' refers to the servlet instance. So 'page' is not an instance of PageContext.

3 - If I use page.setAttribute("xx",...), can I retrieve the attribute from another page, in the same request by using page.getAttribute("xx")?

If you look in the API at the reference and instance type of 'page' (Object and HttpServlet) you can not find a setAttribute method, so you can't use this.
If you want to use setAttribute from your page, the ff is an example
<% session.setAttribute("yourObj", yourObj); %>
and get it in the receiving page as
<% YourObj yo=(YourObj)session.getAttribute("yourObj"); %>
By the way you could also use pageContext, try exploring the ff later
<% pageContext.setAttribute("yourObj", yourObj, PageContext.SESSION_SCOPE); %>
For the request object, you could set objects to it using request.setAttribute and retrieve it with request.getAttribute, but the navigation from one page to another should be thru a redirect e.g. <jsp:forward...> you could look up how this is used...
[ February 21, 2003: Message edited by: boyet silverio ]
Engin Okucu
Ranch Hand

Joined: Feb 09, 2002
Posts: 174
Hi Renata ,
Keep in mind for the difference between request and page is as follow :
- Page : You can access your parameter ONLY in
'servlet1' where you submitted your
form. It is like 'this' in Java. It
means that it refers to the actual
object. The scope is limited to the
actual servlet.
In another page(servlet2), you won't
be able to access your paramters
if you do a forward to servlet2 from
servet1.

- Request : if you submit your form to a servlet1
and from this servlet1 you want to
forward to servlet2, you
can STILL access to the parameters
here. The parameter's access is not
limited in servlet1.
I think you see the difference better.
Thanks.
Renata fonseca
Ranch Hand

Joined: Mar 15, 2002
Posts: 48
Thanks for the reply but I don't believe that page is like "this object". The page scope uses "pageContext"!!!

Objects in the page scope are accessible only in the translation unit in which they are
defined. They do not exist outside the processing of a single request within a single
translation unit. These objects are maintained as attribute-value pairs by an instance
of a concrete subclass of the abstract class PageContext. In a JSP page, this instance
is available in the form of the implicit object pageContext.
boyet silverio
Ranch Hand

Joined: Aug 28, 2002
Posts: 173
originally posted by Renata fonseca
...I don't believe that page is like "this object"...

A servlet translation of a jsp will indicate that the page 'object' is 'this' object thru the following line
'Object page = this;'

...The page scope uses "pageContext"!!!

In a way, you can say that. Just don't confuse the 'page scope' (which sort of means the 'confines of the jsp/servlet') with the implicit object 'page' of type 'Object' which is an instance (this) of the jsp/servlet, and the implicit object 'pageContext' which is of type 'PageContext'.
In case you haven't tried it yet, try coding a jsp which contains a jsp:useBean tag e.g.
<jsp:useBean id="yourObject" class="com.YourObject" scope="page" />
deploy it in your web app, run it, then examine the code in the corresponding servlet translation. It will substantiate those mentioned above. A whole lot more can also be learned there.
Engin Okucu
Ranch Hand

Joined: Feb 09, 2002
Posts: 174
Renata,
I said : it is like 'this' keyword in Java.
Then when you use 'this' in java , it means you talk about the current instance of object and you not refer outside from this page.It is just an example for understanding the purpose of 'page' implicit object.
Thank you.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: pageContext vs request vs page