Concepts: Java Server Page (JSP) Custom Tag
Topics
Introduction
Custom tags are a mechanism for extending the JSP API. Custom tags enable declarative extensions of JSP pages.
The JSP tags are implemented in Java, but activated in JSP through tags similar to HTML tags. Unlike HTML tags though, JSP tags are processed by the server,
not by the client browser.
Custom tags provide a clean separation of responsibilities between the Web
designers (JSP-developers) and the Java programmers. The Java programmers implement
custom JSP tags. The Web designers select these tags declaratively to activate
the Java implementation.
The JSP designer "invokes" the Java code by declaring tags and the
Java developer develops code in tag handlers, as shown in the following diagram.

To provide a custom tag, the Java developer has to provide two things:
Tag Handler
A tag handler is a class that implements the Tag interface. Two library classes make it easy to provide tag handlers. The two classes are
javax.servlet.jsp.TagSupport and javax.servlet.jsp.BodyTagSupport. The TagSupport class is used for tags that do not have a body;
BodyTagSupport is used for tags with a body.
The following is an example of the simplest possible tag handler implementation.

Tag Library Descriptor
For a JSP to be able to use JSP custom tags, a tag library descriptor has to be created.
A tag library descriptor is an XML-file deployed on the Web server along with the tag handlers.
The following is a simple tag library file containing the HelloWorld tag library
descriptor.
<?xml version="1.0" encoding="ISO-8859-1" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLScheme-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd"
version="2.0">
<tlib-version>1.0</tlib-version>
<short-name>simp</short-name>
<uri> http://www.inferdata.com/taglib/hello-world</uri>
<description>Simple example of a JSP custom tag</description>
<tag>
<name>hello</name>
<tag-class>com.inferdata.taglib.HelloWorld</tag-class>
<body-content>empty</body-content>
<description>Simple hello world tag</description>
</tag>
</taglib>
|