Displaying Specific 'Error' Pages for Specific Errors

A Web Application including HTML, Java Server Pages, Servlets, and other Java classes can cause many kinds of errors. A Java Application Server responds to most of these errors by showing a traceback from the Java Virtual Machine. There are several reasons that you may prefer not to have this traceback shown, especially in a production environment, such as:

For these and other reasons, the JSP spec allows you redirect errors to specific HTML or JSP pages that you create for these situations. The technique involves adding information to the web.xml file describing the pages that are to be displayed under certain conditions that you specify. The web.xml file is located in the WEB-INF directory for the Web Application. We will describe two such situations: HTML page not found, and a Java NullPointerException:

HTML page not found

If a URL points to an HTML that doesn't exist, a traceback such as this will be displayed:

404 Not Found
/default/demo2.html:
allaire.jrun.JRunServletException: not found
at allaire.jrun.file.FileServlet.service(FileServlet.java:148)
at allaire.jrun.servlet.JRunServletPool.service(JRunServletPool.java:90)
at allaire.jrun.servlet.JRunSE.service(JRunSE.java:1417)
at allaire.jrun.session.JRunSessionService.service(JRunSessionService.java:1106)
at allaire.jrun.servlet.JRunSE.runServlet(JRunSE.java:1271)
at allaire.jrun.servlet.JRunRequestDispatcher.forward(JRunRequestDispatcher.java:89)
at allaire.jrun.servlet.JRunSE.service(JRunSE.java:1557)
at allaire.jrun.servlet.JRunSE.service(JRunSE.java:1547)
at allaire.jrun.servlet.JvmContext.dispatch(JvmContext.java:364)
at allaire.jrun.http.WebEndpoint.run(WebEndpoint.java:115)
at allaire.jrun.ThreadPool.run(ThreadPool.java:272)
at allaire.jrun.WorkerThread.run(WorkerThread.java:75)

The following lines can be added to the web.xml file (that is located in the WEB-INF directory):

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

There is a 'slash' in front of the '/404.html' because any HTML referenced here is relative to the root directory of the Web Application. If your Web Application has all its HTML in a subdirectory called 'html', then you could put the HTML in this directory and this entry in web.xml would look like this:

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

An appropriate place to add this to web.xml is between the following two lines that most likely already exists in web.xml:

<description>Default Application...</description>
…add the new xml here
<session-config>

You then must put an HTML page called 404.html in the Web Applicaiton, that can be placed in the root directory of the Web Application (if it's referred to as /404.html). (This is at the same level as the WEB-INF directory). This HTML page can tell the user than an error has occurred and/or click on such-and-such a link to get back to the main page, and provide information come directly or indirectlly from the request or response objects that are passed to JSP, or that you can optionally store in Session variablesr.

Note: The Jrun server must be restarted in order to have the new values in web.xml take effect.

You can create a different web page for each error that can occur.

Java Exceptions

Here is a short JSP page that will produce a NullPointerException:

   <%
String s = null;
String news = s.toUpperCase();
%>

Executing this JSP page will result in a screen similar to this:

500 Internal Server Error
/default/useNull.jsp:
java.lang.NullPointerException
at jrun__useNull2ejspc._jspService(jrun__useNull2ejspc.java:42)
at allaire.jrun.jsp.HttpJSPServlet.service(HttpJSPServlet.java:39)
at allaire.jrun.jsp.JSPServlet.service(JSPServlet.java:228)
at allaire.jrun.jsp.JSPServlet.service(JSPServlet.java:196)
at allaire.jrun.servlet.JRunSE.service(JRunSE.java:1417)
at allaire.jrun.session.JRunSessionService.service(JRunSessionService.java:1106)
at allaire.jrun.servlet.JRunSE.runServlet(JRunSE.java:1271)
at allaire.jrun.servlet.JRunRequestDispatcher.forward(JRunRequestDispatcher.java:89)
at allaire.jrun.servlet.JRunSE.service(JRunSE.java:1557)
at allaire.jrun.servlet.JRunSE.service(JRunSE.java:1547)
at allaire.jrun.servlet.JvmContext.dispatch(JvmContext.java:364)
at allaire.jrun.http.WebEndpoint.run(WebEndpoint.java:115)
at allaire.jrun.ThreadPool.run(ThreadPool.java:272)
at allaire.jrun.WorkerThread.run(WorkerThread.java:75)

The following lines can be added to web.xml:

   <error-page>
<exception-type>java.lang.NullPointerException</exception-type>
<location>/NullException.html</location>
</error-page>

The NullException.html must be added to the Web Application, for example in the root directory of the Web Application. Also, the Web Application must be restarted to have the new values in web.xml take effect.

The following lines in web.xml would catch all Java Exceptions:

   <error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/exception.html</location>
</error-page>


Catching all errors and exceptions
Here is a web.xml file that will catch all errors and exceptions, and redirect to a standard error or a standard exception page.

<web-app>
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/exception.html</location>
</error-page>
<error-page>
<exception-type>java.lang.Error</exception-type>
<location>/error.html</location>
</error-page>
</web-app>

Here are JSP pages that create an Exception and another than creates an Error:

DoException.jsp:

    <%
int i=1;
if (i > 0){
throw new java.lang.Exception("this is an exception message");
}
out.println("continue any processsing"); %>


DoError.jsp:

<%
int i=1;
if (i > 0){
throw new java.lang.Error("this is an error message");
}
out.println("continue any processsing");
%>

Then you need an exception.html and error.html page to display that there was an exception or error.

If you had just the following web.xml, then both Errors and Exceptions will both get redirected to the exception.html page:

<web-app>
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/exception.html</location>
</error-page>
</web-app>

Note: On Internet Explorer, to view the resulting HTML page sometimes requires the following browser setting:

  1. Use pull-down menu item Tools, Internet Options, Advanced-tab
  2. Under Browsing, un-click Show Friendly HTTP Messages