File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Why custom tags? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Why custom tags?" Watch "Why custom tags?" New topic

Why custom tags?

Paul Gilette

Joined: May 09, 2001
Posts: 10
Why are custom tags taken for granted as the "right" way to add functionality to JSPs? Why not just create libraries of static Java routines, then call those as JSP expressions?

Here's an example of the Struts iterate tag:
<logic:iterate id="element" name="myhashtable", scope="session">
<br><bean:write name="element"/>
Why not just code a static String method that does the same thing:
<%= HtmlUtils.iterate(session.getAttribute("myhashtable"), "<br>?")%>
Of course, you are free to make up your own parameter list -- you could have the using code pass "page", request, or response if those would be handy.
The training cost for the HTML programmer would be the same: It takes the same effort to learn a new JSP tag such as logic:iterate, as it is to learn how to call a new static method. (The static method call is probably a little simpler.)
The organization benefits because you no longer have to manage TLDs. Documentation of these static methods is easy because you use Javadoc. The organization also benefits because the learning curve for coding some static routine is lower than the learning curve for implementing a custom tag.
Kyle Brown
Ranch Hand

Joined: Aug 10, 2001
Posts: 3892
Well, the primary idea behind them is that they would encapsulate larger chunks of functionality than most custom tags you see nowadays actually do.
When you're looking at a larger, project or company-specific piece of functionality, it starts making more sense, since you can begin to see that someone without ANY Java knowledge (e.g. a page designer, and not a programmer) could drop them into a page just as they would use any other HTTP tag.
However, the fine-grained tags that we seem to be stuck with haven't lived up to this promise. Programming is still programming. As usual, things have turned out to be more complicated than they were in theory.
Kyle Brown
Kyle Brown,
Author of Enterprise Java (tm) Programming with IBM Websphere

Kyle Brown, Author of Persistence in the Enterprise and Enterprise Java Programming with IBM Websphere, 2nd Edition
See my homepage at for other WebSphere information.
chanoch wiggers
Ranch Hand

Joined: May 24, 2001
Posts: 245
also it allows editor to manipulate the jsp. If you look at the spec for j2ee 1.3 is recommends it greatly for this reason. Initially tools like coldfusion will provide support for it, eventually all java IDEs should
Chanoch Wiggers
Architect for Professional Java Mobile Programming

chanoch<p><a href="" target="_blank" rel="nofollow">Author of Professional Apache Tomcat</a></p>
Paul Gilette

Joined: May 09, 2001
Posts: 10
The editor can be Java-aware just as easily. (Probably more easily, because those parsers are already build into most Java development tools.)
Simon Brown
sharp shooter, and author
Ranch Hand

Joined: May 10, 2000
Posts: 1913
One of the goals of JSP (and web development in general) is to separate out content and presentation. Writing static methods will allow you to achieve this to a degree, but it's not as flexible as custom tags.
Using the iterate example, you could write a static method that returned the results of an iteration. You could pass it the collection and a "template" of what you'd like to see during each iteration. This would work for simple cases, but how would you specify that you wanted to display a specific property of the objects stored within the collection? And how would you specify the style and formatting of that property?
One of the most powerful features of custom tags is that they allow you to introduce variables back into the page. This means that an iteration tag simply needs to provide iteration functionality and "introduce" each object from the collection back into the page. With this in mind, the presentation is a simpler task as it stays in the page.
Another powerful feature is that the tags can be customized easily at runtime with attributes, just like HTML tags. This could be done with static methods taking parameters, but methods can't have optional parameters whereas tags can have optional attributes. Okay, you could provide alternative versions of the static methods but this will get messy!
Finally, there are many other useful features tags provide that are hard or impossible to implement with static methods, or regular JavaBeans for that matter. Examples being controlling the flow of the page, manipulating body content after it has been generated and so on. The key to all of this is that you can easily separate out the content from the presentation of that content using custom tags - something that is often hard to acheive with simple method calls returning generated content.
With tags, the presentation stays in the page and doesn't get wrapped up in Java code. This is similar to the Servlets vs JSP debate.
Sorry for the long post ... hope it helps though!
Simon Brown
Co-author of Professional JSP 2nd Edition
Gerry Giese
Ranch Hand

