javaerror-handlingweb.xml

How to specify common error page excluding 404 and 500 in web.xml



I already have two web pages designed for 404 and 500 error pages in web.xml

 <error-page>
        <error-code>404</error-code>
        <location>/WEB-INF/error/404.jsp</location>
    </error-page>
    <error-page>
        <error-code>500</error-code>
        <location>/WEB-INF/error/500.jsp</location>
    </error-page>

I want to redirect to a predesignated page for all the other errors.
Please help. Thank you


Solution

  • This should be sufficient, along with few customer error related pages, you can add default page. So all error other than 404, 500 , will be redirected to generic page error.html

    <error-page>
        <location>/error.html</location>
    </error-page>
    

    or more customized, using Exception class, can be your custom exception class as well.

    <error-page>
      <exception-type>java.lang.Throwable</exception-type>
      <location>/error.html</location>
    </error-page>
    

    So code becomes,

    <error-page>
        <error-code>404</error-code>
        <location>/WEB-INF/error/404.jsp</location>
    </error-page>
    <error-page>
        <error-code>500</error-code>
        <location>/WEB-INF/error/500.jsp</location>
    </error-page>
    <error-page>
        <location>/error.html</location>
    </error-page>