aspose file tools*
The moose likes Struts and the fly likes access objects in a session Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "access objects in a session" Watch "access objects in a session" New topic
Author

access objects in a session

steve francisco
Ranch Hand

Joined: Jan 26, 2005
Posts: 46
In JSP1 --> Action1 --> JSP2 --> Action2 --> JSP3-->Action3-->JSP4, we have
Form1Bean, Form2Bean, Form3Bean matching JSP1, JSP2, JSP3.

We need to carry over the information from JSP1(Form1Bean) to 2nd and 3rd page and more.. It seems the easiest way is to put scope="session" in the "action" tag in config-struts.xml. But besides doing that, do I need to explicitly write "session.setAttribute("Form1", Form1Bean);" (or explicitly do setAttribute() for every object I want to carry over pages) ? I am wondering if I only put "scope=session" in xml and skip doing this "session.setAttribute", can I still access "Form1Bean" in "Action3" class ?
alan do
Ranch Hand

Joined: Apr 14, 2005
Posts: 354
you can place the form bean in session by specifying the action mapping's 'scope' attribute to 'session'...e.g scope="session". the form bean mapped to the action mapping will be placed in the session scope. remember to clear the form values out when you are done with them.


-/a<br />certified slacker...yes, my last name is 'do' - <a href="http://www.luckycouple.com" target="_blank" rel="nofollow">luckycouple.com</a>
steve francisco
Ranch Hand

Joined: Jan 26, 2005
Posts: 46
Originally posted by alan do:
you can place the form bean in session by specifying the action mapping's 'scope' attribute to 'session'...e.g scope="session". the form bean mapped to the action mapping will be placed in the session scope. remember to clear the form values out when you are done with them.


thanks. since we usually don't need ALL of the information from a form, I want to selectively store some information and carry them over pages. Thus, I want to use session.setAttribute() in the Action classes. It should work, right ?
alan do
Ranch Hand

Joined: Apr 14, 2005
Posts: 354
as long as you clean out ALL the session values upon a successful action, yes. be careful not to over storing objects in session. session is not where large amount of data should be persisted. this impacts performance severely if improperly used.
Srilakshmi Vara
Ranch Hand

Joined: Jul 21, 2004
Posts: 169
Hi,

Instead of using multiple forms, try to use a single bean which holds all the fields over jsp1,jsp2.. , use the single bean in session scope it works perfect.

Coming to cleaning, create another action which cleans the form before displaying the form for first time.


Srilakshmi
alan do
Ranch Hand

Joined: Apr 14, 2005
Posts: 354
putting everything into 1 form bean will add major complexity to validation, especially if validation is declarative. doing so only make sense for multi-page forms, not for a case where ONLY selective values are passed from 1 form to another.
Artemesia Lakener
Ranch Hand

Joined: Jun 21, 2005
Posts: 162
Originally posted by alan do:
as long as you clean out ALL the session values upon a successful action, yes. be careful not to over storing objects in session. session is not where large amount of data should be persisted. this impacts performance severely if improperly used.


Interesting. What if the large object is stored in HttpRequest ? session costs memory resource, but doesn't HttpRequest cost too ?
alan do
Ranch Hand

Joined: Apr 14, 2005
Posts: 354
objects in requests are stored once then destroyed per each completed request and DO NOT reside 'persistently' in the server memory. server memory is therefore 'reused' at each request. imagine having thousands of same objects per thousands of user sessions, each with large amount of data...all stored in your server memory...hence the necessary clean up when you're done with objects in session.
 
 
subject: access objects in a session