Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
14.6 Manipulating the Tag Body
335
"
, and
&
, respectively. This method is useful when servlets output
strings that might contain characters that would interfere with the HTML
structure of the page in which the strings are embedded. Listing 14.16 shows
a tag implementation that gives this filtering functionality to a custom JSP
tag.
Listing 14.16 FilterTag.java
package coreservlets.tags;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.*;
import coreservlets.*;
/** A tag that replaces <, >, ", and & with their HTML
* character entities (<, >, ", and &).
* After filtering, arbitrary strings can be placed
* in either the page body or in HTML attributes.
*/
public class FilterTag extends BodyTagSupport {
public int doAfterBody() {
BodyContent body = getBodyContent();
String filteredBody =
ServletUtilities.filter(body.getString());
try {
JspWriter out = body.getEnclosingWriter();
out.print(filteredBody);
} catch(IOException ioe) {
System.out.println("Error in FilterTag: " + ioe);
}
// SKIP_BODY means I m done. If I wanted to evaluate
// and handle the body again, I d return EVAL_BODY_TAG.
return(SKIP_BODY);
}
}
The Tag Library Descriptor File
Tags that manipulate their body content should use the
bodycontent
ele
ment the same way as tags that simply include it verbatim; they should supply
a value of
JSP
. Other than that, nothing new is required in the descriptor file,
as you can see by examining Listing 14.17.
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