Introduction: When you reference more interactivity in a custom tag, you also increase the likelihood of error occurrence, especially if the error occurs as an illegal argument. In this issue of JSP best practices, Brett McLaughlin shows you how to capture and ignore illegalargumentexception exceptions in your source code.
As we discuss JSP custom tags, we have managed to circumvent the important details of error handling. In the previous half of the JSP best practices series that we've launched, most of the things we've said are functions provided by the JSP container, such as Param, out, and import tags. In these cases, error handling is the JSP specification and the WEB server implementation, which means we can't do anything if we want to.
But in the last two, we have focused on custom tag libraries and properties. Not only did we start adding new functionality to the JSP page, but we also created properties that allow JSP authors to interact with the code. Also, it is likely that we have introduced a new type of error into the JSP page, or that the fault of the author of the page (later) may have catastrophic consequences for markup code or subsequent output. In either case, it becomes necessary to add an error-handling mechanism to the custom tag.
A description of the example: the code for all the examples we use in this issue of JSP best practices is based on the code developed in the previous issue. Before you continue to read this article, you should make sure that you have successfully completed the setup and coding exercises for the previous issue.
Review
We'll start by repeating the previous steps and reviewing some of the code we developed in the previous issue, but this time we'll focus on finding bugs (or potential bugs) in the code.
First, let's recall the new attribute format added to the lastmodified tag. This property allows the page author to pass in a format string that the Java.text.SimpleDateFomrat class can use to customize the format of the last modified date stamp. Although this interaction is essential for most JSP pages, it also introduces potential error possibilities to our markup.
For example, a page author could easily provide a format string that could lead to a nasty error message. If you want to see what kind of message you have, you can modify your footer file (or any other JSP page that uses the lastmodified tag) as in Listing 1:
Listing 1. An invalid format string
<%@ taglib prefix= "Site-utils"
Uri= "Http://www.newInstance.com/taglibs/site-utils"%>
</td>
<TD width= "align=" "left" valign= "Top" > </td>
</tr>
<!--end main content-->
<!--Begin footer Section-->
<tr>
<TD width= "align=" "left" valign= "Top" bgcolor= "#330066" > </td>
<TD align= "left" valign= "Top" > </td>
<TD class= "Footer" align= "left" valign= "top" ><div align= "center" ><br>
© 2003
<a href= "mailto:webmaster@newinstance.com" >brett mclaughlin</a><br>
Last Updated: <site-utils:lastmodified
Format= "phh:mm A, mm/dd/yyyy"/>
</div></td>
<TD align= "left" valign= "Top" > </td>
<TD width= "align=" "right" valign= "top" bgcolor= "#330066" > </td>
</tr>
</table>
<!--end footer Section-->