Home >Java >javaTutorial >How to Configure a Default Error Page in web.xml?
Default Error Page Configuration in Web.xml
In an effort to provide a user-friendly experience, you can utilize the
For Servlet 3.0 and above, simply include the following code in your web.xml:
<code class="xml"><web-app ...> <error-page> <location>/general-error.html</location> </error-page> </web-app></code>
However, if you are using Servlet 2.5 or below, you must specify each potential error code individually. Common error codes to consider include:
Here's an example of the necessary web.xml configuration:
<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>
By implementing these configurations, you can ensure that all unspecified errors are directed to a user-friendly default error page, enhancing the user experience and providing a consistent error handling mechanism for your web application.
The above is the detailed content of How to Configure a Default Error Page in web.xml?. For more information, please follow other related articles on the PHP Chinese website!