File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes Maintaining State between different calls to jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Maintaining State between different calls to jsp" Watch "Maintaining State between different calls to jsp" New topic

Maintaining State between different calls to jsp

Pallavi Sikdar

Joined: Jan 16, 2004
Posts: 12
Hi all,
Can anyone tell me what is the best way to maintain state between different client requests?
I used hidden form fields ,<jsp:forward > and kept setting the variable in the request attributes:-
Problem with this was whenever a new form was submitted the request
attributes were lost and needed to be reassigned via hidden form fields.
Should i use session.setAttribute instead to pass around variables?
Or is cookies a better option? How should i decide?
Frank Carver

Joined: Jan 07, 1999
Posts: 6920
The recommended way to do this is to store you values in the session context. In most cases the session mechanism uses cookies "under the hood".

Read about me at ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Amigo Mine

Joined: Feb 15, 2004
Posts: 5
I agree with Frank.
usually its best to avoid heavy requestobjects.
You can use session.setAttribute("foo") tp maintain the state. In case u have a number of parameters u better encapsulate them and then add it to the session context.
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63844

Welcome to the Ranch "AmigoM"!
You'll find this forum a great place to seek help on JSP pages, and there aren't many rules you'll have to worry about, but one is that proper names are required. Please take a look at the JavaRanch Naming Policy and change your display name to match it.
In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.
Additionally, JavaRanch is a community of people from all over the world, many of who are not native English speakers. While using abbreviations like "u" instead of spelling out "you" is convenient when text messaging your friends on a cell phone or chat room, it presents an extra challenge to those that are already struggling with English. I would like to ask for your help in making the content of JavaRanch a little easier to read for everybody that visits here by not abbreviating these words.
JSP Forum Bartender

[Asking smart questions] [About Bear] [Books by Bear]
Dhondiba Joshi
Ranch Hand

Joined: Jan 23, 2004
Posts: 39
Hi Pallavi
This is one question that should definitely occur in every web developers mind.
Adding attribute in URLs is unsafe if method is not post.Also threre is limit to its size.
Hidden attributes again add up to the request object and as you realized request object is valid only for one 'request' (Sounding odd!)

Cookies if disabled by client browser wont work. Also user may manually delete them.
Best way is to put it in session object. But hey! wait dont make seesion object too heavy. First design your application such that minimum burdon is put on the session hourse which runs from here and there ;-)

Dhondiba Joshi
Ranch Hand

Joined: Jan 23, 2004
Posts: 39
In conjuction with prevoius mail. I didn get what you wanted to say abt
<jsp:forward> will like to know it .
I agree. Here's the link:
subject: Maintaining State between different calls to jsp
It's not a secret anymore!