Recipe 22.7 Handling Exceptions in a Custom Tag Class


You want your custom tag handler to deal with any exceptions thrown inside the tag.


Implement the TryCatchFinally interface in your tag handler.


The tag extension API provides the TryCatchFinally interface, which you can implement in your tag handler class to write code dealing with any exceptions the tag handler might throw. If the class implements TryCatchFinally , it must include the methods doCatch( ) and doFinally( ) . In doCatch( ) , the code has access to any Throwable object thrown by doStartTag( ) or doEndTag( ) , for instance. In doFinally( ) , the code closes any resources the tag uses, such as a database connection.

In general, this interface allows the tag handler itself to catch and handle any exceptions that do not affect the output of the JSP enclosing the tag. Example 22-5 uses the same code as Example 22-1, but additional methods are added by implementing the TryCatchFinally interface.

Example 22-5. A logo tag handler that catches any exceptions
 package com.jspservletcookbook;     import javax.servlet.*; import javax.servlet.http.*;  import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*;  /** This tag generates a thumbnail image using the HTML img tag, next to a text message. The user specifies the content of the message and the Heading level (i.e., <H1>-<H6>) */  public class LogoTag extends BodyTagSupport implements TryCatchFinally{  private String heading = null;     private String image =null;     private String width =null;     private String height =null;   //this method assumes that attribute properties have been set.   public int doStartTag( ) throws JspException{          try {                int h = new Integer(heading).intValue( );                if(! (h > 0 && h < 7))                            throw new JspException(               "The 'heading' attribute value must between 1 and 6"+               " inclusive.");                } catch (Exception e) {            throw new JspException(e.getMessage( ));        }          return EVAL_BODY_BUFFERED;      }   public int doEndTag( ) throws JspException {       JspWriter out = pageContext.getOut( );       String imgDir = ((HttpServletRequest) pageContext.getRequest( )).       getContextPath( ) + "/images/";       String message = getBodyContent( ).getString( ).trim( );       try{           out.println("<img src=\""+ imgDir + image + "\" width=\"" +            width + "\" height=\"" + height + "\" align=\"left\">" + "<H" +            heading + ">" + message + "</H" + heading+ ">");            } catch ( io) {}            return EVAL_PAGE;   } //doEndTag  /* The next two methods have to be implemented in this class since the class implements  TryCatchFinally */   public void doCatch(Throwable t){              try{                  //print the exception message inside the JSP where the tag             //appears           pageContext.getOut( ).println(t.getMessage( )+"<br />");              } catch ( io) {}   }      public void doFinally( ){         //do nothing here, since we don't have any resources open       //like database connections      }  public void setHeading(String level){     this.heading= level;   }      /* THE REST OF THE SOURCE CODE FROM EXAMPLE 22-1 CONTINUES... */ } 

If the tag throws an exception, then the web container invokes the doCatch( ) method and the tag handler prints the exception message where the JSP would otherwise output the image produced by the tag. Our doFinally( ) method does not do anything, because this code does not have any open resources such as a FileInputStream .

See Also

The JSP 2.0 specification web page:; Recipe 22.2 and Recipe 22.3 on creating TLD files for tag libraries; Recipe 22.4 and Recipe 22.5 on packaging a tag library in a web application; Recipe 22.6 on using the custom tag in a JSP; Recipe 22.8 and Recipe 22.9 on creating a simple tag handler; Recipe 22.10 on using the simple tag handler in a JSP; Recipe 22.11-Recipe 22.14 on using a JSP tag file ; Recipe 22.15 on adding a listener class to a tag library; the custom tag sections of Hans Bergsten's JavaServer Pages , Third Edition (O'Reilly).

Java Servlet & JSP Cookbook
Java Servlet & JSP Cookbook
ISBN: 0596005725
EAN: 2147483647
Year: 2004
Pages: 326

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: