• 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

what happens when doStartTag returns EVAL_BODY_BUFFERED?  RSS feed

Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • 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?

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

Posts: 14691
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • 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.
    Did you just should on me? You should read this tiny ad:
    Programmatically Create PDF Using Free Spire.PDF with Java
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!