• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

page vs pageContext

 
Joseph Sweet
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

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

Thanks.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64620
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Joseph Sweet
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64620
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

[ March 19, 2007: Message edited by: Joseph Sweet ]
 
Joseph Sweet
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64620
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This article may be illumninating,
 
Joseph Sweet
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic