Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Encapsulate reusable functionality in JSP tags

Build your own custom JSP tag with Tomcat

  • Print
  • Feedback

Page 3 of 3

You should now compile the above class, ensuring that you have the Servlet/JSP classes in your classpath. Those can be found in TOMCAT_HOME/lib/servlets.jar.

Registering the tag library

Now that you've defined your tag and written its supporting class, you now need to tell the Web application where to find the tags by registering the taglib with it. The Web application configuration file (web.xml) is stored under the web-inf directory. Open up the web.xml file and insert the following XML fragment. The ordering of XML elements in the file is important, so the fragment below should be placed before or after the existing example taglib.

    <taglib>
        <taglib-uri>
            http://www.mycompany.com/taglib
        <taglib-uri>
        <taglib-location>
           /WEB-INF/taglib.tld
        <taglib-location>
    </taglib>


That tells the Web application where to find the taglib descriptor and the URI that refers to the taglib. Again, that doesn't have to be a real URI, but it should be the same as the one specified in the taglib.tld file that you created above.

Using the tag

The final step in this example is to actually use the tag in a JSP page. To do that, you'll write a very simple JSP page as illustrated below. It should be saved with the normal .jsp extension and can be placed in the examples root directory (TOMCAT_HOME/webapps/examples).

<html>
<head>
<%@ taglib uri="http://www.mycompany.com/taglib" prefix="examples" %>
</head>
<body>
The file is <examples:size uri="/jsp/index.html"/>.
</body>
</html>


The taglib directive tells the JSP engine that your JSP wishes to use a taglib referred to by the specified URI. The taglib directive also takes a prefix that tags on the page will use. For the sake of this example, I'll be using the examples prefix.

On the JSP, your tag consists of the prefix (like an XML namespace), the tag name, and any attributes. It's written by using normal XML notation, and the example above uses the shorthand for combining the opening and closing tags. The URI that you pass your tag just points to an HTML file provided with the JSP examples.

All you need now is to start up Tomcat and request the JSP from your browser. What you should notice is that your custom tag has been completely processed and substituted with the HTML that it generated dynamically. If you take a look at the HTML source via the browser, you should also notice that no trace of your tag remains.

Summary

I hope this article has given you an introduction and overview of JSP tag libraries. I've covered some of the tag basics, illustrated when you would use custom tags over JavaBeans, and presented a short example of how to build and deploy a tag.

Although there's a lot more to taglibs than has been presented here, the article aimed to give you a good starting point from which to explore further and begin to develop your own taglibs. Custom tags are a very powerful addition to the current JSP specification and a great mechanism for wrapping up reusable functionality on your JSPs.

About the author

Simon Brown is a distributed systems designer and developer with more than three years of commercial Java experience. He has considerable knowledge of large-scale distributed developments for major clients, having designed and implemented a number of important Java systems, largely in the banking and media sectors. Considered one of Synamic's more senior Java specialists, he acts as technical lead and mentor to other team members and presents at Java events, including a BOF session at JavaOne 2000. Simon is a Sun-certified enterprise architect for J2EE and developer for Java 2.

Read more about Core Java in JavaWorld's Core Java section.

  • Print
  • Feedback

Resources