When do I need to call clearBody()?

In another JSP book, they make the point of clearing the body content after using it, so the same instance can be reused for another tag handler.In JavaServer Pages (O’Reilly), you do not say anything about calling the clearBody() method. When do I really have to call this method?
Answer:
Typically, you do not need to call the clearBody() method, and it has nothing to do with the container’s ability to reuse the BodyContent instance.According to the JSP 1.1 spec, the container is supposed to get an instance of the BodyContent class for each BodyTag by calling PageContext.pushBody(). The spec says that this method must return a “new” instance, but the language will likely be relaxed in JSP 1.2 to allow a container to use pooled instances instead. Anyway, the BodyContent instance it returns must be empty, so there’s no need to clear it for reuse reasons. This is also clarified in the description of setBodyContent() in the JSP 1.2 Proposed Final Draft.

A scenario when you do need to call clearBody() is in an iterating tag handler class, if you push the processed body content to the parent’s “out” in doAfterBody(), i.e. for each pass through the body. In the iteration example in my book (Chapter 16), it’s not needed since I let the evaluation of the body for each pass accumulate and process all of it after the last pass in the doEndTag() method.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>