This week's book giveaway is in the General Computing forum.
We're giving away four copies of Learning Regular Expressions and have Ben Forta on-line!
See this thread for details.
Win a copy of Learning Regular Expressions this week in the General Computing 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:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Creating a bean in session scope  RSS feed

 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the servlet, I am setting an attribute in the request object. In the JSP, trying to create a new attribute in the session scope. And also, trying to print the above attributes in the request and session scope using EL.

SuperCreature is the base class of Person and Dog. And, Person has a Dog.




At step 1, trying to create a new bean in 'session' scope. A bean named 'person' already exists in 'request' scope as set by the servlet.

At step 2, printing the bean using EL that is in the sessionScope and at step3, printing the bean in the requestScope.

At 3, I see the expected result ie. the values set by the servlet.
but at 2, I don't see the values set by the Standard action tag <jsp:useBean>. Any ideas?
 
Ranch Hand
Posts: 510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would you please post the code for com.example.SuperCreature.

Thank you
 
Meena R. Krishnan
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here it is..
 
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using setProperty in the body of useBean does not mean : "set the property of this bean". No, it still means "set the property of the bean defined by "name". Both setProperty and getProperty use pageContext.findAttribute to find the bean they are referring to. So the scopes will be searched in the following order : page, request, session, and application. Your bean in the request will then be found before the bean in the session.
[ September 06, 2007: Message edited by: Christophe Verre ]
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note also that getProperty will be called only once : when the bean is created in the session scope. Once the bean has been set in the session, its body will not be executed, so getProperty will not be called.
 
Meena R. Krishnan
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>>So the scopes will be searched in the following order : page, request, session, and application.
So, what is the purpose of 'scope' in the <jsp:useBean ... ?

My understanding from HFSJ is, when the scope is given and if the bean could not be found (provided 'class' is also given) in the given scope, a new bean will be created and set, if <jsp:useBean> has a setproperty in its body.

I rewrote that part as below:


this time, I am setting a new attribute 'person2' (which does not exist in the request or anywhere). This time it worked when I tried to print the attribute from the SESSION as below:


The above code printed the values as 'SAName'. How come?
 
Meena R. Krishnan
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Note also that getProperty will be called only once : when the bean is created in the session scope. Once the bean has been set in the session, its body will not be executed, so getProperty will not be called."

But, isn't the setProperty also in the body of the action?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes both of them will be executed when the bean is first created in the session.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

So, what is the purpose of 'scope' in the <jsp:useBean ... ?


To make a bean in a particular scope ! You usually don't give the same name to different beans. Even if you do, you could use EL to access bean properties, or JSTL c:set to set the values.

The above code printed the values as 'SAName'. How come?


There's no bean called person2 in the request.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!