• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Creating a bean in session scope

 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here it is..
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
You can thank my dental hygienist for my untimely aliveness. So tiny:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic