File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes session variables in struts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "session variables in struts" Watch "session variables in struts" New topic

session variables in struts

Poorav Chaudhari
Ranch Hand

Joined: Sep 17, 2002
Posts: 50
I am developing a web application using the struts framework on tomcat. Now, I
have one form that spans multiple pages. So to enable free user navigation
between pages (1st to 3rd to 4th to 2nd....) and maintain the form values i
store the form information in a form object with session scope. if the user,
after completing the 4 pages of the form presses save, i save the info into the
database and remove the session attribute from memory. but my problem is what
if the user decides in the middle of filling out the form to go to some other
page, and clicks on one of the other links. Now the session variable is still
in memory without any use for it. one way around this i see is to remove all
session variable my application creates within each action class except for the
ones that are required for that particular page. But this i don't see a
practical or an elegant solution. Can some one please help me with this
problem. suggestions and/or references to sites with helpful info on this will
be greatly appreciated.
Thank you.
Poorav Chaudhari

Poorav Chaudhari
David Hibbs
Ranch Hand

Joined: Dec 19, 2002
Posts: 374
Unless you are using persistent sessions, this shouldn't be a major concern. You could possibly run some javascript on the onUnload event to redirect the response to another page, forcing the session clearing. This would be somewhat awkward, though, and of course is not guaranteed to work.
Keeping the form data is much more friendly anyway. What if your user navigates away from your pages in order to locate some information for the form? You've just made them very frustrated by not saving their state.
So IMHO the best course of action is simply to let it be, and when the session expires it will expire the object. You could also clear the session on logout if you are truly concerned about leaving things in memory.

"Write beautiful code; then profile that beautiful code and make little bits of it uglier but faster." --The team, Newsletter 039.
I agree. Here's the link:
subject: session variables in struts
It's not a secret anymore!