How to get the message in a custom error page (Tomcat)?

后端 未结 6 1942
刺人心
刺人心 2020-12-03 02:53

In JSPs, you may use response.sendError(int code, String message) to return a particular error code (eg 404 for not found) and a message as well. These messages

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-03 03:40

    Hmm exception.getMessage() should work

    Try adding exception.getClass().getName()

    1. It could be a NullPointerException which has no message
    2. or the exception is not from Sun and the message isn't set properly

    Of course this only works, if I remember correctly, if the error is thrown by a jsp with <%@ page errorPage="/yourerrorpage.jsp" %> at the top.

    If the error comes from a servlet the exception details are passed as request attributes

    javax.servlet.error.status_code    java.lang.Integer
    javax.servlet.error.exception_type java.lang.Class
    javax.servlet.error.message        java.lang.String
    javax.servlet.error.exception      java.lang.Throwable
    javax.servlet.error.request_uri    java.lang.String
    javax.servlet.error.servlet_name   java.lang.String
    

    Check the Servlet Specification (link is broken since ~2011) section 9.9

提交回复
热议问题