Jakarta Taglibs
If you need to send mail from a JSP, the Mailer taglib
can help you out. Mail can be sent in three ways; using the name of
the SMTP host, using the name of a JNDI Resource for a JavaMail
Session, or using the name of a JNDI Resource for a JavaMail
MimePartDataSource. Tags for including all necessary
message parts, including body, header, to, cc, subject, and send, are
included. Using Mailer might look like
<!-- Create a message by using the SMTP host, specified
in the -->
<!—- server attribute -->
<!-- The body of the e-mail is supplied in the -->
<!-- message tag. The send tag is necessary to send the
message. -->
<<a href="mailto:mt:mail%20server=%22switchbacksoftware.com%22%20to=
%22developers@switchbacksoftware.com%22%20from=
%22sspielman@switchbacksoftware.com%22%20subject=
%22Mark%20Twain%20Quote">
mt:mail server="switchbacksoftware.com"
to="developers@switchbacksoftware.com"
from="sspielman@switchbacksoftware.com"
subject="Mark Twain Quote</a>">
<mt:message>Always do right. This will gratify
some people and astonish the rest.
</mt:message>
<mt:send/>
</mt:mail>
There are four separate taglibs that are focused on tags related to
Page, Request, Response, and Session. If you need to access the
PageContext, or attributes within a page-scope, the
Page taglib will provide tags for getting, setting, and
testing of attributes. The Request lib currently has 28
tags that provide a variety of actions. They can be grouped into the
following categories: session, attributes,
cookie, header, and
queryStrings. Basically anything that you want or need
to do with a request can be handled with this tag library. The
Response lib can be used to set cookies, encode,
redirect, set header information, or send status in a response. Tags
in the Session lib are useful for getting, setting, and,
testing attributes in the session as well as for invalidating and
setting timeout limits.
The regexp custom tag library contains tags that can be used to
perform Perl-like regular expressions. These tags implement the three
most common Perl 5 operations involving regular expressions:
- [m]/pattern/[i][m][s][x]
- s/pattern/replacement/[g][i][m][o][s][x]
- and split()
As with Perl, any non-alphanumeric character can be used in lieu of
the slashes. In order to use this taglib you not only need a servlet
container, but you also need the Jakarta ORO Perl Regular Expression
package.
Scraping or extracting content from web documents is common these
days. After your JSP scrapes a document for the first time, the
results can be cached for subsequent JSP requests using the
Scrape taglib. These results are returned unless the JSP
determines that the document must be rescraped using predefined
logic. By specifying the URL and text anchors to mark the beginning
and ending of content to be scraped, you can easily provide
information from other sites, such as stock quotes, in your pages.
The Utility taglib has samples of some basic
tags. Some of which, like the hello tag, are strictly for
demonstration purposes, while others can actually be used like the
copy, paste, and showsource
tags. There are also conditional and looping tags.
XSL (eXtensible Stylesheet Language) uses XSLT to perform
transformations of XML documents. The XSL taglib provides tags to
apply a specified URI that points an XSL file to an XML document
(which is itself specified by a URI). The InsertWithXSL
will insert the transformed result into the writer of the JSP. This
taglib requires the binary distributions of the Apache Xerces XML
parser as well as the Apache Xalan XSL processor. Both can be found on
the Apache XML site.
Last but not least is the Xtags taglib. This is a
brand new taglib that was just added to the Jakarta project by Citria
Ltd. XTags implement an XSLT-like JSP tag library to allow navigating,
processing, and styling of XML documents directly in JSP. XTags makes
heavy use of the XPath expression language and is built on top of
dom4j. Using this taglib requires dom4j and log4j. To be able to use
the <xtags:style> tag, JAXP is required along with
an XSLT implementation such as xalan.jar and
crimson.jar, both available from http://xml.apache.org.
There is also an SQL sample taglib, but it's no longer
supported. So if you want the SQL functionality, try using the DBTags
mentioned above.
Summary
So there you have it. Hours of coding (not to mention debugging)
saved by using one of the many available taglibs from the Jakarta
project. Chances are some of them have tags that you could use in your
current JSP application. Why reinvent the wheel?
The value of open source is that you can use (and contribute) code
that has been run, reviewed, and tested by many. Let's rephrase that:
if you are using custom tag libraries in your JSP, and you are not
using at least one of the Jakarta taglibs in the project, you may be
spending money on development cycles that are both unnecessary and
wasteful. Simple enough.
Sue Spielman
is an associate editor for ONJava.com, covering JSP and Servlets technologies. She is also President and Senior Consulting Engineer
for Switchback Software LLC.
Read more JSP and Servlets columns.
Return to ONJava.com.