The include Directive
The include directive for a tag file is the same as the include directive for a JSP page.
You use this directive to include the contents of other files in the current tag file. It is
useful when you have a common source that will be used by more than one tag file.
The included source can be static (such as an HTML file) or dynamic (another tag file).
As an example, the includeDemoTag.tag page below shows a tag file that includes
one static resource (included.html) and one dynamic resource (included.tagf).
This tag file shows the use of the include directive.
The first include directive demonstrates how you can include
a static resource called included.html.
<br/>
Here is the content of included.html:
<%@ include file="included.html" %>
<br/>
<br/>
The second include directive includes another
dynamic resource: included.tagf.
<br/>
<%@ include file="included.tagf" %>
The included.html file looks like this:
<table>
<tr>
<td><b>Menu</b></td>
</tr>
<tr>
<td>CDs</td>
</tr>
<tr>
<td>DVDs</td>
</tr>
<tr>
<td>Others</td>
</tr>
</table>
The included.tagf file looks like this (note that the recommended extension for a tag file is .tagf):
<%
out.print("Hello from included.tagf");
%>
To test the includeDemoTag.tag file, use the includeDemoTagTest.jsp page:
<%@ taglib prefix="easy"
tagdir="/WEB-INF/tags" %>
<easy:includeDemoTag/>
You can invoke the includeDemoTagTest.jsp page with the following URL:
http://localhost:8080/tagfiles/includeDemoTagTest.jsp
The result is shown in Figure 3.

Figure 3. Including other resources from a tag file
The taglib Directive
You can use other custom actions from your tag file using the taglib directive. The taglib directive has the following syntax:
<%@ taglib uri="tagLibraryURI"
prefix="tagPrefix" %>
The uri attribute specifies an absolute or relative URI that uniquely identifies the tag
library descriptor associated with this prefix.
The prefix attribute defines a string that will become the prefix to distinguish a custom
action.
With a taglib directive, you then can use a custom tag of the following format for a
custom tag that does not have a content body:
<prefix:tagName/>
Or, you can use the following format for a custom tag that has a content body:
<prefix:tagName>body</prefix:tagName>
As an example, consider the taglibDemo.tag file:
<%@ taglib prefix="simple"
tagdir="/WEB-INF/tags" %>
The server's date: <simple:firstTag/>
It uses the firstTag.tag file from the first example to display the server's date. The
taglibDemo.tag file is used in the taglibDemoTest.jsp page:
<%@ taglib prefix="easy"
tagdir="/WEB-INF/tags" %>
<easy:taglibDemo/>
You can invoke this JSP page using the following URL:
http://localhost:8080/tagfiles/taglibDemoTest.jsp
The result is shown in Figure 4.

Figure 4. Using the taglib directive in a tag file
The taglib directive in a tag file is the same as the taglib directive in a JSP page.
The attribute Directive
The attribute directive supports the use of attributes in a tag file. It is equivalent to the
attribute element in a tag library descriptor. Here is the syntax of the attribute directive:
<%@ attribute (attribute="value")* %>
The syntax can be expressed in the following more informal form:
<%@ attribute attribute1="value1"
attribute2="value2" ... %>
The list of attributes for the attribute directive is given in Table 4. Note:
The only required attribute for the attribute directive is the name attribute.
Table 4. The attributes for the attribute directive
| Attribute |
Description |
name |
The name for the attribute that this tag file accepts. The value for the name attribute must be unique throughout the current tag file. |
required |
Indicates whether this attribute is required. The value can be true or false (default). |
fragment |
Indicates whether this attribute is a fragment to be evaluated by the tag handler, or a normal attribute to be
evaluated by the container prior to being passed to the tag handler. The value is either true or false (default). The value is true if this attribute is to be evaluated by the tag handler. |
rtexprvalue |
Specifies whether the attribute's value may be dynamically calculated at runtime by a scriplet expression. The value is either true (default) or false. |
type |
The type of the attribute value. The default is java.lang.String. |
description |
The description of this attribute. |
As an example, consider the encode.tag file below, which can be used to HTML
encode a String. This encode tag defines one attribute, input, which is of type
java.lang.String.
<%@ attribute name="input"
required="true" %>
<%!
private String encodeHtmlTag(String tag) {
if (tag==null)
return null;
int length = tag.length();
StringBuffer encodedTag =
new StringBuffer(2 * length);
for (int i=0; i<length; i++) {
char c = tag.charAt(i);
if (c=='>')
encodedTag.append("<");
else if (c=='>')
encodedTag.append(">");
else if (c=='&')
encodedTag.append("&");
else if (c=='"')
encodedTag.append(""");
else if (c==' ')
encodedTag.append(" ");
else
encodedTag.append(c);
}
return encodedTag.toString();
}
%>
<%=encodeHtmlTag(input)%>
To test the encode.tag file, use the encodeTagTest.jsp page:
<%@ taglib prefix="easy"
tagdir="/WEB-INF/tags" %>
<easy:encode input="<br/> means changing line"/>
You can invoke the encodeTagTest.jsp page using the following URL:
http://localhost:8080/tagfiles/encodeTagTest.jsp
The result is shown in Figure 5.

Figure 5. Using attributes in a tag file
Summary
In this first part of the series, you have seen how tag files alleviate the problem of
writing tag extensions. In fact, writing tag files is easy, because you don't need a tag
library descriptor or compile the tag handler. Deployment is also simple: you just need to
copy the tag files under the WEB-INF/tags directory of your application. Part two of this
article will discuss the more advanced aspects of tag files, including the <jsp:doBody> and
<jsp:invoke> standard actions that are new to JSP 2.0.