How Tag Handlers May Be Reused

Creating new objects is a relatively expensive operation in Java. For high-performance applications, it’s common to try to minimize the number of objects created and reuse the same objects instead. The JSP 1.1 specification describes how a tag handler instance can be reused within the code generated for a JSP page if the same type of custom action appears more than once. The reuse is subject to a number of restrictions and relies on tag handler classes dealing with their internal state as specified. It’s important to understand the reuse rules, so your tag handler classes behave as expected in a JSP implementation that takes advantage of this mechanism.

As discussed in the previous sections of this chapter, a tag handler’s state is initiated through property setter methods corresponding to the action element’s attributes. The tag handler is then offered a chance to do its thing in various stages, represented by the doStartTag( ), doInitBody( ), doAfterBody( ), and doEndTag( ) methods. It’s clear that the property values must be kept at least until the tag handler has done what it intends to do. But when can it safely reset its state? If a tag handler implements all logic in the doStartTag( ) method, can it reset all instance variables before it returns from this method? Or should it wait until the doEndTag( ) method is called? The answer is that it must not reset the state until the release( ) method is called. Let’s use a JSP page fragment to discuss ...

Get Java Server Pages 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.