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,

<error-page>
    <error-code>404</error-code>
    <location>/my404page.jsp</location>
</error-page>


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" %>
<html>
<head>
<title>My 404 Page</title>
</head>
<body>

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

</body>
</html>


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

http://localhost:8080/WebAppName/notthere.jsp

Note:
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