Skip to main content
Jump to: navigation, search

Jetty/Howto/Custom Error Pages

< Jetty‎ | Howto


Jetty 7 and Jetty 8 are now EOL (End of Life)


All development and stable releases are being performed with Jetty 9 and Jetty 10.

This wiki is now officially out of date and all content has been moved to the Jetty Documentation Hub

Direct Link to updated documentation:

There are several ways to create custom error pages in Jetty that are described below.

Define error pages in web.xml

The standard webapp configuration file located in <webapp>/WEB-INF/web.xml can be used to map errors to specific URLs with the <error-page> element. This element creates a mapping between the error-code or exception-type to the location of a resource in the web application.

  • error-code - integer value
  • exception-type - fully qualified class name of a Java Exception type
  • location - location of the resource in webapp relative to the root of the web application. Value should start with "/".

Error code example:


Exception example:


Context file configuration

Context files are normally located in <jetty.home>/contexts/ (see ContextDeployer for more details). Context files can be used to configure the default error handler provided for a context with more flexibility than is available with web.xml, specifically with the support of error code ranges:

<?xml version="1.0"  encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "">
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
  <Set name="contextPath">/test</Set>
  <Set name="war">
    <SystemProperty name="jetty.home" default="."/>/webapps/test
  <!-- by Code -->
  <Get name="errorHandler">
    <Call name="addErrorPage">
      <Arg type="int">404</Arg>
      <Arg type="String">/jspsnoop/ERROR/404</Arg>
  <!-- by Exception -->
  <Get name="errorHandler">
    <Call name="addErrorPage">
        <Call class="java.lang.Class" name="forName">
          <Arg type="String"></Arg>
      <Arg type="String">/jspsnoop/IOException</Arg>
  <!-- by Code Range -->
  <Get name="errorHandler">
    <Call name="addErrorPage">
      <Arg type="int">500</Arg>
      <Arg type="int">599</Arg>
      <Arg type="String">/dump/errorCodeRangeMapping</Arg>

Custom error handler class

A context may be configured with a custom error handler class that extends either ErrorHandler for generic contexts, or ErrorPageErrorHandler for webapp contexts.

The following methods may be implemented to control the appearance of the error pages:

void handle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch) throws IOException
void handleErrorPage(HttpServletRequest request, Writer writer, int code, String message) throws IOException
void writeErrorPage(HttpServletRequest request, Writer writer, int code, String message, boolean showStacks) throws IOException
void writeErrorPageHead(HttpServletRequest request, Writer writer, int code, String message) throws IOException
void writeErrorPageBody(HttpServletRequest request, Writer writer, int code, String message, boolean showStacks) throws IOException
void writeErrorPageMessage(HttpServletRequest request, Writer writer, int code, String message, String uri) throws IOException
void writeErrorPageStacks(HttpServletRequest request, Writer writer) throws IOException

The ErrorPageErrorHandler can also be configured to suppress stacktraces by calling setShowStacks(false).

The custom error handler may be set on the context via the API or via a context configuration file. For example a custom error handling class can be added to the javadoc context with:

<?xml version="1.0"  encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "">
<Configure class="org.eclipse.jetty.server.handler.ContextHandler">
  <Call class="org.eclipse.util.log.Log" name="debug"><Arg>Configure javadoc.xml</Arg></Call>
  <Set name="contextPath">/javadoc</Set>
  <Set name="resourceBase"><SystemProperty name="jetty.home" default="."/>/javadoc/</Set>
  <!-- Instantiate your own error handler -->
  <Set name="errorHandler">
    <New class="com.acme.handler.MyErrorHandler"/>

Server level 404 error

One may get a 'page not found' when a request is made to the server for a resource that is outside of any registered contexts. As an example, you have a domain name pointing to your public server IP yet no context is registered with jetty to serve pages for that domain. As a consequence, the server, by default, will give a listing of all contexts running on the server.

One of the quickest ways to avoid this behavior is to create a catch all context. Create a "root" web app mapped to the "/" URI. Have the index.html redirect to whatever place with a header directive.

Copyright © Eclipse Foundation, Inc. All Rights Reserved.