Home arrow Java & J2EE arrow Page 4 - The JSP Files (part 8): Tagged And Bagged

Meeting Popeye - Java

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.

  1. The JSP Files (part 8): Tagged And Bagged
  2. Playing Tag
  3. Looking Inside
  4. Meeting Popeye
  5. You've Got Mail!
  6. Applet Antics
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 5
June 11, 2001

print this article


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.

<%@ taglib uri="http://jakarta.apache.org/taglibs/datetime-1.0" prefix="popeye" %>
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...

<html> <head> </head> <body> <%@ taglib uri="http://jakarta.apache.org/taglibs/datetime-1.0" prefix="popeye" %> <form> Select a day <select name="weekday"> <popeye:weekdays id="day"> <option value="<jsp:getProperty name="day" property="dayOfWeek"/>"> <jsp:getProperty name="day" property="weekday"/> </popeye:weekdays> </select> </form> </body> </html>
The DATETIME library comes with a whole bunch of other useful tags too - take a look at the documentation to see the various other features available.

>>> More Java & J2EE Articles          >>> More By Vikram Vaswani and Harish Kamath, (c) Melonfire

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates


Dev Shed Tutorial Topics: