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.
WOW. We removed 4 of the 6 lines of extra code and we didn’t lose a single bit of functionality. All of that code is now contained in our two beans. The bean programmer can do everything that they need to do without cluttering up the JSP. On top of that, we’ve made the code reusable so that any page that needs to add our counter need only include the Java Bean to get the functionality. Better yet, the site designers can go about their design tasks in parallel with the Java coders, and they never have to come in contact with the mumbo jumbo that we call a job.
So, tell me again, why anyone would want to use Custom Tags? The Java Beans allow us to remove the code from the JSP and they allow us to reuse our work. What more could we ever want? To answer that question, we’re going to need an example with a bit more complexity. For this example, we’ll be carrying out a very common task in any system. We are going to display rows of data such that each row of data will be displayed in an HTML table row. Here’s the example.
Not really much different from the earlier example, we instantiate the bean with the "useBean" tag and then retrieve the data by using the "getProperty" tag. But in this case, it is more about what you don’t see than about what you do. Look closely at the table definition on lines 5 through 7. Can you see what’s missing? There are no table row (<tr>) or table column (<td>) tags. That’s because they are being written out from the Java Bean. Since the Java Bean can only pass data out in chunks, the programmer is required to build the HTML representation of the data within the bean, and write it out all at once. This might look like this:
StringBuffer data = new StringBuffer();
Iterator i = new Iterator();
while ( i.hasNext() )
{
String rowData = (String)i.next();
data append("<tr><td>").append(rowData).append("</td></tr>");
}
// Here the data looks like <tr><td>lineone</td></tr><tr><td>linetwo</td></tr> etc.
// Write the table chunk out here
At first blush, this doesn’t really seem to be a big problem. It’s pretty simple code and it isn’t really a big deal for the Java coder who has to write it. Now, change the color of alternating rows so that they are blue/white. Uh oh. You need to go back to the Java coder to change the class to output every other row with a blue background. The Java coder of course has no problem making the change and does so in a few minutes, compiles the code, and deploys the code to the server. Now the CIO wants a demo and he’d like to see the colors as blue and saffron. Same deal--go back to the Java coder, who makes the color change in the code--compile the code--deploy the code to the server. Imagine repeating that same deployment cycle over and over again because someone wants to change the color, the font, the size, the spacing and a thousand other little things that have now grown up to take over the Java coder’s time that they were supposed to be using to finish the project. We’ve made the same mistake that we made by embedding the Java code in the JSP, only this time in reverse. We’ve blurred the lines of where the designers leave off and where the programmers pick up and by doing so, we’ve caused a boatload of extra work that wasn’t on the schedule and is frankly annoying to both parties, who only want to get on with doing their jobs.
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.