Making a Simple tag handler

For the simplest of Simple tags, the process is...simple.

  1. Write a class that extends SimpleTagSupport

    package foo;
    import javax.servlet.jsp.tagext.SimpleTagSupport;
    // more imports needed
    
    public class SimpleTagTest1 extends SimpleTagSupport {
       // tag handler code here
    }
  2. Override the doTag() method

    image with no caption
  3. Create a TLD for the tag

    <taglib ...>
      <tlib-version>1.2</tlib-version>
      <uri>simpleTags</uri>
      <tag>
         <description>worst use of a custom tag</description>
         <name>simple1</name>
         <tag-class>foo.SimpleTagTest1</tag-class>
         <body-content>empty</body-content>
      </tag>
    </taglib>
  4. Deploy the tag handler and TLD

    Put the TLD in WEB-INF, and put the tag handler inside WEB-INF/classes, using the package directory structure, of course. In other words, tag handler classes go in the same place all other web app Java classes go.

    image with no caption
  5. Write a JSP that uses the tag

    <%@ taglib prefix="myTags" uri="simpleTags" %>
    <html><body>
    <myTags:simple1/>
    </body></html>

Get Head First Servlets and JSP, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.