January 21, 2007 on 6:08 am | In Java |
JavaSercer Pages 16.9 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 why: In this case, a JSP container is allowed to use one instance of the tag handler for both action elements, with generated code similar to this: // Code for first occurrence MyActionTag _jspx_th_test_myAction_1 = new MyActionTag( ); _jspx_th_test_myAction_1.setPageContext(pageContext); _jspx_th_test_myAction_1.setParent(null); _jspx_th_test_myAction_1.setAttr1(”one”); _jspx_th_test_myAction_1.setAttr2(”two”); _jspx_th_test_myAction_1.doStartTag( ); if (_jspx_th_test_myAction_1.doEndTag( ) == Tag.SKIP_PAGE) return; // Code for second occurrence _jspx_th_test_myAction_1.setAttr2(”new”); _jspx_th_test_myAction_1.doStartTag( ); if (_jspx_th_test_myAction_1.doEndTag( ) == Tag.SKIP_PAGE) return; _jspx_th_test_myAction_1.release( ); As you can see, all the property setter methods are called to initialize the instance for the first occurrence of the element. But for the second occurrence, only the setter method for the property with a different value is called. The release( ) method is called when the tag handler has been used for both occurrences. If the tag handler class resets all property variables in any method other than release( ), the processing of the second action element fails. The only scenario in which a tag handler can be reused in JSP 1.1 is the one described above. If the same action element is used multiple times on the same page but with different sets of attributes, the state of the tag handler is not guaranteed to be correct if the same instance is reused. Reuse between pages, using a tag handler object pool, is not explicitly supported in JSP 1.1. For this reason, most JSP containers do not implement tag handler pooling today. To get your tag handler classes to work with the few that do, you must reset all properties before the tag handler is used to handle a new request. I recommend that you do this in the release( ) method, as shown in the examples in this chapter. Note that if some properties must have a default value set instead of null, you must set it in the release( ) method as well. A typical example is a primitive type property, such as an int property: public void release( ) { aStringProperty = null; anIntProperty = -1; } To make it easier for a container to reuse tag handlers, both within a page and between pages, a future version of JSP will likely introduce a method that resets all properties in a controlled manner. page 232
Note: If you are looking for cheap and inexpensive provider to host and run your tomcat application check professional tomcat hosting services
Sorry, the comment form is closed at this time.