In Java web application development, error handling is required to build user-friendly and secured websites. In case of errors occurred, the user doesn’t have to see technical error details which they cannot understand. Instead, they should see a friendly page that tells them what happened. Also, it’s recommended to handle error properly to hide sensitive, technical information which can be exploited by hackers.

Java EE allows you to handle errors (by HTTP error codes and Java exception types) easily by putting some pieces of configuration in the web deployment descriptor document (the web.xml file). That means instead of showing the default error pages provided by the server, you can show your own error pages.

For example, putting the following declaration in the web.xmlwill override the handling of HTTP 404 error by the server:

<error-page>
	<error-code>404</error-code>
	<location>/Error404.jsp</location>
</error-page>
With this declaration, if any 404 error occurred, your own error page is displayed to the user (Error404.jsp page in the above example). Note that your custom error page is relative to the web application’s context root.

In your own error handling page, you can hide the technical information by showing only readable message to the user. For example, here’s the HTML code of the Error404.jsp page:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
	"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>404 Error - Page Not Found</title>
</head>
<body>
	<center>
		<h1>Sorry, the page you requested were not found.</h1>
	</center>
</body>
</html>
So instead of letting the user this default error page provided by the server (e.g. Tomcat):

Tomcat 404 default error

You can show them a friendlier version like this:

Custom 404 error page



 

You can add other <error-page> elements to handle other error codes. For example, the following XML snippet declares the page that handles the HTTP 500 error:

<error-page>
	<error-code>500</error-code>
	<location>/Error500.jsp</location>
</error-page>
 

Similarly, you can also declare JSP pages to handle exceptions thrown by the application. For example, the following XML code declares which page handles exceptions of type java.io.IOException:

<error-page>
	<exception-type>java.io.IOException</exception-type>
	<location>/IOException.jsp</location>
</error-page>
Here, if an IOException occurred, the server will show the IOException.jsp page you specify.

You can also declare a generic exception page to catch all types of exception like this:

<error-page>
	<exception-type>java.lang.Throwable</exception-type>
	<location>/Exception.jsp</location>
</error-page>
Here, if any exception occurred and it is not declared to be handled specifically in the web.xml, then the server will show the Exception.jsp page, because all exceptions are subtypes of the Throwable type.

Note that the most specific exception type is chosen if there are multiple matches. Consider the following declaration:

<error-page>
	<exception-type>java.io.IOException</exception-type>
	<location>/IOException.jsp</location>
</error-page>
<error-page>
	<exception-type>java.lang.Throwable</exception-type>
	<location>/Exception.jsp</location>
</error-page>
In this case, if an IOException occurred, both the handlers match, but the handler for java.io.Exception is chosen because it is more specific than the one for java.lang.Throwable.

But if a ServletException occurred, the handler for java.lang.Throwable is chosen because there’s no specific handling for this exception.

That’s how to handle errors in web.xml for Java web applications. You can download the sample project in the attachment.

 

Related Java Error Handling Tutorials:

 

Other Java Servlet Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.



Attachments:
Download this file (WebXmlErrorHandlingExample.zip)WebXmlErrorHandlingExample.zip[Error Handling Example for Java Web Application]13 kB

Add comment

   


Comments 

#2Dhiman Roy2023-05-28 21:04
I have executed the above code. But in all the cases the result is "Sorry, the page you requested were not found." Kindly clarify
Quote
#1Jennifer2021-04-14 23:38
Very helpful article
Quote