Solved

Usage of Scope in Struts

Posted on 2004-10-06
6
9,429 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Some of the SEO trends we might expect in 2017.
This article describes how to reset your Windows 10 password when you've forgotten it.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

856 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