148

I am using <error-page> element in web.xml to specify the friendly error page when user encounters a certain error such as error with code of 404:

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

However, I want that if the user does not meet any error code specified in <error-page>, he or she should see a default error page. How can I do that using the element in the web.xml?

Community
  • 1
  • 1
ipkiss
  • 13,311
  • 33
  • 88
  • 123

3 Answers3

253

On Servlet 3.0 or newer you could just specify

<web-app ...>
    <error-page>
        <location>/general-error.html</location>
    </error-page>
</web-app>

But as you're still on Servlet 2.5, there's no other way than specifying every common HTTP error individually. You need to figure which HTTP errors the enduser could possibly face. On a barebones webapp with for example the usage of HTTP authentication, having a disabled directory listing, using custom servlets and code which can possibly throw unhandled exceptions or does not have all methods implemented, then you'd like to set it for HTTP errors 401, 403, 500 and 503 respectively.

<error-page>
    <!-- Missing login -->
    <error-code>401</error-code>
    <location>/general-error.html</location>
</error-page>
<error-page>
    <!-- Forbidden directory listing -->
    <error-code>403</error-code>
    <location>/general-error.html</location>
</error-page>
<error-page>
    <!-- Missing resource -->
    <error-code>404</error-code>
    <location>/Error404.html</location>
</error-page>
<error-page>
    <!-- Uncaught exception -->
    <error-code>500</error-code>
    <location>/general-error.html</location>
</error-page>
<error-page>
    <!-- Unsupported servlet method -->
    <error-code>503</error-code>
    <location>/general-error.html</location>
</error-page>

That should cover the most common ones.

Benny Code
  • 51,456
  • 28
  • 233
  • 198
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Can you specify a general error page and then override certain error codes with the `` tag? – Qix - MONICA WAS MISTREATED Jan 02 '13 at 12:39
  • 6
    @Tomas: Tomcat guys had the same problem as you. This is nowhere literally mentioned in spec, only figure 14-10 in the spec and the `web.xml` XSD file proves that `` and `` became optional instead of required. See [issue 52135](https://issues.apache.org/bugzilla/show_bug.cgi?id=52135). – BalusC Mar 21 '13 at 10:42
  • http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd specifies no child for the element, so pasting the above code as-is in a Servlet 2.5 web.xml will cause XSD validation errors. If I comment them, though, it works fine, thanks! – László van den Hoek Jul 23 '13 at 13:42
  • 1
    @BalusC: Where should the general-error.html page(mentioned in your answer) placed, inside WEB-INF? I have a weird issue with servlet 2.5 in which for HTTP Error 403, i have created a definition and the error403.jsp page sits inside in the WEB-INF and because of this the error403.jsp page loads when any of the resources are tried to access but images and css within the error403.jsp are not loaded making the page look misplaced. – Jayy Sep 22 '14 at 14:17
  • 1
    @Kaipa: Just use URL-relative paths instead of webcontent-relative paths to CSS/JS/image resources. A lot of starters incorrectly think that those resources are resolved server side, but they are actually resolved client side. See also http://stackoverflow.com/questions/3655316/browser-cant-access-find-relative-resources-like-css-images-and-links-when-cal – BalusC Sep 22 '14 at 15:41
  • Having problems with a location that has a hash in it: /index.html#404 The hash and everything after it is ignored. Does anyone know how to make this work? – Scott Wiedemann Jan 31 '17 at 17:19
  • @ScottWiedemann: this section is for comments. To ask a new question, press [Ask Question] button on right top. – BalusC Jan 31 '17 at 18:01
26

You can also do something like that:

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

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

For error code 403 it will return the page 403.html, and for any other error code it will return the page error.html.

Guy
  • 1,547
  • 1
  • 18
  • 29
11

You can also specify <error-page> for exceptions using <exception-type>, eg below:

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

Or map a error code using <error-code>:

<error-page>
    <error-code>404</error-code>
    <location>/errorpages/404error.html</location>
</error-page>
Aniket Sahrawat
  • 12,410
  • 3
  • 41
  • 67