Error Page Customizing on Java Web Application

Have we ever seen this web page:

tomcat 404

Or this one:

This web page is standard error page produced by Apache Tomcat when our web application have some errors. These error occurred because the logic of our application had a mistake or user do some mistaken when requesting via HTTP browser.

We can change these pages into our custom page but have same function just like the page above. The advantages is we can redesign the page just like we  desired to. And other function is we could hide some error points, so the user can’t find out what’s the problem behind.

Before that, we can divide type of error that probably occurred on our web application. 1st, caused by user fault such as accesing file that’s not exist on the server, and the 2nd caused by error logic on our web application.

Let’s try to customize the error page that could be happen like above.

1. Creating error page caused by user:

When creating this type of error page, we can create it by adding some lines on deployment descriptor (web.xml) like this:


On the source above, we can see clearly, if File Not Found Error occurred when user requesting via HTTP browser and the web application will automatically redirect to a 404.jsp file, and of course we have created this file before and customed like this one:

2.  Creating Error Page caused by Programming mistakes:

While configuring the error page caused by our programming mistakes, we can do it by adding these lines on deployment descriptor:



On the example source, we can see that the error can be define one by one, such as Null Pointer Exception Error, nor Number Format Exception Error.

On the sampe source too, we can only create one jsp page to accommodate and informing the stacktrace that occurred caused by our programming mistakesl.

The source code of this jsp file is:

<%@ page import="" %>
<%@ page import="" %>
  User: Muhammad Yusuf
  Date: Aug 30, 2005
  Time: 5:56:29 PM
<%@ page isErrorPage="true" %>
    <title>JSP Error Page</title>
    <meta http-equiv="Pragma" content="no-cache">
    <style type="text/css">
        body {
            background-color: #cccccc;
            font-family: verdana, sans-serif, monospace;
            font-size: medium;

<body bgcolor=#ffffff>
<font face="Verdana">
        <h2><font color=#DB1260>Error Page</font></h2>
    <p> An exception was thrown: <b> <%=exception %>
        <p> With the following stack trace:

    ByteArrayOutputStream ostr = new ByteArrayOutputStream();
    exception.printStackTrace(new PrintStream(ostr));
        <br><a class="Verdana" href="#">Back To Main</a>


On this source code, the occurred exception can be defined by using this tag <%=exception%> and the stack-trace that probably occurred can be print-out to the page like this:

    ByteArrayOutputStream ostr = new ByteArrayOutputStream();
    exception.printStackTrace(new PrintStream(ostr));

And here’s the final result of the 2nd error page caused by our programming mistaken.

Or like this:

Hope this will helpfull

Menteng, 29 November 2009.