View response headers before they are sent to the

2019-07-27 18:47发布

问题:

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

回答1:

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)



回答2:

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