aspose file tools*
The moose likes JSP and the fly likes page vs pageContext Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "page vs pageContext" Watch "page vs pageContext" New topic
Author

page vs pageContext

Joseph Sweet
Ranch Hand

Joined: Jan 29, 2005
Posts: 327
Hi Guys,

Anyone really knows what the difference is between page and pageContext in JSP???

Thanks.


We must know, we will know. -- David Hilbert
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

page is merely a synonym for the JSP instance (the this reference) and is rarely used.

pageContext is an instance of PageContext and is the means by which JSPs obtain information about their environment.
[ March 18, 2007: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Joseph Sweet
Ranch Hand

Joined: Jan 29, 2005
Posts: 327
Thanks Sheriff.

When I use:

<jsp:useBean id="name" class="package.class" />

What is the difference between scope="page" and scope="request" ? it looks like both "page" and "request" have the scope of the service method. Am I wrong?
[ March 18, 2007: Message edited by: Joseph Sweet ]
Kevin Liu
Greenhorn

Joined: Mar 17, 2007
Posts: 10
scope="page" means your JavaBean instance is available just in the current .jsp file
scope="request" means your JavaBean instance can be accessed through the request
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

Page scope is limited to the scope of the current JSP. Request scope is shared across all resources participating in the request. This can include any servlet controller that might have gained control prior to the JSP -- a very common pattern to employ -- and (with a bit of hand-waving) any included JSPs. ALthough it's rare to forward from a JSP, request scope would also be shared with the forwarded resources.
Joseph Sweet
Ranch Hand

Joined: Jan 29, 2005
Posts: 327

[ March 19, 2007: Message edited by: Joseph Sweet ]
Joseph Sweet
Ranch Hand

Joined: Jan 29, 2005
Posts: 327
I don't understand your last message...



Request scope is shared across all resources participating in the request. This can include any servlet controller that might have gained control prior to the JSP -- a very common pattern to employ -- and (with a bit of hand-waving) any included JSPs.



Not sure what you mean by a servlet controller and how it gains control over the JSP..

Any code example to this?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Within one request/response cycle the context can be passed between multiple servlets and/or jsp with requestDispatcher.forward(resource) or with the jsp:forward action.
MVC apps always separate the various concerns by having a servlet receive the request, create java objects and call their methods to perform the business logic, and then forward to the JSP for formatting.

In a case such as this, beans generated in the servlet can be bound to request scope to be made available to the JSP.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

This article may be illumninating,
Joseph Sweet
Ranch Hand

Joined: Jan 29, 2005
Posts: 327
Thank you for the article.

So I have a way to instantiate beans in a JSP and define their scope to be the request's scope.

How do I define the scope from within a servlet?
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
From within a servlet, you will add you bean/object to the appropriate scope(request, session,etc) with setAttribute() as in request.setAttribute(yourObjectsName, yourobject). Then within your JSP it will be accesed from the appropriate scope. For obvious reasons, the page scope does not exist for a servlet.

Originally posted by Joseph Sweet:
Thank you for the article.

So I have a way to instantiate beans in a JSP and define their scope to be the request's scope.

How do I define the scope from within a servlet?


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: page vs pageContext