JavaSercer Pages 16.3 Processing the Action Body As you can see, it’s easy to develop a tag handler that doesn’t need to do anything with the action element’s body. For a tag handler that does need to process the body, however, just a few more methods are needed. They are defined by the BodyTag interface, which extends the Tag interface. The action element’s body has many possible uses. It can be used for input values spanning multiple lines; the SQL custom actions introduced in Chapter 9, use the body this way. The SQL statement is often large, so it’s better to let the page author write it in the action body instead of forcing it to fit on one line, which is a requirement for an attribute value. The body can also contain nested actions that rely on the enclosing action in some way. The action, also from Chapter 9, provides the nested SQL actions with the DataSource object they use to communicate with the database, and ensures that the SQL statements in all actions are treated as one transaction that either fails or succeeds. A third example is an action that processes the body content in one way or another before it’s added to the response. Chapter 12, contains an example of an action that processes its XML body using the XSL stylesheet specified as an attribute. Later in this section we look at an action that replaces characters that have special meanings in HTML with the corresponding HTML character entities. As with the Tag interface, there’s a BodyTagSupport class that implements all the methods of the BodyTag interface, plus a few utility methods: public class BodyTagSupport extends TagSupport implements BodyTag { A tag handler that implements the BodyTag interface is at first handled the same way as a tag handler implementing the Tag interface: the container calls all property setter methods and the doStartTag( ) method. But then things divert, as illustrated in Figure 16.3. Figure 16.3. BodyTag interface methods First of all, the BodyTagSupport class overrides the doStartTag( ) method inherited from the TagSupport class: public int doStartTag( ) throws JspException { return EVAL_BODY_TAG; } Instead of returning SKIP_BODY, like the TagSupport class does, it returns EVAL_BODY_TAG . The EVAL_BODY_TAG value is valid only for a tag handler that implements the BodyTag interface. It means that not only should the action’s body be processed, but the container must also make the result available to the tag handler. To satisfy this requirement, the container uses a BodyContent object. This is a subclass of the JspWriter, the class used to write text to the response body. In addition to the inherited methods for writing to the object, the BodyContent class has methods that the tag handler can use to read the content. This is how it works. The JSP container assigns a reference to a JspWriter to the implicit out variable at the top of the page. Everything that’s added to the response body - either explicitly by JSP elements or implicitly by the JSP container (template text) - is written to out, so it ends up in the JspWriter before it’s sent to the browser. When the JSP container encounters a custom action with a tag handler that implements the BodyTag interface, it temporarily reassigns out to a BodyContent object until the action’s end tag is encountered. The content produced when the element body is processed is therefore buffered in the BodyContent object where the tag handler can read it. page 218
Quick Hint: If you are looking for best quality webspace to host and run your tomcat application check Vision tomcat hosting services