File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes useBean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "useBean" Watch "useBean" New topic


Thiru Thangavelu
Ranch Hand

Joined: Aug 29, 2001
Posts: 219

<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?

Thanks,<br />Thiru<br />[SCJP,SCWCD,SCBCD]
Vedhas Pitkar
Ranch Hand

Joined: Jan 27, 2001
Posts: 445
<% temp %> declares a local String temp variable INSIDE the _jspService method.For furthur curiosity,see the generated .java file.
Bernd Stransky
Ranch Hand

Joined: Nov 20, 2001
Posts: 47
Originally posted by Thiru Thangavelu:

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

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,
Maha Annadurai
Ranch Hand

Joined: Oct 27, 2002
Posts: 87
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.

Maha Anna
[ December 16, 2002: Message edited by: Maha Annadurai ]
I agree. Here's the link:
subject: useBean
jQuery in Action, 3rd edition