Iteration JSP custom tag tutorial

Iteration tag if for achieving the for loop, while loop and if condition inside the JSP page, for example you want to iterate a body of custom tag 10 time.

We can achieve the iteration using the IterationTag interface which is the interface in jsp API, this interface has one extra method named doAfterBody() which will decide that body has to execute one more time or not.

The doAfterBody() method has two valid return type as:

EVAL_BODY_AGAIN: If it returns means the body of the custom tag has to execute one more time

EVAL_BODY: If it returns means the body has to skip next time, and the custom tag stop the iteration of body.

If you want to achieve the iteration of tag, doStartTag() should return EVAL_BODY_INCLUDE always, otherwise body will never be executed.

In this example we are going to iterate the body if custom tag 10 time, for this we are putting one attribute names as index with value 10.

Below is the example of iteration custom tag handler class.

package com.java.connect.tag;
 
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.IterationTag;
import javax.servlet.jsp.tagext.Tag;
 
import org.apache.log4j.Logger;
 
public class IterationTagHandller implements IterationTag {
 
    private static Logger logger = Logger.getLogger(IterationTagHandller.class);
    private PageContext context;
    private Integer index;
 
    public int doStartTag() throws JspException {
        try {
            JspWriter out = context.getOut();
            out.println("");
        } catch (Exception e) {
            logger.info(e);
        }
        logger.info("I am in the doStartTag");
        return EVAL_BODY_INCLUDE;
    }
 
    public int doAfterBody() throws JspException {
        logger.info("I am in the doAfterBody");
        if (index > 0) {
            index--;
            return EVAL_BODY_AGAIN;
        } else {
            return SKIP_BODY;
        }
    }
 
    public int doEndTag() throws JspException {
        logger.info("I am in the doEndTag");
        return EVAL_PAGE;
    }
 
    public Tag getParent() {
        logger.info("I am in the getParent");
        return null;
    }
 
    public void release() {
        logger.info("I am in release");
    }
 
    public void setPageContext(PageContext context) {
        this.context = context;
        logger.info("I am in setPageContext");
    }
 
    public void setParent(Tag t) {
        logger.info("I am in setParent");
    }
 
    public Integer getIndex() {
        return index;
    }
 
    public void setIndex(Integer index) {
        this.index = index;
    }
 
}

The configuration of iteration tag inside TLD file looks like as:

<tag>
    <name>iteration</name>
    <tagclass>com.java.connect.tag.IterationTagHandller</tagclass>
    <bodycontent>jsp</bodycontent>
    <attribute>
        <name>index</name>
        <required>true</required>
    </attribute>
</tag>

The JSP page where the iteration custom tag has been used looks like:

<%@ taglib uri="/WEB-INF/tld/custom-tags.tld" prefix="custom"%>
 
<custom:iteration index="10">
Hello<br />
</custom:iteration>

The output of JSP page will look like as:

iteration-jsp-custom-tag-tutorial_files