When you need more than Tag Files... Sometimes you need Java

Tag Files are fine when you’re doing an include—when all you need to handle the tag you can do from another JSP (renamed with a .tag extension and with the appropriate directives added). But sometimes you need more. Sometimes you need good old Java code, and you don’t want to do it from scriptlets, since that’s what you’re trying to prevent by using tags.

Tag files implement the tag functionality with another page (using JSP).

When you need Java, you need a custom tag handler. A tag handler, as opposed to a tag file, is simply a Java class that does the work of the tag. It’s a little like an EL function, except much more powerful and flexible. Where EL functions are nothing more than static methods, a tag handler class has access to tag attributes, the tag body, and even the page context so it can get scoped attributes and the request and response.

Tag handlers implement the tag functionality with a special Java class.

Custom tag handlers come in two flavors: Classic and Simple. Classic tags were all you had in the previous version of JSP, but with JSP 2.0, a new and much simpler model was added. You’ll have a hard time coming up with reasons to use the classic model when you need a custom tag handler, because the simple model (especially combined with JSTL and tag files) can handle nearly anything you’d want to do. But we can’t dump the classic model for two reasons, and these two reasons are why you still have to learn it for the exam:

Tag handlers come in two flavors: Simple and Classic.

1) Like scripting, Classic tag handlers are out there, and you might need to read and support them, even if you never create one yourself.

2) There are those rare scenarios for which a classic tag handler is the best choice. This is pretty obscure, though. So point #1 is by far the most important reason to learn about Classic tags.

We’ll start with the Simple tag model first, to get warmed up.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset