Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Form Bean - Session Scope

 
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When we say we have declared our form bean in session scope, few questions arise :
NOTE : A session is per client.
Assumptions :
a)Form bean object is in session.
b)In reset() method, we access the fields of form bean object, that is there in session.

Q-1) When is this session created and destroyed ?
Q-2) Will reset() method will be called for each user request? Is yes, then we usually reset the field values in reset(), then how come these values will be available thorought session ?
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Sandeep Vaid:
When is this session created and destroyed ?


It is created when Struts processes the first request from this particular user. It is destroyed when the invalidate() method is called on it, or when it is inactive for a specified timeout value.

Originally posted by Sandeep Vaid:
Will reset() method will be called for each user request? Is yes, then we usually reset the field values in reset(), then how come these values will be available thorought session ?


Yes, reset() is called for each user request. It's called immediately before Struts calls the setter methods to populate the properties from those entered in the HTML Form. The values are available throughout the session until new values are entered in another request. You shouldn't clear any values in the reset() method that you don't expect to be populated through an HTML form.
[ September 15, 2008: Message edited by: Merrill Higginson ]
 
Sandeep Vaid
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Yes, reset() is called for each user request. It's called immediately before Struts calls the setter methods to populate the properties from those entered in the HTML Form. The values are available throughout the session until new values are entered in another request. You shouldn't clear any values in the reset() method that you don't expect to be populated through an HTML form.



but this seems problematic :
1) What if the same form bean is used for action1 and is in request scope and the same form bean is used for action2 in session scope. Now first condition require you to reset it's fields while second scenario doesn't.

2)suppose i have a JSP page which contains firstName. The corresponding form bean is in session. Suppose first i enter "Sandeep" as firstname and submit the form. Now formbean.firstName="Sandeep". Now in JSP i made this firstName as '' (blank), then as the setter methods are called, formbean.firstName='' (blank).... This seems fine to me but what about problem 1) ?
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Sandeep Vaid:


but this seems problematic :
1) What if the same form bean is used for action1 and is in request scope and the same form bean is used for action2 in session scope. Now first condition require you to reset it's fields while second scenario doesn't.



I think, when we used same Form for two different action, in two different scope , we are creating two different object of same type Form !

May be I`m wrong , then that's leads the problem . May be we can try it first .
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Sandeep Vaid:
What if the same form bean is used for action1 and is in request scope and the same form bean is used for action2 in session scope.


Sagar is right. In this case, Struts would create two different instances of the form bean, one in request scope and the other in session scope. Because of the confusion that is caused by this, my suggestion is that this is something you should never do.
 
Sagar Rohankar
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Sandeep Vaid:

Q-2) Will reset() method will be called for each user request? Is yes, then we usually reset the field values in reset(), then how come these values will be available thorought session ?



For each new request coming from different session , different object of Form Bean is created .
 
Why am I so drawn to cherry pie? I can't seem to stop. Save me tiny ad!
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic