Powered by Blogger.

Using Debug Tag with an Example

>> Thursday, October 13, 2011


In this article we explained that to send the JSP content of the tag body to the client, one need only call the getJspBody().invoke(null) method inside the doTag method of the tag handler class.

This simplicity allows us to easily create tags that output their bodies conditionally. This functionality can be achieved by simply surrounding the getJspBody().invoke(null) invocation within an if statement.

In this section, we present an example of a custom tag that conditionally outputs its tag body. It's quite often the case when the output of the JSP page is something other than what you expected. In such a case, it's useful to have the option of seeing some debugging information right on the page without having to resort to embedding System.out.print statements throughout the page.

However, we do not want the user to see the debugging information in the production system. To solve this problem, we create a custom tag that conditionally outputs its body based on the presence of the debug request parameter. If the debug request parameter is present, it would signal to the JSP page to output the debugging information.

DebugTag.java file : In its doTag method, we output the tag body if the debug request parameter is present and skip the body of the tag if it's not. Inside the JSP page, shown in below, we surround the debugging information with our debug tag. Listing 7.15 shows the excerpt from the csajsp-taglib.tld file declaring the debug tag to the container. Listing 7.16 shows the debug.jsp page that uses the debug tag.

DebugTag.java :
Code:
package javabynataraj.tags;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.*;
import javax.servlet.http.*;

/**
 *  DebugTag outputs its body if the request parameter
 *  'debug' is present and skips it if it's not.
 */
public class DebugTag extends SimpleTagSupport {
  public void doTag() throws JspException, IOException {
    PageContext context = (PageContext) getJspContext();
    HttpServletRequest request =
      (HttpServletRequest) context.getRequest();
    // Output body of tag only if debug param is present.
    if (request.getParameter("debug") != null) {
      getJspBody().invoke(null);
    }
  }
}

Excerpt from csajsp-taglib.tld :
Code:
<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
  http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
  version="2.0">
  <tlib-version>1.0</tlib-version>
  <short-name>csajsp-taglib</short-name>

  <tag>
    <description>Conditionally outputs enclosed body</description>
    <name>debug</name>
    <tag-class>javabynataraj.tags.DebugTag</tag-class>
    <body-content>scriptless</body-content>
  </tag>
</taglib>

debug.jsp :
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Some Hard-to-Debug Page</TITLE>
<LINK REL=STYLESHEET
      HREF="JSP-Styles.css"
      TYPE="text/css">
</HEAD>
<BODY>
<H1>Some Hard-to-Debug Page</H1>
<%@ taglib uri="/WEB-INF/tlds/csajsp-taglib.tld"
             prefix="csajsp" %>
Top of regular page. Blah, blah, blah.
Yadda, yadda, yadda.
<csajsp:debug>
<H2>Debug Info:</H2>
********************<BR>
-Remote Host: ${pageContext.request.remoteHost}<BR>
-Session ID: ${pageContext.session.id}<BR>
-The foo parameter: ${param.foo}<BR>
********************<BR>
</csajsp:debug>
<P>
Footer of the page.
</BODY></HTML>


Related Posts Plugin for WordPress, Blogger...
© javabynataraj.blogspot.com from 2009 - 2022. All rights reserved.