[ACCEPTED]-How to get the message in a custom error page (Tomcat)?-custom-error-pages

Accepted answer
Score: 13

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

Here 3 is the jsp syntax to access it:

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

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

Score: 9

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 6 only works, if I remember correctly, if 5 the error is thrown by a jsp with <%@ page errorPage="/yourerrorpage.jsp" %> at the 4 top.

If the error comes from a servlet the 3 exception details are passed as request 2 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 1 ~2011) section 9.9

Score: 5

I'm sorry for answering so late, but I faced 7 with this problem just a week ago, I've 6 browsed a lot of different sites but nobody 5 really aswered this problem the way I wanted 4 to hear. In this post I found out a few 3 interesting solutions and then came up to 2 my own. Just include this source in your 1 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.

Score: 0

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

  1. Implement 5 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 4 Filter and wrap any response in this

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

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

  5. Get your message

Score: 0

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

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

Sends an error response to the client 11 using the specified status. The server defaults 10 to creating the response to look like an 9 HTML-formatted server error page containing 8 the specified message, setting the content 7 type to "text/html", leaving cookies 6 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 5 response has already been committed, this 4 method throws an IllegalStateException. After 3 using this method, the response should be 2 considered to be committed and should not 1 be written to.

Score: 0

You can use

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

if you don't have jstl on the 1 page

More Related questions