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

EVAL_BODY_BUFFERED behavior

 
Anoopriya Muthuswaami
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
TLD:
<body-content>JSP</body-content>

JSP:
<prefix:tagName>Hello</prefix:tagName>

Case 1:

Tag Handler:
public class TextTag extends BodyTagSupport{


public int doStartTag() throws JspException {
return EVAL_BODY_INCLUDE;
}


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

Output:
Hello How are you?

Case 2:

Tag Handler:
public class TextTag extends BodyTagSupport{


public int doStartTag() throws JspException {
return EVAL_BODY_BUFFERED;
}


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

Output:
Nothing gets printed.

If I add the following code, �Hello How are you?� gets printed
JspWriter out = getBodyContent().getEnclosingWriter();
out.print(getBodyContent().getString());
out().print("How are you?");

What is the difference between pageContext.getOut and JspWriter got from getEnclosingWriter()? The behavior differs only when I return EVAL_BODY_BUFFERED from doStartTag().
Can someone please explain me the concept.
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
PageContext.getOut() gets the output for the entire response, so data written there is written directly to the response. The getEnclosingWriter() returns a nested (buffered) writer for the tag. This allows a BodyTag to drop or iterate over and control its content. So the BodyTag can have content written to its buffered writer, then choose to discard it all in the doEndTag(). What this means is that the content is only written to the getOut() Writer when the entire BodyTag has completed. So content written to getOut() gets to the output first, before the content of getEnclosingWriter().
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic