September 30, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Hypewatch: Pre-built JSP Tag Libraries

  • November 12, 2001
  • By David Fox
  • Send Email »
  • More Articles »

There's been a lot of hype, to be sure, surrounding JavaServer Pages tag libraries. Some people get mighty excited at their mere mention. But JSP taglibs really are one of those rare software ideas that are incredibly both easy to grasp and use for both the hardened back-end Java developer and HTML-guru Web page producer.

David Fox

Basically, a JSP tag library is a just a way of running a big ol' Java method right inside a JSP page. Of course, JSP gives you the ability to drop Java code right in the page anyway. But using real chunks of Java code mixed in with HTML layout is a frightening experience. There's a world of conceptual difference between an HTML coder and a Java coder. An HTML coder loves using little tags surrounded by their little brackets, and enjoys thinking of the page in terms of clean layout of fonts, graphics, styles, and tables. A Java coder, on the other hand, is only at home wading through lots and lots of sticky, gooey (not to mention GUI) code.

Additionally, using Java code directly in a JSP page is a waste of time and space if the same method needs to be put throughout hundreds of different pages. Sure, you can use JSP's <%include/> directive, but then you need to set up special variables for passing parameters and watch out for all sorts of other snafus.

The JSPTL includes goodies that will help you iterate through dynamic database tables or scrape data from external Web pages for you to "borrow" on your own pages.

Several companies sell interesting sets of tag libraries. Whether these are worth the price depends on your particular needs. For example, Coldjava offers 60 different taglibs. For example, one tag pops up a calendar. Another easily deals with cookies. Some tags create menu and navigation trees. Others parse SOAP, DOM, XSL, SMS, WML, and other formats. There's even one to checks if a credit card number looks valid. Coldjava's products are easy to test out and free for non-commercial use.

If you look hard enough, however, you'll probably find plenty of public domain tags out there to suit your needs. Sun, in fact, is developing a standard set of tag libraries called the JavaServer Pages Standard Tag Library (JSPTL). An implementation-in-progress is open source and hosted by the Apache Project. The JSPTL includes goodies that will help you iterate through dynamic database tables or scrape data from external Web pages for you to "borrow" on your own pages.

It also comes with its own Java-like expression language allowing an easier way for an HTML guru to throw simple conditionals and other script-like functions on the page. For example:

<jx:if test="$user.visitCount == 1"> 
     This is the first time you came use. Welcome to the site! 
</jx:if>

The hardest part of taglibs is installing them. Basically, you need to add a line to your JSP application server's /WEB-INF/web.xml file that points to the taglib descriptor (TLD) file:

<taglib>
  <taglib-uri>http://jakarta.apache.org/taglibs/mylibrary</taglib-uri>
  <taglib-location>/WEB-INF/mylibrary.tld</taglib-location>
</taglib>

The TLD contains a list of all the custom tags, and points to Java classes that handle the tags. You can get more info about how to construct your own TLD and related classes from Sun's site.

The JSP file itself then should start off with the line:

<%@ taglib uri="http://jakarta.apache.org/taglibs/mylibrary" prefix="mylib" %>

All custom tags will then use the given prefix:

<mylib:customtag>Hello</mylib:customtag>

You've been tagged! Go forth and make life easier for your Web production team.

About the Author

David Fox is the author of numerous books and articles about cyberculture and technology.






Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel