This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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?
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.
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
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
Joined: Jun 27, 2001
Yes, I was thinking redirect. Thanks for the distinction!