View response headers before they are sent to the client?

1.6k views Asked by At

I am writing a project for school. I want to be able to display, on a web page, the response headers that the web server sent to the client. I am able to read request headers from HttpServletRequest and am able to write response headers to HttpServletResponse no problem.

Is there any way to do this? It is possible to make a copy of what the server is about to send?

I am using Eclipse Helios to develop this JSP with POJOs application, and am using Tomcat 5.5 running on Debian Lenny to serve it.

Thanks,

Ean

2

There are 2 answers

1
adarshr On

You probably want to write a servlet filter which can intercept both the request and response before it gets sent.

9
Bozho On

You can use a Filter and an HttpServletResponseWrapper.

Override the three addXHeader(..) methods with something like:

void addHeader(String name, String value) {
   super.addHeader(name, value);
   getWriter().write(name + " : " + value);
}

And then, in a Filter:

chain.doFilter(request, new HeaderHttpServletResponseWrapper(response));

But I would use Firebug to check headers.

Or see this question (the 2nd answer)