Home > Java > javaTutorial > 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
Release: 2024-11-01 07:43:31
Original
224 people have browsed it

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>
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template