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

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

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • 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
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • 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
 
Goan Balchao
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic