File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes 3 is correct ...but the answer states it is 2..am i right Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Professional Certification » Web Component Certification (SCWCD/OCPJWCD)
Reply Bookmark "3 is correct ...but the answer states it is 2..am i right" Watch "3 is correct ...but the answer states it is 2..am i right" New topic
Author

3 is correct ...but the answer states it is 2..am i right

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Which of the following staments are correct about the following jsp lines:
<jps:useBean id=”name” class=”java.lang.String” />
<%= name %>
1) It won't compile.
2) It is a valid jsp line and it will print the variable called name.
3) It will compile but it will always produce null as the output.
4) It will work if you create a javabean class with only one variable of type java.lang.String
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 16617

OK, here is my weak area, but I do remember that it will return the value from a variable named "name", so 2 is correct. I think I read it in the specs, which states that if you have a line like
<%= name %>
In the two lines of
<jps:useBean id=�name� class=�java.lang.String� />
<%= name %>
Since no get method is called it looks for a get method in name called getName, like if you had put <%=name.getName()%>
Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Goan Balchao
Ranch Hand

Joined: Mar 25, 2002
Posts: 93
Well,
I think the declaration of the bean is ok, because java.lang.String has a no argument constructor and the bean has page scope.
But the container will try to find a variable named 'name' and will spew an error on not finding it. Unless the 'id' of the bean is referenced or something like 'PageContext.getAttribute(name)' is used, the bean cannot be referenced in the manner shown


Hemant Kamat<br />SCJP2<br />SCWCD<br />SCBCD<br />SCEA-I
Goan Balchao
Ranch Hand

Joined: Mar 25, 2002
Posts: 93
Now come to think of it, I think it is a scoping problem .
If you try this
<jsp:useBean id="name" class="java.lang.String" >
<%= name %>
</jsp:useBean>
or
<% { %>
<jsp:useBean id="name" class="java.lang.String"/>
<%= name %>
<% } %>
it will work.
So the scripting variable is obtained only within the useBean tag or in scope.
It can't be obtained outside the scope.
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Whoa, too many contradictory opinions here.Will retrieve a bean with the name "name" from the page scope. If it isn't there, the bean will be instantiated and stored in the page scope. In either case, a scripting variable "name" will be created with the bean reference in it. The variable will be available in the entire rest of the page, from the opening tag onwards. Not just between opening and closing tags.in general, a scriptlet <%= expression %> will be translated to something likeIn this case, this means that name.toString() will be called and the result included in the output stream.
The correct answer is therefore indeed (2).
- Peter
[ March 30, 2002: Message edited by: Peter den Haan ]
Goan Balchao
Ranch Hand

Joined: Mar 25, 2002
Posts: 93
Thanks for the clarification Peter. Sorry about my earlier posting. I misunderstood the scope for the variables introduced in this manner.
 
 
subject: 3 is correct ...but the answer states it is 2..am i right
 
Threads others viewed
Question reagrding Beans....
JSP - UseBean
mock question
mock exam question
doubt in <jsp:usebean..
MyEclipse, The Clear Choice