Solved

Usage of Scope in Struts

Posted on 2004-10-06
6
9,431 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

We asked our MSP customer base what their favorite tools were and how they help them serve clients. We focused our questions on favorite tools in the following categories: >PSA tools >RMM tools >Alert management tools >Communication tools and Mo…
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

738 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