File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes doAfterBody not being run when using IterationTag Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "doAfterBody not being run when using IterationTag" Watch "doAfterBody not being run when using IterationTag" New topic

doAfterBody not being run when using IterationTag

David Barker

Joined: Feb 15, 2003
Posts: 2
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;

-David Barker<br /> Sun Certified Java Programmer
David Hibbs
Ranch Hand

Joined: Dec 19, 2002
Posts: 374
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.

"Write beautiful code; then profile that beautiful code and make little bits of it uglier but faster." --The team, Newsletter 039.
David Barker

Joined: Feb 15, 2003
Posts: 2
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.
I agree. Here's the link:
subject: doAfterBody not being run when using IterationTag
It's not a secret anymore!