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

我们两清 提交于 2019-11-27 13:09:04
Gennady Shumakher

The error message is available via javax.servlet.error.message attribute of the request object in error page jsp.

Here is the jsp syntax to access it:

<c:out value="${requestScope['javax.servlet.error.message']}"/>

You could look for other error related information available in the error page here under New Error Attributes.

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 section 9.9

I'm sorry for answering so late, but I faced with this problem just a week ago, I've browsed a lot of different sites but nobody really aswered this problem the way I wanted to hear. In this post I found out a few interesting solutions and then came up to my own. Just include this source in your page:

<%
    out.println(pageContext.getErrorData().getRequestURI());
    out.println("<br/>");
    out.println(pageContext.getErrorData().getStatusCode());
    out.println("<br/>");
    out.println(pageContext.getException());
    out.println("<br/>");
%>

It worked perfectly fine with me.

The exception part of the ErrorData will only be set if the error page was loaded as a result of an exception and not a response error code.

See the javadoc for sendError on HttpServletResponse. It mentions why you're not seeing the message you passed to sendError (emphasis mine):

Sends an error response to the client using the specified status. The server defaults to creating the response to look like an HTML-formatted server error page containing the specified message, setting the content type to "text/html", leaving cookies and other headers unmodified. If an error-page declaration has been made for the web application corresponding to the status code passed in, it will be served back in preference to the suggested msg parameter.

If the response has already been committed, this method throws an IllegalStateException. After using this method, the response should be considered to be committed and should not be written to.

Georgy Bolyuba

The thing you want looks weird to me :). That said, I would do the following:

  1. Implement HttpResponseWrapper to wrap any other HttpResponse in this way:

    public class HttpResponseWrapper implements HttpResponse {
        private String errorMessage;
    ...
    
        @Override
        public void sendError(...) {
            <save error message here>
        }
    ...
    }
    
  2. Create a Filter and wrap any response in this

  3. Put filter on all requests and first in the chain

  4. In your error page check if response is instanceof HttpResponseWrapper

  5. Get your message

You can use

${requestScope['javax.servlet.error.message']}

if you don't have jstl on the page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!