Section 14.7. Error Pages

   

14.7 Error Pages

You can specify a custom error page to use with your JSPs just as you can in ColdFusion. In order to use an error page in ColdFusion, you need to specify its name in the < cferror > tag in the application.cfm file, like this:

 <cferror type="request" template = "myRequestErrorPage.cfm"> 

On this page you can then use certain built-in ColdFusion error objects; for instance, you can reference ERROR.diagnostics in order to output information about the bad request.

It seems we are starting to see a number of ways in which the application.cfm file can be thought of as similiar to the web.xml file. It contains startup and initialization parameters and controls what happens in the application.

Anyway, in JSP, you can tell the application what error page to use for different HTTP error codes in the web.xml file. Add this entry:

 <error-page>     <error-code>404</error-code>    <location>/chp14/error404.jsp</location> </error-page> 

The <location> tag requires a path relative to the root of the application that points to the location of the file you want to use. This is the equivalent to our <cferror> code above. However, in one thing that is different about the three types of messages ColdFusion limits us to, you can specify a different error page for each different kind of HTTP error returned by the request.

14.7.1 error404.jsp

 <%--      File: error404.jsp     Purpose: display a custom error message --%> <%@ page isErrorPage = "true" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional// EN"> <HTML> <HEAD> <TITLE>page not found (404)</TITLE> </HEAD> <BODY BGCOLOR="#FFFFFF"> <CENTER> <table cellpadding="5" border="0" width="500" align="center" valign="middle"> <tr> <td> <font face="arial, helvetica, sans-serif" size="3" color="#000000"> <blockquote> <b>The page you requested:</b> <br>  <%=request.getRequestURL() %>  <br> <b>is not available on this server.</b> <br> <b>Please check the address and try again. <br> <br>If you continue to receive this message, and feel you have received it in error,<br> please <a href="mailto:webmaster@mysite.com">contact the Webmaster</a> for this server.</b> </blockquote> </font> </td> </tr> </table> </center> </BODY> </HTML> 

Another kind of error page, that is, one for a different HTTP status code, could include exception object methods , which are the rough equivalent of the ColdFusion ERROR.diagnostics message:

 <%= e.getMessage() %> 

and

 <%= e.printStackTrace() %> // like CF's ERROR.TagContext 

Tip

Remember that you must specify the elements in your web.xml file in a certain order. For instance, you must put the <error-page> element after any <servlet> element. The end of this chapter contains the complete list of possible values for this file and the order in which they must appear.


So now our web.xml file, if you kept adding to the same one from the previous chapter, looks like this:

 <?xml version="1.0" encoding="ISO-8859-1"?>  <!DOCTYPE web-app     PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"     "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app>   <servlet>       <servlet-name>Login</servlet-name>    <servlet-class>com.javaforcf.utils.Login</servlet-class> </servlet>   <servlet-mapping>       <servlet-name>Login</servlet-name>       <url-pattern>*.jfcf</url-pattern>    </servlet-mapping>    <error-page>       <error-code>404</error-code>       <location>/chp14/error404.jsp</location>    </error-page> </web-app> 

If we now reference some file within the purview of the javaforcf Web application that does not exist, then we should be redirected to the custom error page we have defined, as shown in Figure 14.1.

Figure 14.1. Our custom error page is displayed when an HTTP 404 is returned to the client.

graphics/14fig01.gif

As with ColdFusion or IIS or other custom error pages, these make more than a lovely decorative finish to your applications they allow you to appear more professional and truly offer some help to your users. Microsoft.com has a very helpful error page, for instance ”it offers you a search form and displays a complete site map.


   
Top


Java for ColdFusion Developers
Java for ColdFusion Developers
ISBN: 0130461806
EAN: 2147483647
Year: 2005
Pages: 206
Authors: Eben Hewitt

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