JSP Best Practices: Error Handling in Custom tags

Source: Internet
Author: User
Tags error handling

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>
&LT;TD width= "align=" "left" valign= "Top" > </td>
</tr>
<!--end main content-->
<!--Begin footer Section-->
<tr>
&LT;TD width= "align=" "left" valign= "Top" bgcolor= "#330066" > </td>
&LT;TD align= "left" valign= "Top" > </td>
&LT;TD class= "Footer" align= "left" valign= "top" ><div align= "center" ><br>
&copy; 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>
&LT;TD align= "left" valign= "Top" > </td>
&LT;TD width= "align=" "right" valign= "top" bgcolor= "#330066" > </td>
</tr>
</table>
<!--end footer Section-->

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.