• 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

<jsp:useBean>inside the scripting block

 
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What will the following JSP page print?(Enthuware Mock)


1. It will print null.
2. It will print "Hello".
3. It will not compile because <jsp:useBean> cannot be used inside a block.
4. It will not compile because StringBuffer is not a bean.
5. None of these.

correct option is in bold. given explanation is:
Since, the useBean tag is inside a block, variable sb will be declared inside the block and thus will not be accessible outside it. So the line <%=sb%> will give:

Undefined variable: sb


If defualt is page scope , then using a <jsp:useBean> tag inside a scriplet block can restrict the default scope to block scope??

Please help
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well I think that the page scope only works if you don't declare any scriptlets. That is why it is said that use scriptless JSPs and you'll be fine
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all I think there is a little code issue here

has not been written as a scriptlet, instead the line should have been



If that were corrected,

Poonam Agarwal wrote:
If defualt is page scope , then using a <jsp:useBean> tag inside a scriplet block can restrict the default scope to block scope??


I think its not the tag which is doing it but if you think of it in terms of how container will translate this jsp code into java for a servlet, you'll realise why its so

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kamal Tripathi wrote:First of all I think there is a little code issue here

has not been written as a scriptlet, instead the line should have been




I agree with Kamal. Since the bean hasn't been assigned any value as the above line is rendered as plain html, only this line itself will get printed and the last expression will call the toString() method of StringBuffer which will print nothing.
 
Poonam Agarwal
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sourin,

Sourin wrote
agree with Kamal. Since the bean hasn't been assigned any value as the above line is rendered as plain html, only this line itself will get printed and the last expression will call the toString() method of StringBuffer which will print nothing.



please check the question again AND the given explanation too.

<% { %>
<jsp:useBean id="sb" class="java.lang.StringBuffer" />
sb.append("Hello");
<% } %>

1. sb.append("Hello") is already inside the scriplets and it also append the 'Hello' string to the sb
2. My doubt is related to scope issue. As sb is defined inside the scriplet block, outside the block it gives Undefined variable.

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



1. sb.append("Hello") is already inside the scriplets and it also append the 'Hello' string to the sb



it's not inside the scriplet. because it's not inside <% %>. if you see the generated servlet you'll find it inside out.write().
so, nothing is being appended. If you correct this, you'll get sb cannot be resolved compiler error because you are trying to access it outside the block in which you initialized it. you can get it out the block like this <%pageContext.getAttribute("sb")%>
not by <%=sb%>. because sb has blockscope.
 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nisha,

I tried your soultion and it worked. But i have one doubt here us suggested to use <%=pageContext.getAttribute("sb")%> and it worked.

Does it work because of sb is declared at block level and you tried to access unig pageContext because pageContext also covers block scope or
pageContext.getAttribute() will give you attribute at any level.

As per i know pageContext will give you attribute at page level unless you specify scope as a third argument in setAttribute method.

Please Explain.
How your suggestion worked.

Thanks in Advance.
 
Kamal Tripathi
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jay Shukla wrote:

Does it work because of sb is declared at block level and you tried to access unig pageContext because pageContext also covers block scope or
pageContext.getAttribute() will give you attribute at any level.

As per i know pageContext will give you attribute at page level unless you specify scope as a third argument in setAttribute method.



This worked because jsp:useBean works in a way that if attribute is not found in the scope (by default, page) mentioned
then a new object of type is created and set as an attribute in the same scope so translated code is something like this.


Now sb.append("hello") works entire because container exposes this attribute thru the same "id" attribute i.e. sb. If by saying that "sb is declared at block level" you mean that it was used in jsp:useBean inside a block so container created it, you are right.

However sb is declared inside a block and can't be accessed outside it so <%= sb %> won't work
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic