5.8. Designating Pages to Handle Errors

Now, I realize that you never make any mistakes when developing servlets and JSP pages and that all of your pages are so clear that no rational person could be confused by them. Still, however, the world is full of irrational people, and users could supply illegal parameters, use incorrect URLs, or fail to provide values for required form fields. Besides, other developers might not be as careful as you are, and they should have some tools to overcome their deficiencies.

The error-page element is used to handle problems. It has two possible subelements: error-code and exception-type. The first of these, error-code, designates what URL to use when a designated HTTP error code occurs. (If you aren’t familiar with HTTP error codes, they are discussed in Chapter 6 of Core Servlets and JavaServer Pages, which is available in PDF in its entirety at http://www.moreservlets.com.) The second of these subelements, exception-type, designates what URL to use when a designated Java exception is thrown but not caught. Both error-code and exception-type use the location element to designate the URL. This URL must begin with / . The page at the place designated by location can access information about the error by looking up two special-purpose attributes of the HttpServletRequest object: javax.servlet.error.status_code and javax.servlet.error.message.

Recall that it is important to declare the web-app subelements in the proper order within web.xml. Section 5.2 (The Order of Elements within the Deployment Descriptor) gives complete details on the required ordering. For now, however, just remember that error-page comes near the end of the web.xml file, after servlet, servlet-name, and welcome-file-list.

The error-code Element

To better understand the value of the error-code element, consider what happens at most sites when you type the filename incorrectly. You typically get a 404 error message that tells you that the file can’t be found but provides little useful information. On the other hand, try typing unknown filenames at www.microsoft.com, www.ibm.com, or especially www.bea.com. There, you get useful messages that provide alternative places to look for the page of interest. Providing such useful error pages is a valuable addition to your Web application. In fact, http://www.plinko.net/404/ has an entire site devoted to the topic of 404 error pages. This site includes examples of the best, worst, and funniest 404 pages from around the world.

Listing 5.13 shows a JSP page that could be returned to clients that provide unknown filenames. Listing 5.14 shows the web.xml file that designates Listing 5.13 as the page that gets displayed when a 404 error code is returned. Figure 5-15 shows a typical result. Note that the URL displayed in the browser remains the one supplied by the client; the error page is a behind-the-scenes implementation technique.

Figure 5-15. Use of helpful 404 messages can enhance the usability of your site.


Finally, remember that the default configuration of Internet Explorer version 5, in clear violation of the HTTP spec, ignores server-generated error messages and displays its own standard error message instead. Fix this by going to the Tools menu, selecting Internet Options, clicking on Advanced, then deselecting Show Friendly HTTP Error Messages.

Core Warning

In the default configuration, Internet Explorer improperly ignores servergenerated error messages.


Listing 5.13. NotFound.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
<HTML> 
<HEAD><TITLE>404: Not Found</TITLE></HEAD> 
<BODY BGCOLOR="#FDF5E6"> 
<H2>Error!</H2> 
I'm sorry, but I cannot find a page that matches 
<%= request.getRequestURI() %> on the system. Maybe you should 
try one of the following: 
<UL> 
  <LI>Go to the server's <A HREF="/">home page</A>. 
  <LI>Search for relevant pages.<BR> 
      <FORM ACTION="http://www.google.com/search"> 
      <CENTER> 
      Keywords: <INPUT TYPE="TEXT" NAME="q"><BR> 
      <INPUT TYPE="SUBMIT" VALUE="Search"> 
      </CENTER> 
      </FORM> 
  <LI>Admire a random multiple of 404: 
      <%= 404*((int)(1000*Math.random())) %>. 
  <LI>Try a <A HREF="http://www.plinko.net/404/rndindex.asp" 
               TARGET="_blank"> 
      random 404 error message</A>. From the amazing and 
      amusing plinko.net <A HREF="http://www.plinko.net/404/"> 
      404 archive</A>. 
</UL> 
</BODY></HTML> 

Listing 5.14. web.xml (Excerpt designating error pages for HTTP error codes)
<?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> 
  <error-page>
							<error-code>404</error-code>
							<location>/NotFound.jsp</location>
							</error-page> 
  <!-- ... --> 
</web-app> 

The exception-type Element

The error-code element handles the case when a request results in a particular HTTP status code. But what about the equally common case when the servlet or JSP page returns 200 but generates a runtime exception? That’s the situation handled by the exception-type element. You only need to supply two things: a fully qualified exception class and a location, as below:

<error-page> 
  <exception-type>packageName.className</exception-type>
							<location>/SomeURL</location> 
</error-page> 

Then, if any servlet or JSP page in the Web application generates an uncaught exception of the specified type, the designated URL is used. The exception type can be a standard one like javax.ServletException or java.lang.OutOfMemoryError, or it can be an exception specific to your application.

For instance, Listing 5.15 shows an exception class named DumbDeveloperException that might be used to flag particularly knuckle-headed mistakes by clueless programmers (not that you have any of those types on your development team). The class also contains a static method called dangerousComputation that sometimes generates this type of exception. Listing 5.16 shows a JSP page that calls dangerousComputation on random integer values. When the exception is thrown, DDE.jsp (Listing 5.17) is displayed to the client, as designated by the exception-type entry shown in the web.xml version of Listing 5.18. Figures 5-16 and 5-17 show lucky and unlucky results, respectively.

Figure 5-16. Fortuitous results of RiskyPage.jsp.


Figure 5-17. Unlucky results of RiskyPage.jsp.


Listing 5.15. DumbDeveloperException.java
package moreservlets; 

/** Exception used to flag particularly onerous 
    programmer blunders. Used to illustrate the 
    exception-type web.xml element. 
 */ 

public class DumbDeveloperException extends Exception {
  public DumbDeveloperException() {
    super("Duh. What was I *thinking*?"); 
  } 

  public static int dangerousComputation(int n) 
      throws DumbDeveloperException {
    if (n < 5) {
      return(n + 10); 
    } else {
      throw(new DumbDeveloperException()); 
    } 
  } 
} 

Listing 5.16. RiskyPage.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
<HTML> 
<HEAD><TITLE>Risky JSP Page</TITLE></HEAD> 
<BODY BGCOLOR="#FDF5E6"> 
<H2>Risky Calculations</H2> 
<%@ page import="moreservlets.*" %> 
<% int n = ((int)(10 * Math.random())); %> 
<UL> 
  <LI>n: <%= n %> 
  <LI>dangerousComputation(n): 
      <%= DumbDeveloperException.dangerousComputation(n) %> 
</UL> 
</BODY></HTML> 

Listing 5.17. DDE.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
<HTML> 
<HEAD><TITLE>Dumb</TITLE></HEAD> 
<BODY BGCOLOR="#FDF5E6"> 
<H2>Dumb Developer</H2> 
We're brain dead. Consider using our competitors. 
</BODY></HTML> 

Listing 5.18. web.xml (Excerpt designating error pages for exceptions)
<?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> 
  <!-- ... --> 
  <error-page>
							<exception-type>
							moreservlets.DumbDeveloperException
							</exception-type>
							<location>/DDE.jsp</location>
							</error-page> 
  <!-- ... --> 
</web-app> 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.5.57