Tag Files: like include, only better

With Tag Files, you can invoke reusable content using a custom tag instead of the generic <jsp:include> or <c:import>. You can think of Tag Files as a kind of “tag handler lite”, because they let page developers create custom tags, without having to write a complicated Java tag handler class, but Tag Files are really just glorified includes.

Simplest way to make and use a Tag File

  1. Take an included file (like “Header.jsp”) and rename it with a .tag extension.

    image with no caption
    image with no caption
  2. Put the tag file (“Header.tag”) in a directory named “tags” inside the “WEB-INF” directory.

    image with no caption
  3. Put a taglib directive (with a tagdir attribute) in the JSP, and invoke the tag.

    image with no caption

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.