Iteration through Custom Tags in JSP

843 Views Asked by At

Consider the snippet
loop.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="mypredefinedtaglibrary" prefix="my" %>
<html>
<head>

</head>
<body>
    <my:looping end="5" start="1">
     <p>My Name is Khan</p><br/>
    </my:looping>

</body>
</html>

MyTagHandlerForLoop.java

public class MyTagHandlerForLoop extends TagSupport {

    private static final long serialVersionUID = 1L;
    private int start = 0;
    private int end = 0;

    public void setStart(int start) {
        this.start = start;
    }

    public void setEnd(int end) {
        this.end = end;
    }

    @Override
    public int doAfterBody() throws JspException {
        if (start < end) {
            start++;
            return EVAL_BODY_AGAIN;
        } else {
            return SKIP_BODY;
        }

    }

    @Override
    public int doStartTag() throws JspException {

        return EVAL_BODY_INCLUDE;
    }

}

mytags.tld

    <tag>
        <name>looping</name>
        <tag-class>org.customtags.MyTagHandlerForLoop</tag-class>
        <body-content>scriptless</body-content>
        <attribute>
            <name>start</name>
            <required>true</required>
        </attribute>
        <attribute>
            <name>end</name>
            <required>true</required>
        </attribute>
    </tag>

Configuration inside web.xml

  <jsp-config>
    <taglib>
        <taglib-uri>mypredefinedtaglibrary</taglib-uri>
        <taglib-location>/WEB-INF/tags/mytags.tld</taglib-location>
    </taglib>
  </jsp-config>

The O/P reflects as:

My Name is Khan
My Name is Khan
My Name is Khan
My Name is Khan
My Name is Khan

When i run in Debug mode, an object first of all is created, then the setters are c/d, resulting in

start = 1
end = 5

if (start < end) {
                start++;
                return EVAL_BODY_AGAIN;

This looping process executes 4 times.

So, why My Name is Khan is outputted 5 times. Shouldn't it be for 4 times.

1

There are 1 best solutions below

0
On

"Evaluate Body", which outputs "My Name is Khan", runs 5 times. Firstly from doStartTag() method and then, as you suggest, 4 times more from doAfterBody() method.

This graph clarifies how classic custom tags works.

custom tag cycle