Handling Errors

There are a number of possible error or failure conditions that need to be handled by your servlet. These errors fall into the following two categories:

  • HTTP errors

  • Servlet exceptions

HTTP Errors

One way of handling HTTP errors in the deployment descriptor has already been briefly mentioned. By using the XML error-page tag, you can ensure that the client is sent an application-specific page when it gets an error. You can use this page to send appropriate information in response to any HTTP error code. The following, for example, replaces the standard HTTP “404 Not found” error page with one that will have been written for this application:

<error-page>
  <error-code>404</error-code>
  <location>/Servlets/error404.html</location>
</error-page>

If the user tries to access a page that is not found on the server, the /Servlets/error404.html page will be returned to the client. Using the J2EE RI, this error page redirection can be included in the deployment descriptor by adding an Error Mapping entry to the File Refs tab for your application (see Figure 12.16).

Figure 12.16. deploytool File Refs tab.


Generating HTTP Status Codes

There may be times when it is useful for the servlet to generate its own HTTP status codes. In an error situation, the servlet can use either of the following methods to set the HTTP status code

public void HttpServletResponse.sendError(int sc)
public void HttpServletResponse.sendError(int sc, String msg)

The API defines a set of constants that can be used to refer to HTTP status codes. For example, the error status returned can be set to 404, as in the following:

res.sendError(res.SC_NOT_FOUND);

This will he handled by the server and browser in exactly the same way as any “404 Not found” error, including using any error-page redirection specified in the deployment descriptor.

Send Redirect

Another useful thing a servlet can do if an error is detected is to redirect the client to another URL. This can also be used in obsolete servlets to redirect the client seamlessly to a new application.

Use HttpServletResponse.sendRedirect(String location) to redirect the response to the specified location.

The following will redirect the client to another page called "/Servlets/AnotherHTMLPage".

res.sendRedirect("/Servlets/AnotherHTMLPage");

Servlet Exception Handling

In general, you should take care to catch all servlet-generated exceptions in the servlet and take appropriate action to inform the client what has happened. If you don't, instead of the expected page, the user is likely to see a Java exception stack trace on the screen. Although you may find the information useful during development, an exception stack trace sent to the client will leave most users bewildered and worried.

When a fatal exception occurs, there is often nothing to do but tell the client that a serious error has occurred. The following catch block simply returns a HTTP 503 Service Unavailable error to the client, along with a suitable error message:

catch (RemoteException ex) {
    res.sendError(res. SC_SERVICE_UNAVAILABLE, "Internal communication error");
}

In other situations, it might be appropriate to redirect the user to another URL that has a form that the user can use to report the error:

catch (RemoteException ex) {
    res. sendRedirect("/Servlets/ReportErrorPage");
}

A servlet can throw a number of exceptions that will be handled by the server. During initialization or while handling requests, the servlet instance can throw an UnavailableException or a ServletException. If this happens, the action taken by the server is implementation specific, but it is likely to return a 503 Service Unavailable response to the client. In this case, you can still use the error-page tag in the web.xml file to send the client an appropriate message when the server handles the exception.

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

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