JSP Custom Tags
- What Are JSP Custom Tags?
- How Do JSP Custom Tags Work?
- About This Article
This article gives you a brief overview of JavaServer Pages (JSP) custom tags. It is not a comprehensive JSP custom tag reference, but is meant to support your other resources, deepen your understanding of some aspects of tag libraries, and promote your own experimental approach to JSP technology.
It should certainly not be your only resource for this very rich subject. As you learn about JSP tag libraries, be sure to check the wealth of resources available to you at the main JSP Web site (http://java.sun.com/product/jsp/). You should definitely consult the excellent documentation available from the creators of JSP at http://java.sun.com/products/jsp/docs.html.
What Are JSP Custom Tags?
To see where JSP tag libraries (taglibs) and custom tags fit in, it helps to take a lightning tour of JavaServer Pages. In JSP 1.1, a page is made up of elements and template data. An element is something whose meaning is understood and that the JSP container responds to. Everything that is not an element is template data, such as static HTML content for a browser to display. An element belongs to one of three types:
a directive element
a scripting element
an action
A directive element directs the JSP container in a global manner, such as by controlling aspects of page translation or by providing a URL to locate a needed resource. These elements use syntax based on <%, as follows:
<%@ directive ...%>
Scripting elements make it possible to use scripting languages on the page (in JSP 1.1, only Java). A scripting element can be a declaration, a scriptlet, or an expression. Scripting elements also use syntax based on <%:
<%! declaration %> <% scriptlet %> <%= expression %>
A declaration element creates something that is available to all other scripting elements (such as an instance variable). A scriptlet enables you to put any code into the compiled page, allowing its logic to control and affect other page content. An expression is a complete Java expression that can be evaluated at response time, usually providing a string to be included in the JSP output stream.
An action encapsulates useful functionality. Standard actions are always available in JSP, while custom actions are added to JSP by means of the tag extension mechanism provided. Actions are expressed using an XML-based syntax, as follows:
<x:foo attr1="..." attr2="..." attr3="..." />
Actions can have a body and be expressed as follows:
<x:foo attr1="..." attr2="..." attr3="..." > body </x:foo/>
JSP custom tags are used for adding actions to the built-in ones available in JSP. The JSP 1.1 Specification, Section 2.11, has this to say about actions:
Elsewhere, the JSP Specification also says the following:
JSP custom tags can be added to the built-in JSP tags to extend JSP in a portable manner. Each custom tag packages Java code into a reusable "action" element, which can easily be added to JSP documents. A group of one or more custom tags is made available to JSP documents as a tag library. This component technology extends JSP, furthering its aims of portability, reusability, separation of static and dynamic Web content, and a wide choice of development tools.