Chapter 11. Building Your Own Custom JSP Tag Library

JSP tag libraries were introduced in JSP 2.0. Before that, custom tag development was the domain of experienced Java developers. Custom JSP tags can now be developed to encapsulate pieces of repeatable JSP code without the need to program in Java.

This chapter introduces custom tags (also referred to as custom actions) developed as text files, explains their semantics, and provides some examples along the way that illustrate what an extremely powerful addition they are to the J2EE architecture. The chapter concludes with a short discussion about how you decide when to use Java custom tags and JSP custom tags.

Specifically, this chapter covers the following areas:

  • Creating a simple JSP tag file

  • Understanding the advantages of tag files

  • Developing tag files

  • Packaging tag files

What Is a Tag File?

A tag file is a special kind of custom action that is developed much like a JSP page. Unlike JSP pages, tag files can be invoked from within JSP pages in the same way Java Standard Tag Library (JSTL) tags are used. Tag files, for instance, can be passed parameters to use, and they can expose values to the calling JSP page, and invoke fragments of JSP code.

A developer implements a tag file using a text file and places it within a special directory of the Web application. From there, its invocation and usages are much like any other custom tag an application may use.

In contrast, custom tags have usually been developed using Java classes that comply ...

Get Beginning 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.