• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSP useBean question

 
reubin haz
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a question from J2EEcertificaiton.
What is the output at line 6 of the following code fragment? Consider that "postageType" already exists in the session.

1.<jsp:useBean id="postageType" scope="session"
2. class="com.baboon.scwcd.PostageType" >
3. <% postageType.setType("First Class"); %>
4.</jsp:useBean>
5.
6.<%= postageType.getType() %>

The answer:
The bean postageType is already present in the session scope, the JSP engine will skip the body of the useBean tag. The scriplet code at line 3 is not executed.
The output of the scriplet code at line 6 cannot be predicted, the property has been set somewhere else


Can someone please explain further? Why line 6 can't predicted? Thanks.
[ November 24, 2006: Message edited by: reubin haz ]
 
Dinuka Arsakularatne
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the session attribute "postageType" has been set somewhere else, maybe in a servlet. so there is no way to know what the type has been set to in that specific place hence there is no way of knowing the value.

hope this helps
 
reubin haz
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so does it mean many things can be happened between line 4 and 6?
 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When jsp:useBean is used with a body, the body is executed only if a new bean is created, which is not created in your case.
 
reubin haz
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know it won't execute that. So "postageType" existing in the session will be used directly, isn't it? So getType() should return the type of the object from there. Then why is it not predictable?
[ November 27, 2006: Message edited by: reubin haz ]
 
Siddharth Purandare
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi reubin,

I guess first the container will try to look for the already created bean postageType which is NOT as discussed already by Dinuka so it will try to look into session scope if it (bean) exists it will get the property so thats why the behaviour of line 6 is unpredictable.

Hope I am correct.
[ November 27, 2006: Message edited by: Siddharth Purandare ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic