Home  >  Article  >  Java  >  How do I specify a default error page in web.xml for unhandled exceptions?

How do I specify a default error page in web.xml for unhandled exceptions?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-11-01 07:43:31119browse

How do I specify a default error page in web.xml for unhandled exceptions?

How to Specify a Default Error Page in web.xml

In web.xml, the element is used to assign a friendly error page for a specific error like 404. However, what if the user encounters an error beyond the codes specified within ? How do we handle that situation using the element in web.xml?

For Servlet 3.0 and above, the fix is simple:

<code class="xml"><web-app ...>
    <error-page>
        <location>/general-error.html</location>
    </error-page>
</web-app></code>

For Servlet 2.5, where this feature is absent, the solution lies in explicitly defining common HTTP errors that users might encounter:

<code class="xml"><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></code>

This approach ensures that common errors are handled gracefully with custom error pages, providing a better user experience.

The above is the detailed content of How do I specify a default error page in web.xml for unhandled exceptions?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn