• 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

what happens when doStartTag returns EVAL_BODY_BUFFERED?

 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is from http://www.javaranch.com/carl/SCWCD.htm#Question 26)

Given the following tag handler defined with <bodycontent>JSP</bodycontent>

public class body extends BodyTagSupport {
public int doAfterBody() throws JspException {
try { pageContext.getOut().print("how are you?"); }catch(IOException e) {}
return SKIP_BODY;
}
}

what will be printed out by the following part of a jsp page?
<prefix:sufix>
Hello
</prefix:sufix>

1) The tag handler won't compile.
2) The jsp page will print Hello how are you?
3) The jsp page will print how are you? Hello
4) The jsp page will print Hello


Answer is 4


Inside doAfterBdoy the bodyContent is in a stack and the implicit variable out prints into the bodyContent not to the outputStream.The bodyContent keeps a reference to the buffered writer (either a enclosing bodyContent or the implicit out variable) called the previousOut.If you want to print inside doAFterBdoy you have to use this previousOut. To print things unrelated with the body use pageContext.getOut in doStartTag. By default doStartTag in BodyTagSupport returns EVAl_BODY_BUFFERED which produces a stack of bodycontents, one bodycontent for each iteration.If you don't want this stack to be create you have to override doStartTag and return EVAL_BODY_IHCLUDE, then the default out would print to the output stream (to the response).


THe explanation is very confusing. can anyone explains me what it is saying

Thanks,
Geeta
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think there's an error in this question, because nothing should be printed out. Here is what happens :
  • doStartTag() is not overriden, so BodyTagSupport's doStartTag() is called, returning EVAL_BODY_BUFFERED.
  • Returning EVAL_BODY_BUFFERED means that everything will be buffered. The stream returned pageContext.getOut() is not the client's output (browser), but a buffer. So the String from pageContext.getOut().print("how are you?") is going to be printed into a buffer.
  • How do you access the client's output stream then ? BodyTagSupport keeps track of it. You can access it via BodyTagSupport#getPreviousOut. Or also via BodyTagSupport#getBodyContent()#getEnclosingWriter()
  • What happened to the "Hello" string from the JSP ? When using EVAL_BODY_BUFFERED, it is put into the buffer. So unless you flush the buffer to the client's output stream, it's not going to be printed. That's why I think that the answer is not correct.
  •  
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic