Tải bản đầy đủ (.pdf) (13 trang)

The JSP Files (Part 8) - Tagged and Bagged

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (26.52 KB, 13 trang )

The JSP Files (part 8): Tagged And Bagged
By Vikram Vaswani and Harish Kamath
This article copyright Melonfire 2000−2002. All rights reserved.
Table of Contents
The Beginning Of The End................................................................................................................................1
Playing Tag..........................................................................................................................................................2
Looking Inside.....................................................................................................................................................3
Meeting Popeye...................................................................................................................................................4
You've Got Mail!.................................................................................................................................................7
Applet Antics.......................................................................................................................................................9
The JSP Files (part 8): Tagged And Bagged
i
The Beginning Of The End
Over the past few weeks, we've taken you on a guided tour of the intricacies of JSP, beginning with basics
like conditional statements and loops, and quickly moving on to more complex things like form processing,
session management, and error handling.
But all good things must come to an end − and so, in this final episode of The JSP Files, we'll be briefly
touching on a few other facets of this powerful server−side scripting language.
The Beginning Of The End 1
Playing Tag
One of the most exciting features about JSP is the ability to build and use custom "tag libraries" in your JSP
applications. A "tag library" is a reusable block of JSP code, typically written to replace Java code with
easy−to−read−and−understand tags (similar in appearance to HTML markup). Once written, these tag
libraries can be used over and over again, thereby bringing a new element of reusability to the language.
In addition to reusability, tag libraries also offer substantial advantages from the maintenance point of view.
Since tag libraries are largely defined using XML−type markup, they make it possible to separate application
presentation from application logic − which, in turn, implies that designers and developers working on Web
applications can use a tag without worrying about how and why it works. This separation between program
code and final layout is something most designers would kill to have − and it's now available to almost anyone
who knows how to string together Java and JSP code.
Another advantage of the separation discussed above, is the ease of use when it comes to adding new features


to a tag library, or making changes to existing features. Since a tag library is portable and reusable, a change
made to it will immediately reflect in all JSP pages using that tag library. Similarly, a new feature added to the
tag library becomes immediately available to all pages carrying that library.
Tag libraries are slowly catching on, with many JSP developers releasing custom tag libraries for free online.
These libraries are typically designed for specific tasks − database connectivity, string manipulation and the
like − and they provide a rich vein for JSP developers to mine for their own projects. After all, why waste
time writing Java code to send email messages when you can find a feature−rich, properly−tested and free tag
library to do the same thing online?
Playing Tag 2
Looking Inside
A tag library is made up of several components. First comes the tag class itself, written in Java − this contains
all the behind−the−scenes code which makes the tag function. This class will be referenced in the tag library
descriptor, and constitutes the meat of the sandwich we're building.
Next, you need a "tag library descriptor", an XML file usually identified with the .TLD file extension. This
TLD contains information on different aspects of the tag library: the name of the tag, and of the Java class
associated with it, a short description of functionality, and optional attributes and body content.
Both these components are usually packaged into a JAR file for easy distribution.
Now, we're not going to get into the nitty−gritty of writing a tag library here − there are already a whole
bunch of tutorials which discuss this in detail, and links to some are included at the end of this article. What
we will do, however, is illustrate how a tag library can be incorporated into your JSP document, and how it
interacts with the rest of your script.
In order to illustrate this, we'll be using the Jakarta Project's DATETIME tag library, designed to manipulate
date− and time−stamps. You can download a copy of this library from
and you can find a number of other freeware tag
libraries at one of the larger repositories on the Web.
Once you've downloaded a copy of the library, you need to tell Tomcat about it before you can begin using it.
The first step here is to decide the context within which you plan to use the library − for purposes of this
example, we will assume the context is "/test/". Next, copy the TLD file from the distribution into the
context's "web−inf/" directory, and the main JAR file into the context's "web−inf/lib/" directory.
The last step here is to open up the "web.xml" file which resides in the "web−inf/" directory, and alter it to

reflect the new tag library − this is accomplished via the <taglib> directive.
<taglib>
<taglib−uri> /><taglib−location>/WEB−INF/datetime.tld</taglib−location>
</taglib>
This element specifies the location of the tag library, as well as a unique URI used to reference it.
Looking Inside 3

×