J2EE Custom Simple Tags Part 1

Note: This article assumes you have done some Java web development in the past, and you are capable of compiling and executing web applications with J2EE.

Overview

J2EE has done some amazing things in progressing the creation of custom tags. Now it is fairly easy to integrate any custom HTML tag within your application further separating the view from designers. This article will show you how to implement a custom HTML tag that calculates the square of a number by simply placing <math:square num=“12” /> into your web application. Hopefully, you can feel the power with this simple feature.

Creating the class

Every custom tag extends SimpleTagSupport. This is a class implemented by the J2EE spec which allows a class to output information to a jsp. So, lets get right to coding. Create a class in your web application that extends javax.servlet.jsp.tagext.SimpleTagSupport. If you are compiling by hand, the class file must go in the package structure under /WEB-INF/classes/ i.e. /WEB-INF/classes/com/cyberkruz/test/NewMath.class

Every attribute in your custom tag must have a Java Bean mutator property to go with it. This is handled with reflection to send the parameter to your class.

Configuring the application to use the class

Now, all we have to do is let our application know that the new tag is there. To do so, we can set up a TLD file which looks much like the document descriptor for configuring servlets. So lets do it! Create a new file called MyTags.tld and place it in the /WEB-INF directory. Then type the following:

This file configures our web application to use our new custom tag. Multiple tags can be placed and mapped with this single file. The taglib directive just specifies the schemas for this particular file and version. The uri specifies what location these tags are being mapped to. Under the tag element, the description and name should be pretty straight forward. The tag-class element points this tag to the class that we wrote previously. the body-content is stating that we don’t want inline scripting for this element. Now the attribute tag is fairly interesting. While optional, it maps the num variable to our setter in the class we specified. Then, it says we must have it there (required element) and it can be specified at run time (rtexprvalue).

Now all we have to do is use it!

Place our new tag in a jsp

Allowing our jsp to use the custom tag is the easiest part. It only requires a single declaration.

<%@ taglib prefix=“math” uri=“myTags” %>

The taglib keyword tells the jsp what we are trying to do. The prefix is what we want to put before the tags when we call them, and the uri points to the uri specified in the tld file. This is going to load every tag that is configured in that tld file that we made previously.

Now, let’s put it in a jsp. Create a jsp file called SimpleTest.jsp and place it in your web application. Place the following in your jsp:

Run it on your web server and… YAY! This is a base introduction to what you can do with custom simple tags. More information about how to use these is below. Enjoy!

More information

http://java.sun.com/j2ee/1.4/docs/tutorial/doc/JSPTags7.html