wood burning stoves*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes what happens when doStartTag returns EVAL_BODY_BUFFERED? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "what happens when doStartTag returns EVAL_BODY_BUFFERED?" Watch "what happens when doStartTag returns EVAL_BODY_BUFFERED?" New topic
Author

what happens when doStartTag returns EVAL_BODY_BUFFERED?

geeta vemula
Ranch Hand

Joined: Jul 18, 2008
Posts: 208
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
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

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.


  • [My Blog]
    All roads lead to JavaRanch
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: what happens when doStartTag returns EVAL_BODY_BUFFERED?
     
    Similar Threads
    Exam question.
    Custom Tag Handler
    Mock Exam Question - related to doAfterBody
    Can anyone explain this to me?
    Edurardo Cobian's mock exam answer correct for Question No 25