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 ...

Get Head First Servlets and JSP, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.