Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
330
Chapter 14 Creating Custom JSP Tag Libraries
_jspService
,
doGet
, and
doPost
. The solution to this dilemma is to use
getRequest
to obtain the
HttpServletRequest
from the automatically
defined
pageContext
field of
TagSupport
. Strictly speaking, the return
type of
getRequest
is
ServletRequest
, so you have to do a typecast to
HttpServletRequest
if you want to call a method that is not inherited
from
ServletRequest
. However, in this case I just use
getParameter
, so
no typecast is required.
Listing 14.13 defines a tag that ignores its body unless a request time
debug
parameter is supplied. Such a tag provides a useful capability whereby
you embed debugging information directly in the JSP page during develop
ment, but activate it only when a problem occurs.
Listing 14.13 DebugTag.java
package coreservlets.tags;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.*;
import javax.servlet.*;
/** A tag that includes the body content only if
* the "debug" request parameter is set.
*/
public class DebugTag extends TagSupport {
public int doStartTag() {
ServletRequest request = pageContext.getRequest();
String debugFlag = request.getParameter("debug");
if ((debugFlag != null) &&
(!debugFlag.equalsIgnoreCase("false"))) {
return(EVAL_BODY_INCLUDE);
} else {
return(SKIP_BODY);
}
}
}
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