Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 display fine, as long as you use the default ugly Tomcat error pages. However, if you create a custom error page, how do you get that message? I've tried exception.getMessage() or pageContext.getErrorData() but no avail. I've been searching for this for like hours and nobody seems to even wonder about the same thing! :S

I forgot to mention I've only tried it with 404s so far, since that's what I need most... The exception is null for some reason, so trying anything on it throws a NullPointerException. The error page is a 404 error page, set via web.xml (since I want it to be displayed for EVERY single 404 error) and for anyone wondering, yes it has the isErrorPage directive set to true...

like image 972
Lea Verou Avatar asked Jun 15 '09 09:06

Lea Verou


3 Answers

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.

like image 90
Gennady Shumakher Avatar answered Nov 11 '22 10:11

Gennady Shumakher


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

like image 40
jitter Avatar answered Nov 11 '22 10:11

jitter


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.

like image 5
russian eagle Avatar answered Nov 11 '22 11:11

russian eagle