The Tag-Handler Lifecycle and What It Means to You

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 try to reuse the same objects instead. The lifecycle defined for a tag handler allows a tag handler instance to be reused within the code generated for JSP pages under certain circumstances.

The tag-handler lifecycle details are pretty complex and are mostly of interest to container developers. But you need to know how the lifecycle relates to instance reuse to ensure that your tag handlers work correctly in a container that takes advantage of reuse for improved performance. Figure 20-5 shows a state diagram for a tag handler that implements just the Tag interface.

Lifecycle for a tag handler implementing the Tag interface

Figure 20-5. Lifecycle for a tag handler implementing the Tag interface

When the tag instance is created, all instance variables have default values; then all setter methods (setPageContext( ), setParent( ), and all setters for attributes) are called. This brings the instance to a state where it’s initialized for use. The doStartTag( ) method is then called. This method may set instance variables to values that are valid only for the current invocation. The doEndTag( ) method is called if no exception is thrown by doStartTag( ) or while processing the element’s body. The tag handler instance may then be reused ...

Get JavaServer Pages, Second 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.