A.14. Creating Custom JSP Tag Libraries

The Tag Handler Class

  • Implement Tag interface by extending TagSupport (no tag body or tag body included verbatim) or BodyTagSupport (tag body is manipulated).

  • doStartTag: code to run at beginning of tag

  • doEndTag: code to run at end of tag

  • doAfterBody: code to process tag body

The Tag Library Descriptor File

  • Within taglib element, contains tag element for each tag handler. E.g.:

    <tag>
      <name>prime</name>
      <tagclass>coreservlets.tags.PrimeTag</tagclass>
      <info>Outputs a random N-digit prime.</info>
      <bodycontent>EMPTY</bodycontent>
      <attribute>
        <name>length</name>
        <required>false</required>
      </attribute>
    </tag>
    

The JSP File

  • <%@ taglib uri="some-taglib.tld" prefix="prefix" %>

  • <prefix:tagname />

  • <prefix:tagname>body</prefix:tagname> ...

Get Core Servlets and JavaServer Pages™ 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.