Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

useBean

 
Thiru Thangavelu
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

<jsp:useBean id="temp" class="java.lang.String" />
<% temp="temp";
out.println(temp); %>

How come temp can be assigned for both Bean and a Variable?
 
Vedhas Pitkar
Ranch Hand
Posts: 445
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<% temp %> declares a local String temp variable INSIDE the _jspService method.For furthur curiosity,see the generated .java file.
 
Bernd Stransky
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Thiru Thangavelu:

How come temp can be assigned for both Bean and a Variable?

Hi,
here is my understanding.
The jsp:useBean action does several things:
- declares a local variable with name temp and type String inside _jspService method
- searches for an attribute in the page scope with name "temp". if it exists, the associated object is assigned to variable temp.
- if such an attribute does not exist, a new String object is created, placed into the page scope with name "temp" and assigned to the temp variable.
So in any case, temp will point to an object of type string after this action completes.
Thinking in JAVA, the variable temp is just a normal method local variable and can be manipulated inside <% %> scriptlets.
The scriptlet <% temp="temp"; %> makes temp point to a new String object.
So you manipulate the same variable, but change the String object it points to.
Hope this helps,
Bernd
SCJP
 
Maha Annadurai
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thiru,
We can not have 2 vars with the same name in the same visiblity scope. In the above code, we are not declaring 2 vars with same name as "temp". There is ONLY ONE varablie inside _jspService(HttpServletRequest,HttpServletResponse) method.
The String var can take ANY value. In java we do not restrict a String var taking the same value as its name. Right? In the above code it's just so happened a String var named 'temp' takes a value of 'temp'.
The following code will not work since it tries to declare a 2nd var named as 'temp' in the same _jspService(req,res) method. Please take a look at the generated java file for the above jsp to have better understanding.


This code too will not work.

Regards,
Maha Anna
[ December 16, 2002: Message edited by: Maha Annadurai ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic