One of the most exciting features about JSP is the ability tobuild and use custom "tag libraries" in your JSP applications. In thisarticle, find out why tag libraries are a Good Thing, and read about how toget and install custom tag libraries for common tasks.
At this point, you can begin using the new tag library in your JSP pages. First, declare it with the "taglib" directive - this directive must appear before any custom tags in the page.
The URI is the unique identifier for the tag library, and
must match the URI specified in "web.xml", while the prefix appears in every call to a custom tag, and is used to distinguish between tags from different libraries in the same page.
Once the library has been declared, you can begin using custom tags in your JSP scripts. Consider the following example, which uses a custom tag from the DATETIME library to calculate the number of seconds elapsed since January 1 1970.
<html>
<head>
</head>
<body>
<%@ taglib uri="http://jakarta.apache.org/taglibs/datetime-1.0"
prefix="popeye" %>
The number of milliseconds since January 1, 1970 is <popeye:currenttime/>
</body>
</html>
And the output is:
The number of milliseconds since January 1, 1970 is 987165837280
What if you simply want the current date and time? By
combining the <currenttime> tag with the <format> tag, the DATETIME library makes it a snap!
<html>
<head>
</head>
<body>
<%@ taglib uri="http://jakarta.apache.org/taglibs/datetime-1.0"
prefix="popeye" %>
The current date and time is
<popeye:format pattern="hh:mm EEE MMMM dd yyyy">
<popeye:currenttime/>
</popeye:format>
</body>
</html>
In case you're wondering, the EEEs and MMMs you see there are
formatting codes, used to define the format in which the date and time is to be printed. Here's the output:
The current date and time is 06:22 Fri April 13 2001
The example above also illustrates how some tags can be
nested within one another - this can make for powerful combinations, and is one of the clever things about this architecture.
How about generating a list of days or months? The DATETIME library's got you covered with its <weekdays> and <months> tags...