How to specify the default error page in web.xml?

JavaJspServletsweb.xmlCustom Error-Pages

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

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.

Solution 3 - Java

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>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionipkissView Question on Stackoverflow
Solution 1 - JavaBalusCView Answer on Stackoverflow
Solution 2 - JavaGuyView Answer on Stackoverflow
Solution 3 - JavaAniket SahrawatView Answer on Stackoverflow