Solved

Usage of Scope in Struts

Posted on 2004-10-06
6
9,424 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:JohnyStephen
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Veeam Backup & Replication has added a new integration – Veeam Backup for Microsoft Office 365.  In this blog, we will discuss how you can benefit from Office 365 email backup with the Veeam’s new product and try to shed some light on the needs and …
In this article, I will show you HOW TO: Create your first Windows Virtual Machine on a VMware vSphere Hypervisor 6.5 (ESXi 6.5) Host Server, the Windows OS we will install is Windows Server 2016.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now