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.
This example is very close to the example above that is using the Java Beans. That’s because, at their heart, Custom Tags share much in common with Java Beans--except they are specially designed to enhance JSP, and by extension, HTML documents. The one thing that you should notice from this example is the first line where a taglib is referenced. You probably have already made the leap that taglib is short for tag library. Instead of defining each Tag in the document as you did with the useBean tag, you group them together in "libraries" and then let the JSP processor know about them by referencing a tag descriptor. The reference is setup by including the taglib directive.
Now that you’ve seen the tags, you are probably thinking that they are really a small incremental step over using the Java Bean, but as this next example will show, the differences can make all the difference in the world. We’ll be following the same example as in 2b by displaying rows of data in a table.
Ahhh. The light bulb is beginning to brighten. Look closely at the lines defining the table (5-11). Unlike the example above, we now have a tag with a body--that is, a tag that has both an open and close version just like most of the HTML tags. We’re making good use of the body by providing a row template for the data rows. In this example, the body of the tag will be repeated once for each row of the data being displayed, and with each iteration, variables created and populated by the tag (you can see them in the example code delimited by <%= and %>) are populated with the data for that row. Since the Java coder is only providing the data, the HTML coder can change the row template in the tag body to anything that they desire. Now color changes are a snap. So are sizing and style sheets and fonts and images and all of the other things that make a site designer giddy with joy. In the meantime, we Java coders can go back into our caves and work without being disturbed to make UI changes, and who knows, maybe we’ll even finish a project on time now and again (with enough Mountain Dew and Pizza).
JSP Custom Tags are the true power behind JSP. They finally allow programmers and designers to play nice together because they enforce a separation of duties so that each is doing what they are best at. With Custom Tags, it is no longer necessary to make compromises between functionality and beauty, and it will never again be necessary to code your own custom HTML class to try to work around the limitations that we had before tags.
I’ve skimmed across the surface to give you a quick introduction to Custom Tags, and hopefully to give you enough of a taste to make you want to come back for more. Good Coding!
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.