Why are tag libraries popular these days? One reason is that JSP development often consists of many tasks that are common and repeatable; an obvious solution is to incorporate common and functionality into tag libraries, which can then be reused over the course of many projects. Wouldn't it be great if there were existing libraries that everyone could use without having to write them? Welcome to the Taglib project of Jakarta. This article will discuss the Taglib project and how you can take advantage of some great open source tag libraries.
If you are new to JSP development, custom tag libraries are something that you'll want to become familiar with. Custom tags are a way to separate presentation from logic. If you have tasks that are being done repetitively they are probably good candidates to be incorporated into a tag library. Incorporating them into the tag library makes life easier for the page designer because if there are logic changes, they can be handled within the tag library. There is no need to actually change the JSP. This also means that if there are page layout changes, there is no need to change the logic. Using tag libs makes for more maintainable JSP.
Custom tags can modify content within a tag body and have access to the application context. Custom tags can be used to generate dynamic content as well as to implement flow control. Additionally, tags can interact with other tags as well as be nested within each other. For a full description of using and building your own custom tag libraries, see my previous two articles, Building Custom Tag Libraries, and Advanced Custom Tag Libraries.
The Jakarta Taglib project is an open source repository of tag libraries. We'll go into a brief description of what's available, what each one does, and situations in which you might want to use each. For those who aren't familiar with open source, it might be time to find out more about it.
|
Also in JSP and Servlets: Learning the New Jakarta Struts 1.1, Part 2 Learning the New Jakarta Struts 1.1, Part 1 JSP Standard Tag Libraries, Part 2 JSP Standard Tag Libraries, Part 1 Learning Jakarta Struts, Part 3
|
The Jakarta Project is actually an umbrella project for many other projects which aim to deliver commercial-quality Java server programs. All of the projects are open source and are developed by programmers throughout the world. Anyone can participate, but there are guidelines as to how source code gets updated in the CVS repository. There is always a need for more open source programmers, so maybe this article will add a few new faces to the contributing developer list.
You may already have heard of some Jakarata projects: Tomcat, a JSP/Servlet engine, ANT, a make-like build environment, or Structs, a framework based on the MVC pattern for JSP. This article focuses on the Taglib project, but you should take the time to become familiar with some of the others. There is some useful stuff out there if you know where to look. For a complete list of Jakarta projects, go to http://jakarta.apache.org.
It is possible to download just binaries or complete source code for all of the projects. Sometimes all you need is a binary or jar file. If you need to modify any of the functionality, obviously you will want the source code. Each project details how to retrieve and build from source. And it's a relatively straightforward process.
|
|
Related Reading
|
Let's look at the Jakarta tag libraries that are available and what each provides.
At the time of this writing there were twenty supported libraries and one unsupported library. There seem to be new libraries added constantly. If you are interested in using a specific library, it is best to check the Jakarta site for specific installation instructions and requirements for that taglib.
The application taglib contains tags that can be used to access
information contained in the ServletContext. Tags that
are defined in application scope as well as application init
parameters can be accessed. Tags are included for setting, getting,
removing, and testing of attributes. Looping and value testing are
provided for init parameters. A sample tag from this library looks
like
<app:equalsattribute name="sue"
match="oreilly">
The application attribute with name sue matches the string
"oreilly".
</app:equalsattribute>
The Bean Scripting Framework (BSF) is used for incorporating
various scripting into Java applications and applets. Supported
scripts in BSF include Javascript, VBScript, Perl, Tcl, Python,
NetRexx, and Rexx. If you are interested in more details pertaining to
BSF you can check out IBM's Bean Scripting Framework
page. The BSF taglib allows any of these languages to be used in
JSPs. There are scriptlet tags that are the equivalent of
<% in a JSP, and expression tags that are equivalent
to <%=. You specify which language you are using to
the tag.
The DateTime taglib makes dealing with dates and times
easier. It contains tags that handle formatting, generating dates,
using time zones, dealing with localization, and parsing. It doesn't
get much easier than
<%-- Display the current time formatted --%>
The current time is: <dt:format pattern="MM/dd/yyyy
hh:mm"><dt:currenttime/></dt:format>
DBTags, formerly called JDBC, contain tags that can be used to read and write to an SQL database. It includes connection management, creating and executing statements, prepared statements, testing and formatting of result sets. To say that this taglib could save you hours of coding is probably an understatement; having some understanding of JDBC helps.
The I18N taglib is useful for creating
internationalized web applications. The bundle tag is used for loading
the resource bundle based on locale, while the message tag is used for
looking up keys in the bundle.
The Input taglib allows the user to present HTML
<form> elements that are tied to the
ServletRequest. Tags in this lib allow for default values
to be placed on forms when necessary. This lib is optimized for a
special case -- tying data to the ServletRequest object
and providing the standard form elements.
The IO library allows URLs and HTTP requests to be performed. So
JSP can be used to perform HTTP GET or PUT
operations and to make XML-RPC and SOAP requests. Currently the IO
library supports any protocol supported by the Java URL class. This
includes file system, FTP, HTTP, HTTPS, XML-RPC, and SOAP. The
<jsp:include> is only capable of including a
servlet that is in your current web application. The
<io:request> included in this taglib can be used to
make "server side include" style calls to any web server anywhere for
any resource. There is also a Unix-like pipe tag that can be used to
pipe between tags that can't pipe themselves.
The JNDI Library creates an instance of a
javax.naming.Context from attributes providing some of
the standard values. Attributes can include URL, initialFactory, id,
and scope. You can also do lookups, searches, and listings of
contexts.
The JSP Spec taglib is the resource to use if you are looking for examples of how to do the things described in version 1.1 of the JSP specification. This library focuses on techniques more than on production scenarios. It can be useful to those just coming up to speed on writing custom tag libraries.
|
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:
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.
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.
Copyright © 2007 O'Reilly Media, Inc.