Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring 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 ...
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
  • Piet Souris
  • Frits Walraven
  • Carey Brown

doAfterBody not being run when using IterationTag

Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to use IterationTag. I cannot get do AfterBody to run, no matter what I try.
I am using Tomcat 4.1.12 with Java 2 v1.4.
I first tried implementing IteratorTag, then overriding doAfterTag(). I has doStartTag() returning EVAL_BODY_INCLUDE, which I thought would cause doAfterTag() to be executed. My only other choice was to return SKIP_BODY, so I knew that wouldn't do it. This didn't work. The Tomcat container skipped right to doEndTag().
Then I extended first TagSupport, the BodyTagSupport (the one you see here.) I tried having doStartTag() return EVAL_BODY_BUFFERED and EVAL_BODY_INCLUDE, but doAfterTag() never gets run.
Does anyone know what is happening here? Amm i missing something obvoius? Thanks for any help you can provide.
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
public class TestIncludeTag extends BodyTagSupport {
PageContext pageContext;
private int numTimes = 0;
private int maxTimes = 2;
public void setPageContext(PageContext p) {
pageContext = p;
public int doStartTag() {
try {
pageContext.getOut().println("I am in doStartTag.<P>");
} catch (Exception e) { }
public int doAfterBody() throws JspException {
try {
pageContext.getOut().println("I am in doAfterBody.<P>");
} catch (Exception e) { }
if (numTimes < maxTimes)
return SKIP_BODY;
public int doEndTag() throws JspException {
try {
pageContext.getOut().println("I am in doEndTag.<P>");
} catch (Exception e) { }
return EVAL_PAGE;
Ranch Hand
Posts: 374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I ran into this problem when developing my own iterate tag as well.
The body of the tag is not evaluated (calling doAfterBody) unless the returned value of doStartTag is EVAL_BODY_TAG -- not EVAL_BODY_INCLUDE. This value also needs returned from doAfterBody if you want your iteration to continue.
David Barker
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My JSP how-to book doesn't mention this, but I found it in the documentation. Apparently, if there is no body, doStartTag() returns SKIP_BODY.
From the javax.servlet.jsp.tagext, IterationTag Interface documentation:
"If the TagLibraryDescriptor file indicates that the action must always have an empty action, by an <body-content> entry of "empty", then the doStartTag() method must return SKIP_BODY. Otherwise, the doStartTag() method may return SKIP_BODY or EVAL_BODY_INCLUDE."
So, I put in a single space for body content, and suddenly everything was working.
Just to add, in my example, I extended BodyTagSupport. This adds a return code for doStartTag(). In summary:
Interface Tag:
Interface IterationTag:
defines IterationTag.EVAL_BODY_AGAIN
returns IterationTag.EVAL_BODY_AGAIN
Interface BodyTag:
BodyTag.EVAL_BODY_TAG (deprecated)
When you implement BodyTag, doStartTag():
If you return BodyTag.EVAL_BODY_BUFFERED, then setBodyContent() and doInitBody() will be run, otherwise, they will not be run and thus you cannot modify the content body.
BUT, you need some body content, or you will always SKIP_BODY. There. I Learned something.
    Bookmark Topic Watch Topic
  • New Topic