How can I get the HTTP status code out of a ServletResponse in a ServletFilter?

Viewed 88343

I'm trying to report on every HTTP status code returned from my webapp. However the status code does not appear to be accessible via the ServletResponse, or even if I cast it to a HttpServletResponse. Is there a way to get access to this value within a ServletFilter?

7 Answers

Also need to include a wrapper for #sendRedirect, and it would be better to initialize status to '200' rather than '0'

private int httpStatus = SC_OK;

...

@Override
public void sendRedirect(String location) throws IOException {
    httpStatus = SC_MOVED_TEMPORARILY;
    super.sendRedirect(location);
}
Related