• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Goan Balchao
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the clarification Peter. Sorry about my earlier posting. I misunderstood the scope for the variables introduced in this manner.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic