|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
by Budi Kurniawan | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Object | Type |
|---|---|
request |
javax.servlet.http.HttpServletRequest |
response |
javax.servlet.http.HttpServletResponse |
out |
javax.servlet.jsp.JspWriter |
session |
javax.servlet.http.HttpSession |
application |
javax.servlet.ServletContext |
config |
javax.servlet.ServletConfig |
jspContext |
javax.servlet.jsp.JspContext |
Note that these implicit objects are similar to those available in a JSP page.
This section shows how easy it is to write a tag file and use it. The example consists of one tag file and one JSP page that uses the tag file. The directory structure of the application is depicted in Figure 1.

Figure 1. The directory structure
The tag file is called firstTag.tag and looks like this:
<%@ tag import="java.util.Date"
import="java.text.DateFormat"%>
<%
DateFormat dateFormat =
DateFormat.getDateInstance(DateFormat.LONG);
Date now = new Date(System.currentTimeMillis());
out.println(dateFormat.format(now));
%>
As you can see in the above listing, a tag file looks similar to a JSP page. The firstTag.tag file
has a tag directive with two import attributes and some script. The output of this tag file
is the current date in long format. To use this tag file as a tag extension, all you need to
do is save it in the WEB-INF/tags directory of your application. The tag file name is
important because it indicates the tag name. Therefore, for the tag file with the name
firstTag.tag, the tag name will be firstTag.
Here's the firstTagTest.jsp page that uses the firstTag.tag file.
<%@ taglib prefix="easy"
tagdir="/WEB-INF/tags" %>
Today is <easy:firstTag/>
You can invoke the firstTagTest.jsp page using this URL (assuming you use Tomcat 5 on port 8080):
http://localhost:8080/tagfiles/firstTagTest.jsp
The result is shown in Figure 2.

Figure 2. Tag file in action
Just like JSP pages, tag files can use directives to control how the JSP container will compile or interpret the tag files. Tag file directives have the same syntax as the directives you can use in JSP pages:
<%@ directive (attribute="value")* %>
The asterisk (*) means that what is enclosed in the brackets can be repeated zero or more times. The syntax can be rewritten in a more informal way, as follows:
<%@ directive attribute1="value1"
attribute2="value2" ... %>
Attributes must be enclosed by single quotes or double quotes. White space after the opening <%@ and before the closing %> is not required, but can improve readability.
Some of the directives you can use in a tag file are the same as those for JSP pages,
except that you don't have a page directive. Instead of the page directive, you have the
tag directive. Also, in tag files, there are two more directives: attribute and variable.
Table 2 lists all of the directives that can appear in a tag file.
Table 2. Tag file directives
| Directive | Description |
|---|---|
tag |
This directive is similar to the page directive for JSP pages. |
include |
Use this directive to include other resources from the tag file. |
taglib |
Use this directive to use a custom tag library from inside of the tag file. |
attribute |
Use this directive to declare an attribute in a tag file. |
variable |
Use this directive to define a variable that you can expose to the calling JSP page. |
Each of the directives is given below.
tag DirectiveThe tag directive is similar to the page directive you can use in a JSP page. Here is the
syntax of the tag directive:
<%@ tag (attribute="value")* %>
The syntax can be expressed in the following more informal form:
<%@ tag attribute1="value1" attribute2="value2" ... %>
The list of attributes for the tag directive is given in Table 3. Note that all tag directive attributes are optional.
Table 3. The tag directive's attributes
| Attribute | Description |
|---|---|
display-name |
A short name to be displayed by an XML tool. The default value is the tag filename, without the .tag extension. |
body-content |
The information about the body content of this tag. The value can be empty,
tagdependent, or scriptless (default). |
dynamic-attributes |
Indicates support for dynamic attributes. The value identifies a scoped attribute in which to place a Map containing the names and values of the dynamic attributes passed during this invocation. |
small-icon |
A context-relative path (or a path relative to the tag source file) of a small image file to be used by XML tools. You don't normally use this attribute. |
large-icon |
A context-relative path, or a path relative to the tag source file, of an image containing a large icon to be used by XML tools. You don't normally use this attribute, either. |
description |
A string describing this tag. |
example |
An informal description of an example of a use of this action. |
language |
The scripting language used in the tag file. The value for this attribute for
the current version of JSP must be "java". |
import |
Used to import a class or an interface or all members of a
package. The same as the import attribute in the page directive.
|
pageEncoding |
Describes the character encoding for this tag file. The value is of the form
CHARSET, which must be the IANA name for a character encoding. This
attribute is the same as the pageEncoding attribute of the page directive. |
isELIgnored |
Indicates whether EL expressions are ignored or evaluated. The default
value for this attribute is false, which means EL expressions are
evaluated. This attribute is the same as the isELIgnored attribute of the
page directive. |
Except for the import attribute, all other attributes can only appear once within a tag
directive or in multiple tag directives of the same tag file. For example, the following tag
file is invalid because the body-content attributes appear more than once in multiple tag
directives:
<%@ tag display-name="Your first tag file"
body-content="scriptless" %>
<%@ tag body-content="empty" %>
The following is a valid tag directive because the import attribute can appear as many
times as desired.
<%@ tag import="java.util.Enumeration"
import="java.util.Iterator" %>
The following is also valid:
<%@ tag body-content="empty"
import="java.util.Enumeration" %>
<%@ tag import="java.sql.*" %>
Pages: 1, 2 |
Easy Custom Tags with Tag Files, Part 2
In part two of his series on JSP Tag Files, Budi Kurniawan continues his coverage of this hassle-reducing technology. This week he looks at variables, jsp:doBody, jsp:invoke, and how to package your tag files in a jar file.
View all java.net Articles.
|
|