Custom Tags allow you to remove that messy Java code from the JSP and move it into classes so that your HTML coders can code the HTML, and the Java coders can code the Java.
One of the most compelling reasons to use Java Server Pages has to be the addition of the JSP Custom Tags. By using Custom Tags, you are empowered with the ability to add Java Code into your JSPs. Many of you have probably just re-read that last sentence and are thinking that I must be a real goofball. Everyone who has ever used JSPs knows that you can embed Java code in them, just use the code delimiters (<% and %>) and you can code away to your heart’s content. You're right. But, Custom Tags allow you to remove that messy Java code from the JSP and move it into classes so that your HTML coders can code the HTML, and the Java coders can code the Java and you don't end up with scads of lines of code spread across dozens or hundreds of JSPs. "But you can do that with Java Beans" some of you are probably thinking right now. I am starting to get a little annoyed with all of your interruptions! I mean come on, who's writing this article, you or me? Ok, ok you're right again, but nobody likes a wise guy, so watch it. You can use Java Beans to include Java code without embedding it in the JSP. So why would anyone ever use custom tags? In a nutshell, Custom Tags make it oh-so-easy to separate your artistic design from the code that only programmers can love. And why is that important? It’s important because you want to play to your (or your team’s) strengths and that means giving design control of the "look" of your site to a designer who is best able to make it look great and give the guts to the programmers who are best suited to making the complexities work. The trick is doing it in such a way as to let them both work without killing each other. That’s where Custom Tags enter the picture.
We are going to look at five code examples, the first of which shows the Java code embedded into the JSP, the 2nd and 3rd which show the Java code in beans, and the last two examples showing the code in Custom Tags. We’ll start with a small fragment showing a variable being retrieved from the users session, incremented and displayed in the HTML much like the hit counter we’ve all seen countless times.
First, here's what it looks like when you embed the Java code directly in the JSP.
Example 1:
<html>
<head><title>Embedded Code</title></head>
<body>
<% String text = (String)session.getAttribute("displayText"); %>
<table>
<tr><td><%= text %></td></tr>
</table>
<% Integer counter = (Integer)Session.getAttribute("pageCounter");
if ( null == counter )
counter = new Integer();
%>
Counter: <%= counter.intValue() %>
<% Integer newCount = new Integer(counter.intValue() + 1);
session.setAttribute("pageCounter", newCount);
%>
</body>
</html>
Let’s face it. Programmers in general have the artistic sense of the Clampets of Beverly Hills. Now, I compensate for this by thinking of my code as an art form.
Now that's not too bad, but consider this. For this trivial example, we've added 6 lines of code for only 9 lines of HTML! That's a 40% increase for very little payback. In addition, if you have to add the code to display your page counter to another page, you are left with copy & paste for your code reuse strategy. Multiply the number of lines of code by the number of pages in your site and you will find that your maintenance task has just taken a turn for the worse. Finally, consider the distribution of labor that you've locked your site into. Whereas the example code is trivial to the average Java programmer, it is more akin to Klingon to the average web site designer. We Java types know that the funny words in the parens are casting the return type; the designers think that we dropped something on the keyboard and forgot to erase it. Let’s face it. Programmers in general have the artistic sense of the Clampets of Beverly Hills. Now, I compensate for this by thinking of my code as an art form as I’m sure most of you do as well, but Mom and Dad are not likely to hold this same view when they come across a web site that I’ve designed ("I knew we should have made him take pre-law!"). So we are now left with a piece of code that is not entirely in anyone’s control, and will likely take on the worst aspect possible.
But there is hope! Moving on to our next example, we will use a Java Bean to take as much of the code as we can out of the JSP.
Ken Wilson has over 15 years of IT experience, primarily in the legal and financial industries. For the past several years his focus has been on Internet Development, building systems for dotcoms such as Juniper Financial and Bill-Me-Later.com and is currently hard at work on an Investor Portal for Deutsche Bank. Ken is a Senior Architect at Kaloke Technologies, Inc. and a Product Manager for their successful KWML framework.
Sounds nice but if it were that easy then why did not you provide sample tags and the xml file and the TLD and all the other stuff required to make it work
I really liked "I love JSP Custom Tags". I'd been looking on the web for an hour before I finally found an article which simply explained why I'd want to use Custom Tags over JavaBeans in my jsp pages. Please give my kudos to the author.
On the other hand, when I went to vote, I found out I had to register. Being a friendly web citizen, I decided to invest the time to register. Then I found, I had to go and check my email to activate. Then I found the link only gave me 'error contacting ....' when I clicked on it. I made several attempts. No offense, but were you I, would you even be here sending feedback? Of course not! You would have bailed as soon as anyone asked you to register. So, like, why not let people vote without the overhead? Put them through all that mess, why would they bother to vote at all? Being a webdesigns company, perhaps a more effective approach could be adopted.