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>inside the scripting block

 
Poonam Agarwal
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • 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
 
Ankit Garg
Sheriff
Posts: 9578
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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
 
Kamal Tripathi
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • 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

 
Sourin K. Sen
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • 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
  • 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.

 
nisha chidella
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • 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.
 
Jay Shukla
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic