April 12, 2011

Error Handling by Error codes in a Web Application

Error Handling in JSPs can be configured to set up custom error pages to be rendered to the response for a particular error code (of the status code) of the HTTP response message.

Status codes from 400 to 499 are designated as error codes.

1) Configure error handling for the error code 404 (resource not available) in the WebContent/WEB-INF/web.xml file, as follows,


2) Create a custom error page for error code 404 - my404page.jsp - in the WebContent folder. Make sure that the isErrorPage attribute of the page directive is set to true to designate the page as an error page.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" isErrorPage="true" %>
<title>My 404 Page</title>

<p>This is a custom 404 page</p>


3) Test the code by typing the following URL (of an unavailable JSP) in the browser,


If the custom error page does not show up in Internet Explorer, do the following:
- Go to 'Tools' menu and under it 'Internet Options'
- Click on "Advanced"
- Scroll down and uncheck the checkbox, "Show friendly HTTP error messages"

If the custom error page does not show up in Google Chrome, do the following:
- Go to Options
- Click on "Under the Hood"
- Uncheck the checkbox, "Use a web service to help resolve navigation errors"

No comments:

Post a Comment