Joined: Aug 02, 2001
Posts: 247
Wow! THREE book authors responding to a single post?!? I'm impressed!
Thanks for this discussion - I'm in the planning stages of writing a generic framework for writing intranet applications, and I had just about discounted custom tags. Now I think I'll look into them more carefully.
One question - when using JavaBeans and/or custom tags, doesn't that add more work in the instantiation of new classes and garbage collection? Could this possibly become a performance problem in systems with high numbers of users?
My current solution looks something like below, sassuming that I have an MVC-like solution where servlet(s) retrieves data, packs it up into a Vector of hashes, pushes the package into the request as an attribute, then forward to the JSP for display:
<% Vector table = (Vector)request.getAttribute("timecard_table");
Enumeration enum = table.elements();
for (Enumeration e = enum; e.hasMoreElements(); ) {
Hashtable row = (Hashtable) e.nextElement(); %>
<TD ALIGN=LEFT VALIGN=MIDDLE NOWRAP><%= row.get("name") %></TD>
<TD ALIGN=CENTER VALIGN=MIDDLE ><%= row.get("weekendDate") %></TD>
<TD ALIGN=LEFT VALIGN=MIDDLE NOWRAP><%= row.get("description") %></TD>
<TD ALIGN=RIGHT VALIGN=MIDDLE ><%= row.get("hoursTotal") %></TD>
<TD ALIGN=RIGHT VALIGN=MIDDLE ><%= row.get("monHours") %></TD>
<TD ALIGN=RIGHT VALIGN=MIDDLE ><%= row.get("tueHours") %></TD>
<TD ALIGN=RIGHT VALIGN=MIDDLE ><%= row.get("wedHours") %></TD>
<TD ALIGN=RIGHT VALIGN=MIDDLE ><%= row.get("thuHours") %></TD>
<TD ALIGN=RIGHT VALIGN=MIDDLE ><%= row.get("friHours") %></TD>
<TD ALIGN=RIGHT VALIGN=MIDDLE ><%= row.get("satHours") %></TD>
<TD ALIGN=RIGHT VALIGN=MIDDLE ><%= row.get("sunHours") %></TD>
<% } %>
I had planned to find a better option than 'Vector' to iterate over, but haven't gotten around to it yet. I figured it was generic enough to be usable and still allow a lot of flexibility in display. I haven't been too concerned about page designers, because in my case our departments programmers ARE the page designers.
This is just the first pass, and y'all have given me something to think about for the next one. Thanks again for the discussion!

CJP (Certifiable Java Programmer), AMSE (Anti-Microsoft Software Engineer)
Author of Posts in the Saloon
Simon Brown
sharp shooter, and author
Ranch Hand

Joined: May 10, 2000
Posts: 1913
Hi Gerry,
Yes, IMHO custom tags are certainly worth looking into.
You're spot on about the additional work around instantiation and garbage collection but ...
(and didn't you know there was a but coming!)
... vendors can choose to reuse tag handler instances. In other words, they can create a pool of tags at the start and use these to fulfil the requests as appropriate. The release() method on the tag is a useful place to reset tag state in order that it can reused again.
Also, the overheads that the tags introduce are (hopefully!) fairly neglible compared with the processing that they perform so don't let this put you off!
Add that to the reuse opportunities that tags provide and you're on to a winner.
Good luck with the project!
p.s. the slides from the custom tag BOFs I presented at JavaOne this year can be found at
Simon Brown
Co-author of Professional JSP 2nd Edition
[This message has been edited by Simon Brown (edited October 01, 2001).]

Joined: Sep 25, 2001
Posts: 5
For a good example of what JSP custom tags can do, check out the grid and form tags at
Pretty cool stuff...
I agree. Here's the link:
subject: Why custom tags?
jQuery in Action, 3rd edition