With BodyTag, you can buffer the body

The BodyContent argument to setBodyContent() is actually a type of java.io.Writer. (Yes, it’s OK to find that disturbing from an OO perspective.) But that means you can process the body by, say, chaining it to another IO stream or getting the raw bytes.

Q:

Q: What happens if I return EVAL_BODY_BUFFERED even though the invoking tag is empty?

A:

A: The setBodyContent() and doInitBody() method will not be called if the tag invoking the handler is empty! And by empty, we mean that the tag was invoked using an empty tag <my:tag /> or with no content between the opening and closing tags <my:tag><my:tag>.

The Container knows there’s no body this time, and it just skips to the doEndTag() method, so this is usually not a problem.

Unless the TLD declares the tag to have an empty body! If the TLD says <body-content>empty</body-content>, you don’t have a choice, and you must NOT return EVAL_BODY_BUFFERED or EVAL_BODY_INCLUDE from doStartTag().

Q:

Q: What about attributes in a Classic tag? Are they handled the same way as with Simple tags?

A:

A: Yes, on the sequence diagram for both Simple tag handlers and Classic tag handlers, there was a place where bean-style setter methods are called for each attribute. This happens before a Simple tag’s doTag() or a Classic tag’s doStartTag(). In other words, tag attributes work in exactly the same way for both Classic and Simple tags, including the way in which they’re declared in the TLD.

Relax

You don’t need to know all the ...

Get Head First Servlets and JSP, 2nd 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.