logo

Iteration Using JSP Custom Tag


Show

We can iterate the body content of any tag using the doAfterBody() technique of the IterationTag interface.

Here we're going to use the TagSupport class which implements the IterationTag interface. For iterating the body content, we want to use the EVAL_BODY_AGAIN constant withinside the doAfterBody() technique.

Example of Iteration using JSP Custom Tag

In this example, we're going to use the characteristic withinside the custom tag, which returns the strength of any given number. We have created 3 documents here

  • index.jsp
  • PowerNumber.java
  • mytags.tld

index.jsp

<%@ taglib uri="WEB-INF/mytags.tld" prefix="m" %>  
3 ^ 5 = <m:power number="3" power="5">  
body  
</m:power>

PowerNumber.java

package com.javatpoint.taghandler;  

import javax.servlet.jsp.JspException;  
import javax.servlet.jsp.JspWriter;  
import javax.servlet.jsp.tagext.TagSupport;  

public class PowerNumber extends TagSupport{  
private int number;  
private int power;  
private static int counter;  
private static int result=1;  

public void setPower(int power) {  
    this.power = power;  
}  

public void setNumber(int number) {  
    this.number = number;  
}  

public int doStartTag() throws JspException {  
    return EVAL_BODY_INCLUDE;  
}  

public int doAfterBody() {  
    counter++;   
    result *= number;   
    if (counter==power)   
      return SKIP_BODY;   
    else   
     return EVAL_BODY_AGAIN;   

  }   

public int doEndTag() throws JspException {  
    JspWriter out=pageContext.getOut();  
    try{  
        out.print(result);  
    }catch(Exception e){e.printStackTrace();}  

    return EVAL_PAGE;  
}  
}  

Mytags.tld

<?xml version="1.0" encoding="ISO-8859-1" ?>  
<!DOCTYPE taglib  
        PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"  
        "http://java.sun.com/j2ee/dtd/web-jsptaglibrary_1_2.dtd"> 

<taglib>  
  <tlib-version>1.0</tlib-version>  
  <jsp-version>1.2</jsp-version>  
  <short-name>simple</short-name>  
  <uri>http://tomcat.apache.org/example-taglib</uri>  
  <description>A simple tab library for the examples</description>  

  <tag>  
    <name>power</name>  
    <tag-class>com.intellinuts.taghandler.PowerNumber</tag-class>   

    <attribute>  
    <name>number</name>  
    <required>true</required>  
    </attribute>     

    <attribute>  
    <name>power</name>  
    <required>true</required>  
    </attribute>    

  </tag>  
</taglib>  

download this example (developed using MyEclipse ide)

Looping using Iteration Tag (Creating tag for loop)

Let’s develop a loop tag that Iterates the body content of this tag.

File:index.jsp

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
<html>  
<head>  
<title>Insert title here</title>  
</head>  
<body>   

<%@taglib prefix="m" uri="sssuri" %>  
<m:loop end="5" start="1">  
<p>My Name is khan</p>  
</m:loop>   
  
</body>  
</html>

File:mytags.tld

<?xml version="1.0" encoding="ISO-8859-1" ?>  
<!DOCTYPE taglib  
        PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"  
        "http://java.sun.com/j2ee/dtd/web-jsptaglibrary_1_2.dtd">  
<taglib>  
  <tlib-version>1.0</tlib-version>  
  <jsp-version>1.2</jsp-version>  
  <short-name>abc</short-name>  

  <uri>sssuri</uri>  
 <tag>  
    <name>loop</name>  
    <tag-class>com.intellinuts.customtag.Loop</tag-class> 
     
    <attribute>  
    <name>start</name>  
    <required>true</required>  
    </attribute>  

    <attribute>  
    <name>end</name>  
    <required>true</required>  
    </attribute>  
 </tag>  

</taglib>

File: Loop.java

package com.javatpoint.customtag;  
import javax.servlet.jsp.JspException;  
import javax.servlet.jsp.tagext.TagSupport;  

public class Loop extends TagSupport{  
    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 doStartTag() throws JspException {  
        return EVAL_BODY_INCLUDE;  
    }  

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

}  

File: web.xml

<?xml version="1.0" encoding="UTF-8"?>  
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">  

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

</web-app>

download this example (developed using Eclipse ide)

Output