O'Reilly logo

Head First Servlets and JSP, 2nd Edition by Bert Bates, Bryan Basham, Kathy Sierra

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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>

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required