wood burning stoves 2.0*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Creating a bean in session scope Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Creating a bean in session scope " Watch "Creating a bean in session scope " New topic
Author

Creating a bean in session scope

Meena R. Krishnan
Ranch Hand

Joined: Aug 13, 2006
Posts: 178
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?
Michael Ku
Ranch Hand

Joined: Apr 20, 2002
Posts: 510
Would you please post the code for com.example.SuperCreature.

Thank you
Meena R. Krishnan
Ranch Hand

Joined: Aug 13, 2006
Posts: 178
Here it is..
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

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 ]

[My Blog]
All roads lead to JavaRanch
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

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

Joined: Aug 13, 2006
Posts: 178
>>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

Joined: Aug 13, 2006
Posts: 178
"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

Joined: Nov 24, 2005
Posts: 14687
    
  16

Yes both of them will be executed when the bean is first created in the session.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Creating a bean in session scope
 
Similar Threads
Doubt regarding useBean and getAttribute
useBean with only type attribute(please help)
jsp:useBean
EL issue in HFSJ
Weird output from jsp:getProperty : pls help