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:

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

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:

    <error-page>
        <exception-type>java.lang.NullPointerException</exception-type>
        <location>/errorpage.jsp</location>
    </error-page>

    <error-page>
        <exception-type>java.lang.NumberFormatException</exception-type>
        <location>/errorpage.jsp</location>
    </error-page>

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="java.io.ByteArrayOutputStream" %>
<%@ page import="java.io.PrintStream" %>
<%--
  User: Muhammad Yusuf
  Date: Aug 30, 2005
  Time: 5:56:29 PM
--%>
<%@ page isErrorPage="true" %>
<html>
<head>
    <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;
        }
    </style>
</head>

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

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

        <p>
</body>
</html>

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));
    out.print(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.

josescalia

One thought on “Error Page Customizing on Java Web Application

  1. Pingback: Error Page Customizing on Java Web Application | XML Developer India

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s