multiple error-code configuration web.xml

后端 未结 4 799
野趣味
野趣味 2021-01-19 15:05

I\'d like to direct all errors to my Errorsevlet without specifying all the codes explicitly. Is there any way to do like that?


   

        
相关标签:
4条回答
  • 2021-01-19 15:20

    You will need to specify all the desired codes explicitly, a wildcard mechanism is not supported. There are not that many codes, here is a full list.

    To print out the stacktrace (e.g. in a comment, for debugging purposes), you could do something like this:

    <%@ page isErrorPage="true" import="java.io.*"%>
    <body>
    <p>Sorry, there was an error.</p>
    <!-- The full stacktrace follows:-->
    <!-- 
    <%
    if (exception != null) {
        exception.printStackTrace(new PrintWriter(out));
    }
    %> 
    -->
    </body>
    
    0 讨论(0)
  • 2021-01-19 15:26

    If you can upgrade, since Servlet 3.0 it's possible to have a generic error page for all errors, even those not caused by an exception (e.g. 404, 401, etc). Just omit the <error-code> or <exception-type> altogether so that you only have a <location>.

    <error-page>
       <location>/errorServlet</location>
    </error-page>
    

    Note that I replaced the URL to avoid the use of Tomcat's builtin and deprecated InvokerServlet.

    0 讨论(0)
  • 2021-01-19 15:27
    <error-page>
        <exception-type>java.lang.Throwable</exception-type> 
        <location>/servlet/com.ibm.eisa.servlet.ErrorServlet</location> 
    </error-page>
    

    Try this one, all of your errors will be caught(500's) not 404 etc

    0 讨论(0)
  • 2021-01-19 15:31

    I had same concern and after some research I have found out that unfortunately there is no clear requirement to support default error page in Servlet 3.0 specs.

    It's misleading that "error-code" or "exception-type" are optional tags in XSD so we tend to consider that default error page will be the one without "error-code" and without "exception-type" tag.

    Some application servers (e.g. GlassFish) behave as we wish, take default error page, then following the order of specific error pages they override default error page.

    I also tested this on WebLogic 12c and I couldn't get it working as on GlassFish. Below article gives more clues about Tomcat.

    See: bz.apache.org/bugzilla/show_bug.cgi?id=52135

    0 讨论(0)
提交回复
热议问题