The Differences Between Simple and Classic Tags in JSP

By: Grenfel Viewed: 153251 times  Printer Friendly Format    


There are several key differences between simple and classic tags. Let’s take a quick look at each in turn and evaluate what it means to you as a tag developer.

The Tag Handler Interface

The fundamental difference between simple and classic tags is the way in which the tag handler class is implemented. When you’re using simple tags, any tag handlers you build must implement the javax.servlet.jsp.tagext.SimpleTag interface. When you’re using classic tags, however, the tag handlers must implement the javax.servlet.jsp.tagext.Tag interface or, as you’ll see in this chapter, one of its subinterfaces.

For you as a developer, this means you need to learn a slightly different programming model. For example, when using simple tags, all of the functionality to be encapsulated within the tag is defined within the doTag() method. When using classic tags, there are two methods you must implement: doStartTag() and doEndTag().

Feedback from tag developers over the past couple of years has been mixed, and many people find the concepts employed by classic tag handlers confusing. Therefore, the interface has been simplified and simple tags are the result of this process.

The Tag Life Cycle

Another key, though often neglected, difference relates to the tag life cycle. With simple tags, an instance of the tag handler class is created when needed and that instance is used to serve only a single invocation of a custom tag. In other words, a unique tag handler instance is created for each usage of a simple tag on a page.

With classic tags, this may or may not be the case because the JSP specification enables container vendors to optionally improve classic tag performance by pooling and reusing tag handler instances. This means that, for example, a single tag handler instance could be created and reused to service all invocations of that custom tag per page. The rules around reuse are fairly complicated, and to make matters worse, JSP container vendors don’t always choose to implement this optional piece of the specification. Therefore, if you’re using classic tags, you must be aware of whether your container pools and reuses tag handler instances.

The problems that arise between different JSP containers is another reason simple tags were introduced, and it’s another area in which the complexity associated with developing them has been reduced. The downside is that there may be times when you’d like tag instances pooled and reused. For example, your tag might acquire some expensive resource when it is created. In this example, it makes sense to take advantage of any performance benefits that the container may provide. In many scenarios, however, this just isn’t an issue and simple tags are more than adequate.



Most Viewed Articles (in JSP )

Latest Articles (in JSP)

Comment on this tutorial