Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSP:USEBEAN SCOPE:

 
trupti nigam
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
What is the difference between the different scopes available & when are they used?
for e.g te scope page/session/application/request what is main difference between them & under which condition are they used?

thanks,
Trupti
 
Brian Glodde
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Page is the same as request, the elements for the page are only available at the time the page is loaded, or upon each request.
Session means the information contained has visibility across multiple pages, for as long as the users session lasts. You might need this scope for a shopping cart-type application or wherever users need to persist their info across many pages.
Application means universally accessible. All clients using your web application have visibility. You might use this scope to share a database connection among your users.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64700
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Page is the same as request, the elements for the page are only available at the time the page is loaded, or upon each request.

Not exactly. Beans declared at request scope will be available in forwarded pages. Those declared at page scope will not.
hth,
bear
 
Brian Glodde
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean to say that when you forward to a different jsp page, you can retrieve reference to objects that were part of the request that you were forwarded from? When it comes to "scope", I really don't see much distinction between page and request. Can you illustrate, Bear?
TIA
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64700
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Absolutely. When you forward from one servlet to another (remember that JSPs are just servlets in drag) you remain within the context of the same request. That means that all request context elements, including both request parameters and attributes, are available within the forwarded page.
Therefore, beans that have been placed into the request scope are available across all forwarded pages.
This is actually a very important concept for the MVC/Model 2 architecture, as well as useful patterns like Front Controller.
Note, that this is not the case when a redirect is performed. In that case, the browser issues a new request, so context from the original request is no longer available.
hth,
bear
 
Brian Glodde
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I was thinking redirect. Thanks for the distinction!
 
Mahesh Mamani
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
This url should makes things clearer...
http://www.jguru.com/faq/view.jsp?EID=53309


Mahesh
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic