JavaEnterprise JavaXML Tag Libraries For Java

XML Tag Libraries For Java content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

The idea behind tag libraries in JSP looks very good on paper. Tag libraries allow you

to associate Java code to a series of XML-like tags that are packaged as a library. The

user of tag libraries (mainly the JSP developer) will use these tags to develop an

application. The more complete the tag libraries are, the need to write Java code directly

inside the JSP page or via JavaBeans diminishes. Taglibs provide a reusable framework for

J2EE application development, but their key to success is availability. The same way we go

to repositories to retrieve a particular DTD or XML Schema, we need a repository of


The Jakarta taglib project ( is

one such repository of custom tag libraries contributed by various developers. The spectrum

of libraries is very broad including taglibs for JNDI, JDBC, and XSL processing. Every

library is placed in its own directory. The Java class files making up the library are

packaged as a jar file. The Tag Library Descriptor (TLD) file contains the necessary

information for deployment and using the taglib. Reviewing the TLD file is a good way to

get familiarizes with the library, its components and its purpose. Documentation and

example applications using the taglib are packaged as WAR files ready to be deployed to a

servlet/JSP container such as Tomcat.

In the latest distribution, there are 19 libraries. Some of them like request, response,

and session, cover the basic aspects of servlet/JSP API by wrapping these objects and

making them available via the taglib interface. Yet, others provide unique and interesting

functionality. For example, the scrape library can be used to extract portions of other web

pages. The extracted pieces can then be used inside the current page. Many portal engines

are based on this idea. Using tags like <scrape> and <result>

and by modifying their attributes, one can create a JSP page that contains content from

other pages. Here is a code fragment from the sample application extracting weather


 file=forecasts/city/mo/columbia.txt" time="11">
   <scrp:scrape id="weather1" begin="<PRE>" 
         end="</PRE>" anchors="true"/>
   <scrp:scrape id="weather2" begin="<PRE>" 
         end="</PRE>" anchors="false"/>
   <scrp:scrape id="weather3" begin="<PRE>" 
         end="</PRE>" anchors="false" strip="true"/> 

The result can be inserted in the page with a single line like this:

<scrp:result scrape="weather1"/>

Another interesting library is regexp bringing Perl regular expressions to JSP.

<regexp> identifies the regular expression and <text> identifies the text

string in use. Tags like <existmatch>, <substitute>, <split> and

<match> can be used to apply the regular expression to the text string.

The jdbc and jndi libraries provide a gateway to JDBC and JNDI APIs. They encapsulate

the most common aspects of these APIs like opening a connection, performing a query/lookup

and retrieval of the results. It is conceivable that taglibs for EJB and JMS interactions

can be written in a similar manner. To ease integration of XML documents inside a JSP page,

you can use the xsl library. It allows you to transform XML documents via XSL to be used by

the current application or others.

Taglibs represent distinct software components that can easily be reused. The interface

to these reusable components is not JavaBeans or a Java class file, but XML-like tags and

attributes. So while under the hood, there are Java classes and code that make up the

taglib, the user of the taglib will never see them. The user will merely be putting

together tags to make up the application. Of course this model only works when libraries

are available for the application function the user is interested in and repositories such

as Jakarta taglib are a step in the right direction to host and share the various


About the Author

Piroz Mohseni is president of Bita
Technologies, focusing on business improvement through the effective use of
technology. His areas of interest include enterprise Java, XML, and e-commerce

# # #

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories