Win a copy of AWS Security this week in the Cloud/Virtualization forum!
  • 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 ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

EVAL_BODY_BUFFERED behavior

 
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.
 
Author
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().
 
Good night. Drive safely. Here's a tiny ad for the road:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic