This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jsp:useBean Doubt

 
Jagjit Dhaliwal
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got this question from javabeat.com mock exam? They mention answer as E. Can anyone explain what exactly would be output and why?

What will the following JSP page print?

<% { %>

<jsp:useBean id="sb" class="java.lang.StringBuffer" />

sb.append("Hello");

<% } %>

<%=sb%>
Select 1 correct option.
A. It will print null
B.It will print "Hello"
C.It will not compile because <jsp:useBean> cannot be used inside a block
D.It will not compile because StringBuffer is not a bean
E.None of these
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"sb.append("Hello");" is not a scriptlet. It will be output "as is" on the client.
 
Jagjit Dhaliwal
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Christophe, I just overlook it. [ ]
 
Jagjit Dhaliwal
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just another doubt, what is scope of bean variable "sb" if it is included in pair of curly braces?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just another doubt, what is scope of bean variable "sb" if it is included in pair of curly braces?

Good question. It will be local to that block, so not visible outside.
 
Jagjit Dhaliwal
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for reply Christophe,

It makes sense but following question confuse me further. If scope of bean within pair of curly braces is within braces only, then why would following piece of code gives translation error?

-------------------------------------------------------------------
(Source - Manning-Deshmukh)
Consider the following code:
<html><body>
<%{%>
<jsp:useBean id="address" class="AddressBean" scope="session" />
<%}%>
<jsp:useBean id="address" class="AddressBean" scope="session" />
<jsp:getProperty name="address" property="street" />
</body></html>
Which of the following is true about the above code? (Select one)
a It will give translation-time errors.
b It will give compile-time errors.
c It may throw runtime exceptions.
d It will print the value of the street property.
--------------------------------------------------------------------
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now this is a different story. This is not a compiling issue. The id you give to a bean must be unique in a translation unit. No matter if it's local to a block, it must be unique. When the container translates the JSP pages, it keeps track of the id used for the bean. It associates an id to an object (maybe in a map). At this time, it does not know whether it's local to a block or not. It just gets the id, looks if it does not exist, and stores it. If it already exsits, it will result in a translation error. Here is an excerpt from the spec :
the name must be unique within the translation unit, and identifies the particular element in which it appears to the JSP container and page. Duplicate id�s found in the same translation unit shall result in a fatal translation error.
 
Jagjit Dhaliwal
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Christophe,
Its clear now.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic