12.8 The errorPage and isErrorPage Attributes

The errorPage attribute specifies a JSP page that should process any exceptions (i.e., something of type Throwable ) thrown but not caught in the current page. It is used as follows :

 
 <%@ page errorPage="Relative URL" %> 

The exception thrown will automatically be available to the designated error page by means of the exception variable.

The isErrorPage attribute indicates whether or not the current page can act as the error page for another JSP page. Use of isErrorPage takes one of the following two forms:

 
 <%@ page isErrorPage="true" %> <%@ page isErrorPage="false" %> <%-- Default --%> 

For example, Listing 12.4 shows a JSP page that computes speed based on distance and time parameters. The page neglects to check whether the input parameters are missing or malformed , so an error could easily occur at runtime. However, the page designates SpeedErrors.jsp (Listing 12.5) as the page to handle errors that occur in ComputeSpeed.jsp , so the user does not receive the typical terse JSP error messages. Note that SpeedErrors.jsp is placed in the WEB-INF directory. Because servers prohibit direct client access to WEB-INF , this arrangement prevents clients from accidentally accessing SpeedErrors.jsp directly. When an error occurs, SpeedErrors.jsp is accessed by the server , not by the client : error pages of this sort do not result in response.sendRedirect calls, and the client sees only the URL of the originally requested page, not the URL of the error page.

Figures 12-7 and 12-8 show results when good and bad input parameters are received, respectively.

Figure 12-7. ComputeSpeed.jsp when it receives legal values.

graphics/12fig07.jpg

Figure 12-8. ComputeSpeed.jsp when it receives illegal values. Note that the address line shows the URL of ComputeSpeed.jsp , not SpeedErrors.jsp .

graphics/12fig08.jpg

Note that the errorPage attribute designates page-specific error pages. To designate error pages that apply to an entire Web application or to various categories of errors within an application, use the error-page element in web.xml . For details, see Volume 2 of this book.

Listing 12.4 ComputeSpeed.jsp
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE>Computing Speed</TITLE> <LINK REL=STYLESHEET       HREF="JSP-Styles.css"       TYPE="text/css"> </HEAD> <BODY>  <%@ page errorPage="/WEB-INF/SpeedErrors.jsp" %>  <TABLE BORDER=5 ALIGN="CENTER">   <TR><TH CLASS="TITLE">       Computing Speed</TABLE> <%! // Note lack of try/catch for NumberFormatException if // value is null or malformed. private double toDouble(String value) {   return(Double.parseDouble(value)); } %> <% double furlongs = toDouble(request.getParameter("furlongs")); double fortnights = toDouble(request.getParameter("fortnights")); double speed = furlongs/fortnights; %> <UL>   <LI>Distance: <%= furlongs %> furlongs.   <LI>Time: <%= fortnights %> fortnights.   <LI>Speed: <%= speed %> furlongs per fortnight. </UL> </BODY></HTML> 
Listing 12.5 SpeedErrors.jsp
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE>Error Computing Speed</TITLE> <LINK REL=STYLESHEET       HREF="JSP-Styles.css"       TYPE="text/css"> </HEAD> <BODY>  <%@ page isErrorPage="true" %>  <TABLE BORDER=5 ALIGN="CENTER">   <TR><TH CLASS="TITLE">       Error Computing Speed</TABLE> <P> ComputeSpeed.jsp reported the following error: <I><%= exception %></I>. This problem occurred in the following place: <PRE> <%@ page import="java.io.*" %> <% exception.printStackTrace(new PrintWriter(out)); %> </PRE> </BODY></HTML> 


Core Servlets and JavaServer Pages (Vol. 1.Core Technologies)
Core Servlets and Javaserver Pages: Core Technologies, Vol. 1 (2nd Edition)
ISBN: 0130092290
EAN: 2147483647
Year: 2002
Pages: 194

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net