Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Custom Iterator Tag in JSP 
 

This example will demonstrate you how you can make a custom iterator tag in JSP? You can make your own custom tag in JSP which will have functionality as you want to do with them.

 

Custom Iterator Tag in JSP

                         

Example program to make custom iterator tag in JSP

This example will demonstrate you how you can make a custom iterator tag in JSP? You can make your own custom tag in JSP which will have functionality as you want to do with them. You can make an "Iteration Tag" as well. We have made a Custom Iterator Tag here which will iterate for the number of times, in its attribute "counts". For this purpose we have made an IteratorTag class which extends "BodyTagSupport" class .

Full code for "IteratorTag.java" is given as following :

package tags;

import java.io.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;

public class IteratorTag extends BodyTagSupport{

   private int counts = 0;
   private BodyContent bodyContent;

   public void setBodyContent(BodyContent bodyContent) {
      this.bodyContent = bodyContent;
   }
   
   public void setCounts(int counts) {
      this.counts = counts;
   }

   public int doStartTag() throws JspException {
      if (counts >0) {
        return EVAL_BODY_TAG;
      else {
         return SKIP_BODY;
      }
   }

   public int doEndTag() throws JspException {
      try {
         if(bodyContent != null) {
           bodyContent.writeOut(bodyContent.getEnclosingWriter());
         }
      catch(IOException e) {}
      return EVAL_PAGE;
   }

 public int doAfterBody() throws JspException {
      if (counts >1) {
        counts--;
        return EVAL_BODY_TAG;
      else {
         return SKIP_BODY;
      }
   }
}

In "IteratorTag.java" class, whole iteration is being managed through following three methods:

doStartTag() : When it is called then it checks the value of attribute "counts" if counts is greater than zero then it means that tag have to iterate more and it returns "EVAL_BODY_TAG" i.e tag has to evaluate tag body content and when it equals to zero means that tag has to do no more working. 

public int doStartTag() throws JspException {
      if (counts >0) {
        return EVAL_BODY_TAG;
      else {
         return SKIP_BODY;
      }
   }

doEndTag() : Working of doEndTag() is that it is taking the content to write between starting and closing tag. It  is writing the content to JSP output page. This is performing its working in following manner:

public int doEndTag() throws JspException {
   try {
     if(bodyContent != null) {
     bodyContent.writeOut(bodyContent.getEnclosingWriter());
     }
  catch(IOException e) {}
  return EVAL_PAGE;
}

Since bodyContent.writeOut() throws IOException so it is mandatory to handle this exception in your function.

doAfterBody() :  This method is decrementing counter each time when tag body content evaluates.

To use this custom tag as your JSP tag you have to make a Tag lib directory file which consists of following information related to this tag.

  • Tag lib version
  • JSP version
  • shortname or prefix which we will use to call this tag.
  • Tag name
  • tagclass
  • bodycontent :since this tag consists of some body so it has value "JSP" not "empty".
  • attribute:  Iterator tag consists of  attribute "counts" so it is necessary to define.

Full taglib.tld file code is as follows:

taglib.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/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
       <tlibversion>1.0</tlibversion>
       <jspversion>1.1</jspversion>
       <shortname>custom</shortname>
     <tag>
         <name>iterator</name>
         <tagclass>tags.IteratorTag</tagclass>
         <bodycontent>JSP</bodycontent>
         <attribute>
                 <name>counts</name>
                 <required>true</required>
         </attribute>
         <info>Iterator Tag</info>
     </tag>
</taglib>

Now you can use your custom tag in your JSP file as follows:

CustomIterator.jsp: In this JSP, 'counts' attribute is set to the value '3' so the content in the tag is printed 3 times on the page.

<%@ taglib uri="/WEB-INF/taglib.tld" prefix="custom" %>
<html>
    <head>
           <title>Custom Iterator Tag</title>
     </head>
    <body bgcolor="#0033cc">
    <font color="white">
        <H1>Welcome! to Custom Iterator </H1>
                  Custom Iterator tag starts...
        <br><hr>
        <custom:iterator counts="3">
            <br>3 times it would print this line
        </custom:iterator>
            <br><hr>
             Rest Body Content of JSP page
     </font>
     </body>
</html>

To run this program follow these steps:

  1. Create and save IteratorTag.java
  2. Compile and put IteratorTag.class into classes folder of your web application folder.
  3. Create taglib.tld file and place it into WEB-INF folder.
  4. Create and save CustomIterator.jsp
  5. Start Tomcat web server and type following URL in your browser's address bar:
    http://localhost:8080/Example/CustomIterator.jsp

Output:

Download Source Code

                         

» View all related tutorials
Related Tags: java html c web jsp dynamic static script servlet sed ip page tag this log tar close js start technology

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.