Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 11. Custom Tags > Creating a New Custom Tag

11.3. Creating a New Custom Tag

Listing 11.1 shows the first example of using custom tags. The example creates an new tag that iterates through a section of the JSP page a certain number of times. The functional components will be discussed as after each component has been presented.

Listing 11.1. LoopTag.jsp

package com.javadesktop.utiltags;

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

public class LoopTag extends BodyTagSupport {
  private int maxCount = 0;
  private int currentCount = 0;

  public void setCount(int count) {
    this.maxCount = count;
  }

  public int doStartTag() {
    if (currentCount < maxCount) {
      return EVAL_BODY_TAG;
    } else {
      return SKIP_BODY;
    }
  }
  public int doAfterBody() throws JspException {
    currentCount++;

    if (currentCount < maxCount) {
      return EVAL_BODY_TAG;
    } else {
      return SKIP_BODY;
    }
  }

   public int doEndTag() throws JspException {
    try {
      if(bodyContent != null)
    bodyContent.writeOut(bodyContent.getEnclosingWriter());
    } catch(java.io.IOException e) {
    throw new JspException("IO Error: " + e.getMessage());
    }

    return EVAL_PAGE;
  }

  public void release() {
    super.release();
    currentCount = 0;
  }
}


					  


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint