• 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

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().
 
Villains always have antidotes. They're funny that way. Here's an antidote disguised as a tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic