Iteration using JSP Custom Tag

Iteration in JSP Custom tag with examples of session tracking, implicit objects, el, jstl, mvc, custom tags, file upload, file download, interview questions etc.

Iteration using JSP Custom Tag

Iteration using JSP Custom Tag

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

Here we are going to use the TagSupport class which implements the IterationTag interface. For iterating the body content, we need to use the EVAL_BODY_AGAIN constant in the doAfterBody() method.

Example of Iteration using JSP Custom Tag

In this example, we are going to use the attribute in the custom tag, which returns the power of any given number. We have created three files here

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

index.jsp

 

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

PowerNumber.java

 

  1. package com.hpnmaratt.taghandler;  
  2.   
  3. import javax.servlet.jsp.JspException;  
  4. import javax.servlet.jsp.JspWriter;  
  5. import javax.servlet.jsp.tagext.TagSupport;  
  6.   
  7. public class PowerNumber extends TagSupport{  
  8. private int number;  
  9. private int power;  
  10. private static int counter;  
  11. private static int result=1;  
  12.   
  13. public void setPower(int power) {  
  14.     this.power = power;  
  15. }  
  16.   
  17. public void setNumber(int number) {  
  18.     this.number = number;  
  19. }  
  20.   
  21. public int doStartTag() throws JspException {  
  22.     return EVAL_BODY_INCLUDE;  
  23. }  
  24.   
  25. public int doAfterBody() {  
  26.     counter++;   
  27.     result *= number;   
  28.     if (counter==power)   
  29.       return SKIP_BODY;   
  30.     else   
  31.       return EVAL_BODY_AGAIN;   
  32.   }   
  33.   
  34. public int doEndTag() throws JspException {  
  35.     JspWriter out=pageContext.getOut();  
  36.     try{  
  37.         out.print(result);  
  38.     }catch(Exception e){e.printStackTrace();}  
  39.       
  40.     return EVAL_PAGE;  
  41. }  
  42. }  

mytags.tld

 

  1. <?xml version="1.0" encoding="ISO-8859-1" ?>  
  2. <!DOCTYPE taglib  
  3.         PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"  
  4.         "http://java.sun.com/j2ee/dtd/web-jsptaglibrary_1_2.dtd">  
  5.   
  6. <taglib>  
  7.   <tlib-version>1.0</tlib-version>  
  8.   <jsp-version>1.2</jsp-version>  
  9.   <short-name>simple</short-name>  
  10.   <uri>http://tomcat.apache.org/example-taglib</uri>  
  11.   <description>A simple tab library for the examples</description>  
  12.   
  13.   <tag>  
  14.     <name>power</name>  
  15.     <tag-class>com.hpnmaratt.taghandler.PowerNumber</tag-class>  
  16.       
  17.     <attribute>  
  18.     <name>number</name>  
  19.     <required>true</required>  
  20.     </attribute>  
  21.       
  22.     <attribute>  
  23.     <name>power</name>  
  24.     <required>true</required>  
  25.     </attribute>  
  26.     
  27.   </tag>  
  28. </taglib> 

Looping using Iteration Tag (creating tag for loop)

Let's create a loop tag that iterates the body content of this tag.

File: index.jsp
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  2. <html>  
  3. <head>  
  4. <title>Insert title here</title>  
  5. </head>  
  6. <body>  
  7.   
  8. <%@taglib prefix="m" uri="sssuri" %>  
  9. <m:loop end="5" start="1">  
  10. <p>My Name is khan</p>  
  11. </m:loop>   
  12.   
  13. </body>  
  14. </html>  
File: mytags.tld
  1. <?xml version="1.0" encoding="ISO-8859-1" ?>  
  2. <!DOCTYPE taglib  
  3.         PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"  
  4.         "http://java.sun.com/j2ee/dtd/web-jsptaglibrary_1_2.dtd">  
  5. <taglib>  
  6.   <tlib-version>1.0</tlib-version>  
  7.   <jsp-version>1.2</jsp-version>  
  8.   <short-name>abc</short-name>  
  9.     
  10.   <uri>sssuri</uri>  
  11.  <tag>  
  12.     <name>loop</name>  
  13.     <tag-class>com.hpnmaratt.customtag.Loop</tag-class>  
  14.      
  15.     <attribute>  
  16.     <name>start</name>  
  17.     <required>true</required>  
  18.     </attribute>  
  19.       
  20.     <attribute>  
  21.     <name>end</name>  
  22.     <required>true</required>  
  23.     </attribute>  
  24.  </tag>  
  25.    
  26. </taglib>  
File: Loop.java
  1. package com.hpnmaratt.customtag;  
  2. import javax.servlet.jsp.JspException;  
  3. import javax.servlet.jsp.tagext.TagSupport;  
  4.   
  5. public class Loop extends TagSupport{  
  6.     private int start=0;  
  7.     private int end=0;  
  8.       
  9.     public void setStart(int start) {  
  10.         this.start = start;  
  11.     }  
  12.     public void setEnd(int end) {  
  13.         this.end = end;  
  14.     }  
  15.       
  16.     @Override  
  17.     public int doStartTag() throws JspException {  
  18.         return EVAL_BODY_INCLUDE;  
  19.     }  
  20.   
  21.     @Override  
  22.     public int doAfterBody() throws JspException {  
  23.         if(start<end){  
  24.             start++;  
  25.             return EVAL_BODY_AGAIN;  
  26.         }else{  
  27.         return SKIP_BODY;  
  28.        }  
  29.           
  30.     }  
  31.   
  32.       
  33. }  
File: web.xml
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <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">  
  3.   
  4. <jsp-config>  
  5. <taglib>  
  6. <taglib-uri>sssuri</taglib-uri>  
  7. <taglib-location>/WEB-INF/mytags.tld</taglib-location>  
  8. </taglib>  
  9. </jsp-config>  
  10.   
  11. </web-app>