Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
334
Chapter 14 Creating Custom JSP Tag Libraries
14.6 Manipulating the Tag Body
The
csajsp:prime
element (Section 14.3) ignored any body content, the
csajsp:heading
element (Section 14.4) used body content, and the
csa
jsp:debug
element (Section 14.5) ignored or used it depending on a request
time parameter. The common thread among these elements is that the tag
body was never modified; it was either ignored or included verbatim (after
JSP translation). This section shows you how to process the tag body.
The Tag Handler Class
Up to this point, all of the tag handlers have extended the
TagSupport
class.
This is a good standard starting point, as it implements the required
Tag
interface and performs a number of useful setup operations like storing the
PageContext
reference in the
pageContext
field. However,
TagSupport
is
not powerful enough for tag implementations that need to manipulate their
body content, and
BodyTagSupport
should be used instead.
BodyTagSupport
extends
TagSupport
, so the
doStartTag
and
doEndTag
methods are used in the same way as before. The two important new meth
ods defined by
BodyTagSupport
are:
1.
doAfterBody
, a method that you should override to handle the
manipulation of the tag body. This method should normally
return
SKIP_BODY
when it is done, indicating that no further
body processing should be performed.
2.
getBodyContent
, a method that returns an object of type
BodyContent
that encapsulates information about the tag body.
The
BodyContent
class has three important methods:
1.
getEnclosingWriter
, a method that returns the
JspWriter
being used by
doStartTag
and
doEndTag
.
2.
getReader
, a method that returns a
Reader
that can read the
tag's body.
3.
getString
, a method that returns a
String
containing the
entire tag body.
In Section 3.4 (Example: Reading All Parameters), we saw a static
filter
method that would take a string and replace
<
,
>
,
"
, and
&
with
<
,
>
,
Second edition of this book: www.coreservlets.com; Sequel: www.moreservlets.com.
Servlet and JSP training courses by book's author: courses.coreservlets.com.
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved