3

I am using Tomcat 6 (but the same should apply to Tomcat 7). Let's say my web.xml contains this definition:

<error-page>
    <error-code>401</error-code>
    <location>/Handle401Error.jsp</location>
</error-page>

Suppose I now return HTTP 401 from some other servlet/JSP:

httpResponse.sendError(SC_UNAUTHORIZED, "This is a message");

How can I access the HTTP response text ("This is a message") in Handle401Error.jsp? The way Tomcat does this, when it shows an error page like this

Tomcat error page

is by using a Valve (ErrorReportValve). Do I need to write a Valve as well?

Edit: the accepted answer below is exactly what I have been looking for, and the assumed duplicate of this question does not mention the same solution.

vektor
  • 3,312
  • 8
  • 41
  • 71
  • Duplicate of http://stackoverflow.com/questions/2748220/how-to-show-user-friendly-error-page-in-browser-when-runtime-exception-is-thrown – André Schild Jul 09 '15 at 10:06
  • No it's not, this has nothing to do with exceptions. I understand how to handle exceptions this way. – vektor Jul 09 '15 at 10:08
  • If you suggest throwing exceptions instead of returning HTTP code + message, yes, that sounds doable, and I might consider it if there is no direct way to do it. But it's still a hack. – vektor Jul 09 '15 at 10:10
  • possible duplicate of [How to get the message in a custom error page (Tomcat)?](http://stackoverflow.com/questions/995248/how-to-get-the-message-in-a-custom-error-page-tomcat) – kevcodez Jul 09 '15 at 10:14
  • @kevcodez yes it seems to be a duplicate. Will investigate the other question, thanks. – vektor Jul 09 '15 at 10:16

1 Answers1

1

Tomcat stores the message string in an internal class org.apache.coyote.Response.

There is no standard way to access the message: From the javadoc of HttpServletResponse#sendError(int,String):

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 and the msg parameter will be ignored.

Poor API design.

As a workaround you could put the error message as attribute into the request, just call response.sendError(401) and in your error page extract the message from the request attributes:

In your code:

HttpServletRequest request = ...
HttpServletResponse response = ...
request.setAttribute("myerrormessage", "This is a message");
response.sendError(401);

In your error jsp page:

Message <%=request.getAttribute("myerrormessage")%>    
wero
  • 32,544
  • 3
  • 59
  • 84
  • Could you please elaborate on how to add an attribute to the response? – vektor Jul 09 '15 at 11:06
  • my fault, there are no attributes in ServletResponse, but only in ServletRequest (edited my answer accordingly) – wero Jul 09 '15 at 11:12
  • Thanks a lot, this works well. I am going to accept your answer, but just that you get more upvotes in the future - would you mind writing up some example code snippets? :) – vektor Jul 09 '15 at 12:36