• 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

EVAL_BODY_BUFFERED behavior

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author
Posts: 836
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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().
 
She still doesn't approve of my superhero lifestyle. Or this shameless plug:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic