It's not a secret anymore!
The moose likes Servlets and the fly likes Request Scope and Session Scope Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Request Scope and Session Scope" Watch "Request Scope and Session Scope" New topic

Request Scope and Session Scope

Dale DeMott
Ranch Hand

Joined: Nov 02, 2000
Posts: 515
I'm working w/Servlets and JSP pages and want to be efficient with my memory. My question deals with request scope and session scope. If some information needs to only go from one page to the next, shouldn't I put this information in the request scope. And if I DO put this in the request scope, will this survive when I hit the back button to this page 5 pages later. Example: Page A requests an item price for my toy truck. Its $15434.00. (Nice truck huh?) Anyway, this information is put into the request scope. The informaiton is used on page B. I go on and go to page C then D then E and decide I need to hit the back button all the way back to page B. Will that information for the price of the truck still be there?
Dale DeMott
[ January 18, 2002: Message edited by: Dale DeMott ]

By failing to prepare, you are preparing to fail.<br />Benjamin Franklin (1706 - 1790)
Michael Ernest
High Plains Drifter

Joined: Oct 25, 2000
Posts: 7292

Hi Dale -
I'm going to transfer this question to the Servlets forum. Should be a truck of guys over there who can answer this quickly.

Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
i think for what u describing a session scope is better .
request wont do there, and for your question i think it wont survive the 5 pages back.
request scope will be during that request only.
if u forward that request to other servlets or jsp it will stay.
but for other servlets and jsp that u havent actually forwarded that request (the same one) they wont have those variable.
my siggestion is to try it yourself.
put somthn in request move it to fiew other pages and hit the back button.
actully ill try it later at home and post the correct answer.
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

whenever i think about a user using the back and forwards buttons on my web site 'applications', it just gives me a headache. Truly, I wish we could turn them off and on for purposes of 'flow control'.
Manohar Karamballi
Ranch Hand

Joined: Jul 17, 2001
Posts: 227
There's one more issue u have to handle if ur providing back button functionality..
i.r regarding getting latest data..
Ofcourse how crucial this issue depends on ur application..
But one thing..session scope is must for this case..
Consider following scenario..
In shopping application i have 3 links..
Link1 : Add Item
Link2 : Remove Item
Link3 : Show all Added Items.
Now i add three items A,B,C to my shopping cart..
Now i list all items.. By session scoped object u List A,B,C..
Now i delete Item B.
Now i Press back button ..
My listed items will be displayed as A,B,C which is wrong.
But how important this type of scenario depends upon ur application.
Dale DeMott
Ranch Hand

Joined: Nov 02, 2000
Posts: 515
I see what you mean. The situation I'm in is that I'm doing a very large query against an x500 database and wish to conserve memory. Right now the data that is queried is saved in the session scope. I wish to move this to the request scope. I also want the application to work when I hit the back button. If I put this information in the request scope AND pass the query via the URL, would this force the servlet to query the system again?
Any ideas???
Dale DeMott
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
I think you should not try to get tricky here. Leave the data in session scope - also store the query in the session. If you reach a point where you think the large data can safely be discarded, you can force the session to drop it with removeAttribute( "dataname" )- then if you need to rebuild it due to some arcane sequence of events, you can use the stored search string.
I agree. Here's the link:
subject: Request Scope and Session Scope
It's not a secret anymore!