JSP Tag Files

I’ve written my fair share of JSP 1.0 tags, the hell of deciding whether to extend TagSupport or extend BodyTagSupport and implement BodyTag, writing tons of StringBuffer append statements left alot to be desired. Almost all of the tags that I wrote wrapped up some piece of presentation logic with a bunch of HTML, which meant alot of header.append(“html content”) type statements. Luckily, it seems that there were other people who had bad experiences with the JSP 1.2 specification (or at least they understood that it needed improvement) which lead to the JSP 2.0 specification. Among other things, JSP 2.0 includes the ability to create JSP tags *using* JSP syntax, which means that front-end/GUI developers that don’t know Java can now easily create reusaeable tags without having to write and compile a Java class. It’s brain dead simple. Create a file in /WEB-INF/tags/ OR /META-INF/tags/ OR create a jar of all your tag files and place them in /WEB-INF/lib/, save it with a .tag or a .tagx extension, enter some content (ie: “Hello World!”) and then you can invoke the tag in a JSP by importing the taglib:

<%@ taglib prefix="tags" tagdir="/WEB-INF/tags" %>

and then the tag itself:

<tags:helloWorld/>

The prefix is determined by the taglib directive (in this case the prefix is ‘tags’) and the name of the tag is determined by the file name of the tag that you saved.

Using attributes is just as easy. At the beginning of the tag file you can add as many attribute directives as you want:

<%@ attribute name="title" %>

and then write their values to the screen:

<strong>${title}</strong>

I’m just scratching the surface though. There are 2 great articles on java.net to get you started if you want to learn more:

Easy Custom Tags with Tag Files, Part 1
Easy Custom Tags with Tag Files, Part 2

You can also read the JSP 2.0 specification (JSR-152).

As an aside, one of the things I always wanted to be able to do with ColdFusion was to deploy the custom tags I wrote as part of the application, in a non web-accessible directory without having modify the custom tag path through ColdFusion administrator. For instance, if you deploy a ColdFusion application to a shared host and you want to use your own custom tags, you have to either store the custom tags in the same directory as the script that you call them from (which does not help much at all), you an use cfmodule, which is a kludge, or you can ask the ISP/host to either add the directory where your tags live to the custom tag path or to put your custom tags into the existing custom tag path, neither of which is an attractive (or secure!) option. I haven’t used ColdFusion for awhile… can you do that now?

4 thoughts on “JSP Tag Files”

  1. re: How exactly is cfmodule a kludge? You’re right, it’s just characters, but when you’re reading code, what would you rather read:

    <cf_mytag>
    stuff
    </cf_mytag>

    or

    <cfmodule template=”tags/mydire/mytag.cfm”>
    stuff
    </cfmodule>

    The first makes no assumptions about the location of the tag, making it easier to move or re-organize your tags. The second hardcodes a tag path into your code, which makes your code harder to refactor and more prone to bugs (fat finger a template location and you have a bug).

    My rant was less about cfmodule and more about I just thought it would be really nice for each ColdFusion application on a server to have access to it’s own /WEB-INF/ directory, each with it’s own /WEB-INF/tags/ directory. That way there wouldn’t be any issues with naming conflicts (ie: my <cf_state> tag collides with your <cf_state> tag).

  2. if i use prefix ‘d’ in my tag and using the cout tag 2 get the value of an identifier using prefix ‘c’,what will happen is ther any compilation error comes r tell me the result of it

Leave a Reply

Your email address will not be published. Required fields are marked *