Jump to: navigation, search

Difference between revisions of "Jetty/Howto/Custom Error Pages"

< Jetty‎ | Howto
Line 66: Line 66:
 
==Custom error handler class==
 
==Custom error handler class==
  
A context may be configured with a custom error handler class that extends [[http://dev.eclipse.org/viewcvs/index.cgi/jetty/trunk/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ErrorHandler.java?root=RT_Jetty&view=markup|ErrorHandler]] (for webapp contexts it must extend [[http://dev.eclipse.org/viewcvs/index.cgi/jetty/trunk/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/ErrorPageErrorHandler.java?root=RT_Jetty&view=markup|ErrorPageErrorHandler]]).
+
A context may be configured with a custom error handler class that extends either [http://dev.eclipse.org/viewcvs/index.cgi/jetty/trunk/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ErrorHandler.java?root=RT_Jetty&view=markup|ErrorHandler] for contexts, or [http://dev.eclipse.org/viewcvs/index.cgi/jetty/trunk/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/ErrorPageErrorHandler.java?root=RT_Jetty&view=markup|ErrorPageErrorHandler] for webapp contexts.
  
 
The following methods may be implemented to control the appearance of the error pages:
 
The following methods may be implemented to control the appearance of the error pages:

Revision as of 09:29, 10 February 2010



Introduction

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:

<error-page>
  <error-code>404</error-code>
  <location>/jspsnoop/ERROR/404</location>
</error-page>

Exception example:

<error-page>
  <exception-type>java.io.IOException</exception-type>
  <location>/jspsnoop/IOException</location>
</error-page>

Context file configuration

Context files are normally located in <jetty.home>/contexts/ (see Jetty/Feature/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" "http://jetty.mortbay.org/configure.dtd">
 
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
  <Set name="contextPath">/test</Set>
  <Set name="war">
    <SystemProperty name="jetty.home" default="."/>/webapps/test
  </Set>
 
  <!-- by Code -->
  <Get name="errorHandler">
    <Call name="addErrorPage">
      <Arg type="int">404</Arg>
      <Arg type="String">/jspsnoop/ERROR/404</Arg>
    </Call>
  </Get>
 
  <!-- by Exception -->
  <Get name="errorHandler">
    <Call name="addErrorPage">
      <Arg type="java.lang.Class">java.io.IOException</Arg>
      <Arg type="String">/jspsnoop/IOException</Arg>
    </Call>
  </Get>
 
  <!-- 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>
    </Call>
  </Get>
</Configure>

Custom error handler class

A context may be configured with a custom error handler class that extends either [http://dev.eclipse.org/viewcvs/index.cgi/jetty/trunk/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ErrorHandler.java?root=RT_Jetty&view=markup