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.

5
  • Duplicate of stackoverflow.com/questions/2748220/… Commented Jul 9, 2015 at 10:06
  • No it's not, this has nothing to do with exceptions. I understand how to handle exceptions this way. Commented Jul 9, 2015 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. Commented Jul 9, 2015 at 10:10
  • possible duplicate of How to get the message in a custom error page (Tomcat)? Commented Jul 9, 2015 at 10:14
  • @kevcodez yes it seems to be a duplicate. Will investigate the other question, thanks. Commented Jul 9, 2015 at 10:16

1 Answer 1

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")%>    
Sign up to request clarification or add additional context in comments.

3 Comments

Could you please elaborate on how to add an attribute to the response?
my fault, there are no attributes in ServletResponse, but only in ServletRequest (edited my answer accordingly)
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? :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.