Last Updated on 27 June 2019   |   Print Email
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:
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):You can show them a friendlier version like this:
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:
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:
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:
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:
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.
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.
Comments