Jump to: navigation, search

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

< Jetty‎ | Howto
Line 1: Line 1:
 
{{Jetty Howto
 
{{Jetty Howto
 
| introduction = There are three ways to create custom error pages that are described below.
 
| introduction = There are three ways to create custom error pages that are described below.
| steps =
 
 
==Define error pages in web.xml==
 
==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.
+
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
 
* ''error-code'' - integer value
 
* ''exception-type'' - fully qualified class name of a Java Exception type
 
* ''exception-type'' - fully qualified class name of a Java Exception type
Line 23: Line 22:
 
  </error-page>
 
  </error-page>
  
h2. 2. context file configuration.
+
== Context file configuration ==
  
Context files are nomrall located in <jetty.home>/contexts/?.xml (see [ContextDeployer] for more detail). Context files
+
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:
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:
+
{code:xml}
+
<?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.mortbay.jetty.webapp.WebAppContext">
+
<?xml version="1.0" encoding="ISO-8859-1"?>
  <Set name="contextPath">/test</Set>
+
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">
  <Set name="war">
+
    <SystemProperty name="jetty.home" default="."/>/webapps/test
+
  </Set>
+
  
  <!-- by Code -->
+
<Configure class="org.mortbay.jetty.webapp.WebAppContext">
  <Get name="errorHandler">
+
  <Set name="contextPath">/test</Set>
    <Call name="addErrorPage">
+
  <Set name="war">
      <Arg type="int">404</Arg>
+
    <SystemProperty name="jetty.home" default="."/>/webapps/test
      <Arg type="String">/jspsnoop/ERROR/404</Arg>
+
  </Set>
    </Call>
+
  </Get>
+
  
  <!-- by Exception -->
+
  <!-- by Code -->
  <Get name="errorHandler">
+
  <Get name="errorHandler">
    <Call name="addErrorPage">
+
    <Call name="addErrorPage">
      <Arg type="java.lang.Class">java.io.IOException</Arg>
+
      <Arg type="int">404</Arg>
      <Arg type="String">/jspsnoop/IOException</Arg>
+
      <Arg type="String">/jspsnoop/ERROR/404</Arg>
    </Call>
+
    </Call>
  </Get>
+
  </Get>
  
  <!-- by Code Range -->
+
  <!-- by Exception -->
  <Get name="errorHandler">
+
  <Get name="errorHandler">
    <Call name="addErrorPage">
+
    <Call name="addErrorPage">
      <Arg type="int">500</Arg>
+
      <Arg type="java.lang.Class">java.io.IOException</Arg>
      <Arg type="int">599</Arg>
+
      <Arg type="String">/jspsnoop/IOException</Arg>
      <Arg type="String">/dump/errorCodeRangeMapping</Arg>
+
    </Call>
    </Call>
+
  </Get>
  </Get>
+
 
</Configure>
+
  <!-- by Code Range -->
{code}
+
  <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>
  
 
h2. 3. Custom error handle class.
 
h2. 3. Custom error handle class.

Revision as of 08:04, 10 February 2010



Introduction

There are three ways to create custom error pages 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.mortbay.jetty.webapp.WebAppContext">
  <Set name="contextPath">/test</Set>
  <Set name="war">
    <SystemProperty name="jetty.home" default="."/>/webapps/test
  </Set>
  <Get name="errorHandler">
    <Call name="addErrorPage">
      <Arg type="int">404</Arg>
      <Arg type="String">/jspsnoop/ERROR/404</Arg>
    </Call>
  </Get>
  <Get name="errorHandler">
    <Call name="addErrorPage">
      <Arg type="java.lang.Class">java.io.IOException</Arg>
      <Arg type="String">/jspsnoop/IOException</Arg>
    </Call>
  </Get>
  <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>

h2. 3. Custom error handle class.

A context may be configured with a custom error handler class that extends [ErrorHandler



Tips, Hints, and Warnings

(optional)

Examples

(optional)

Snippets and Screenshots

(optional) - for chunks of code that are too big to go into the Steps section, or screenshots

Additional Resources

(optional) - links, additional references

(optional) - categor(ies) to use for this page. If blank, will use the template name as a default category Example: