Solved

Usage of Scope in Struts

Posted on 2004-10-06
6
9,430 Views
Last Modified: 2008-09-17
Hi,
   I am a little confused with when to use session and request scope for the forms when using Struts. I do know that session and request scope means. Can anyone please help me on this??

Thank you,
Stephen
0
Comment
Question by:JohnyStephen
  • 3
  • 2
6 Comments
 
LVL 32

Accepted Solution

by:
ldbkutty earned 25 total points
ID: 12246692
Its the same as the general understanding of "session" and "request". If you set scope="session" in your config file,
the variables and the methods of the form-bean is accessible to all the pages. If scope="request", its accessible only to the next page and not to the other pages. For example, user information of course makes sense to store in the session since you may have to pull that out in numerous places, but a list that is simply used to supply options for a drop down list on one page in a huge application does not need to persist throughout the whole user’s session(so "request" scope should be enough).
0
 
LVL 14

Assisted Solution

by:boonleng
boonleng earned 25 total points
ID: 12248030
Additional info:
"session" and "request" scope also behave a bit different. In scope "request" struts will call the method reset() of the form bean each time the action is performed, whereas scope "session" wont do reset automatically.
0
 
LVL 14

Expert Comment

by:boonleng
ID: 12248117
If you have a form split across several pages, then is better to use "session" scope, data in form bean can be stored in session and pass through pages. (best not to use hidden field to pass value across pages as it will raise security issue).
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:JohnyStephen
ID: 12256387
HI,
  Thanks for all the replies, but I have a doubt, In some places where dynavalidatorforms are used and if a form property is of type array  <form-property name="interests" type="java.lang.String[]" initial="null"/> (displaying dynamic fields), and then if I want to take the values back into the form when I sbmit, i read somewhere that the form should be session scoped and not request scoped, otherwise it won't be able to take the dynamic values. Can someone please explain this?

One more thing,
boonleng ,
U said if I use session scope for the bean the reset() method won't be called automatically, but I found that the reset() was getting called everytime, though it didn't have any effect on the form values. Even though i tried to set the values in the reset method, they were reverted back to the old values !!! :o(


Thanks,
Stephen
0
 
LVL 14

Expert Comment

by:boonleng
ID: 12273501
opps... sorry... my mistake :p

Actually both scope call the reset() method automatically, just that for scope "request", struts will create a new intance of the formbean for each new request, so you dont need to overwirte the reset() method to set the value back to default. For scope "session", you will need to overwrite the reset() method.

Coz struts create new formbean for each request, so you wont be able to get back the dynamic value once you click submit or hit struts validator error. You can either use scope "session" for the form bean or store the dynamic value in http session seperate from form bean.
0
 

Author Comment

by:JohnyStephen
ID: 12296338
Hi , I am not sure I got the answer I wanted, but I guess my question was also vague, and I got good answers for that. So I am splitting the points. Thanks guys, for your help.

Stephen
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article demonstrates probably the easiest way to configure domain-wide tier isolation within Active Directory. If you do not know tier isolation read https://technet.microsoft.com/en-us/windows-server-docs/security/securing-privileged-access/s…
This article summaries thoughts and ideas from two years of sustained use. It provides good reasoning to make the jump to Windows 10.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question