| Contents | Prev | Next |
|
Tag interface (or be derived from TagSupport). If the body of the tag needs to be evaluated, the doStartTag method needs to return EVAL_BODY_INCLUDE; otherwise it should return SKIP_BODY.
BodyTag (or be derived from BodyTagSupport). Such handlers typically implement the doInitBody and the doAfterBody methods. These methods interact with body content passed to the tag handler by the JSP page implementation class.
A body content supports several methods to read and write its contents. A tag handler can use the body content's getString or getReader methods to extract information from the body and the writeOut(out) method to write the body contents to an out stream. The writer supplied to the writeOut method is obtained using the tag handler's getPreviousOut method. This method is used to ensure that a tag handler's results are available to an enclosing tag handler.
If the body of the tag needs to be evaluated, the doStartTag method needs to return EVAL_BODY_TAG; otherwise it should return SKIP_BODY.
The doInitBody method is called after the body content is set but before it is evaluated. You generally use this method to perform any initialization that depends on the body content.
The doAfterBody method is called after the body content is evaluated.
Like the doStartTag method, doAfterBody must return an indication of whether to continue evaluating the body. Thus, if the body should be evaluated again, as would be the case if you were implementing an iteration tag, doAfterBody should return EVAL_BODY_TAG; otherwise doAfterBody should return SKIP_BODY.
A tag handler should reset its state and release any private resources in the release method.
The following example reads the content of the body (which contains an SQL query) and passes it to a object that executes the query. Since the body does not need to be reevaluated, doAfterBody returns SKIP_BODY.
public class QueryTag extends BodyTagSupport {
public int doAfterBody() throws JspTagException {
BodyContent bc = getBodyContent();
// get the bc as string
String query = bc.getString();
// clean up
bc.clearBody();
try {
Statement stmt = connection.createStatement();
result = stmt.executeQuery(query);
} catch (SQLException e) {
throw new JspTagException("QueryTag: " +
e.getMessage());
}
return SKIP_BODY;
}
}
The following example reads the content of the body, transforms that content, and then writes the modified version to the out stream.
public class TransformTag extends BodyTagSupport {
public int doAfterBody() throws JspTagException {
BodyContent bc = getBodyContent();
String body = bc.getString();
bc.clearBody();
try {
getPreviousOut().print(body.transform());
} catch (IOException e) {
throw new JspTagException("TransformTag: " +
e.getMessage());
}
return SKIP_BODY;
}
}
bodycontent Element
Body content containing custom and core tags, scripting elements, and HTML text is categorized as<tag> ... <bodycontent>JSP|tagdependent</bodycontent> </tag>
JSP; all other types of body content are tagdependent. Note that the value of this element does not affect the interpretation of the body. The bodycontent element is only intended to be used by an authoring tool to present the content of the body.