Home > Java > javaTutorial > How to Set a Default Error Page in web.xml?

How to Set a Default Error Page in web.xml?

Mary-Kate Olsen
Release: 2024-11-01 10:33:02
Original
575 people have browsed it

How to Set a Default Error Page in web.xml?

How to Set a Default Error Page in web.xml

In web.xml, you can use the element to handle errors and redirect users to a custom error page. To set a default error page for errors that are not explicitly handled, there are two ways depending on your Servlet version:

Servlet 3.0 or Newer

If you're using Servlet 3.0 or newer, you can simply add the following element to your web.xml:

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

Servlet 2.5

For Servlet 2.5, you need to specify each common HTTP error individually:

<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

To summarize, for Servlet 3.0 or newer, you can set a default error page directly. For Servlet 2.5, you need to manually handle each common HTTP error using separate elements.

The above is the detailed content of How to Set a Default Error Page in web.xml?. 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