The use of interfaces simplifies taking an existing Java object and making it a tag handler.  
There are also two support classes that can be used as base classes: 
TagSupport
 and 
BodyTagSupport
.
Simple Actions
In many cases, the tag handler only needs to use the tag handler's method 
doStartTag()
which is invoked when the start tag is encountered. This method needs to access the 
attributes of the tag and may also want to access information on the state of the JSP page; 
this information is passed to the Tag object before the call to 
doStartTag()
 through 
several setter method calls.
The 
doEndTag()
 is similar to 
doStartTag()
, except that it is invoked when the end tag 
of the action is encountered.  The result of the 
doEndTag
 invocation indicates whether the 
remaining of the page is to be evaluated or not.
A particularly simple and frequent action is one that has an empty body (no text between the 
start and the end tag). The Tag Library Descriptor can be used to indicate that the tag is 
always intended to be empty; this leads to better error checking at translation time and to 
better code quality in the JSP page implementation class.
Actions with Body
Recall that in general, the body of an action may contain other custom and core actions and 
scripting elements, as well as uninterpreted template text.
In some cases, an action is only interested in  passing through  the content of the body.  This 
can be done using the simple Tag interface by using a special return value in 
doStartTag()
.
If an action element can have a non empty body and is interested in the content of that body, 
the methods 
doInitBody()
 and 
doAfterBody()
, defined in the BodyTag interface are 
involved.
The control of the evaluation is actually done based on the result of method invocations as 
follows. The 
doStartTag()
 method is always invoked first and returns an 
int
 value that 
indicates if the body of the action should be evaluated or not.  If so (EVAL_BODY_TAG 
return), a nested stream of type 
BodyContent
 is created and it is passed to the Body
Tag
object through 
setBodyContent
.  Then 
doInitBody
 is invoked.  Next the body is 
evaluated, with the result going into the newly created 
BodyContent
 object.  Finally the 
doAfterBody()
 method of the tag handler object is invoked.
If the invocation to 
doStartTag()
 returned SKIP_BODY, the body is not evaluated at all.
The 
doBody()
 methods may use the 
BodyContent
 object as it sees fit. For example, it 
may convert it into a String and use it as an argument.  Or it may do some filter action to it 
before passing it through to the out stream.  Or something else.
Chapter 5
Tag Extensions
88




JSP Web Hosting JavaServer Pages Specifications JSP Hosting




 
TotalRoute.net Business web hosting division of Vision Web Hosting Inc. All rights reserved.