Errorpages for your Java-Application
For a default Java Web-Application there are no error pages defined. During development this has the advantage, that you can see the error message and the complete stack trace. So you can find the source of the error. In our final version we don’t want to display this information, but we need to display a message that an error occurred.
Define error pages
Error pages can be created in your web.xml File. This can be found in your WEB-INF folder. This code defines some error pages for some different kinds of error sources:
<error-page> <error-code>401</error-code> <location>/WEB-INF/error/401.html</location> </error-page> <error-page> <error-code>403</error-code> <location>/WEB-INF/error/403.html</location> </error-page> <error-page> <error-code>404</error-code> <location>/WEB-INF/error/404.html</location> </error-page> <error-page> <error-code>500</error-code> <location>/WEB-INF/error/500.html</location> </error-page> <!-- catch anything that is not already catched --> <error-page> <exception-type>java.lang.Throwable</exception-type> <location>/WEB-INF/error/undefined.html</location> </error-page>
First we define error pages for different kinds of error codes. For this I have created HTML error pages. There files are in a subdirectory of WEB-INF. The last error page is very important. If an error occurred that was not covered with an error page, than we want to display a default error page. If we forget this, it can happen, that an unknown error displays error code and a stack trace.