1

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

Gareve
  • 3,372
  • 2
  • 21
  • 23
ean
  • 41
  • 1
  • 4
  • Can you explain why you need to know the headers before they are sended? – Gareve Mar 06 '11 at 23:10
  • Ok, this sounds good. One (hopefully minor) problem remains: the doFilter method defines a ServletResponse res as follows: `code` public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) `/code` ...in which, I cast the ServletResponse as an HttpServletResponse so that I can access methods like getHeader(), etc.: `code` HttpServletResponse response =(HttpServletResponse) res; `/code` ...but, I still only have access to the methods from ServletResponse, not those from HttpServletResponse. Any thoughts as to why? Ean – ean Mar 06 '11 at 23:25
  • just cast to `HttpServletResponse`. – Bozho Mar 07 '11 at 13:00

2 Answers2

2

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)

Community
  • 1
  • 1
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • @Bozho: where are the three addXHeaer methods? So far I just have a filter with doFilter, init, and destroy. Thanks! – ean Mar 06 '11 at 23:36
  • In your `HttpServletResponseWrapper` implementation. – BalusC Mar 07 '11 at 02:29
  • @Bozho: looking at this a bit more, I'm not sure this is quite what I'm asking. What I want to do is read the response headers, not write to them. So, wherever addXHeader() is, I wouldn't be using it anyway. Regarding Firebug, normally that is exactly what I would use as well, but the point of this exercise is to show that data on the web page. thanks again. – ean Mar 07 '11 at 04:16
  • @ean - you can't read them, you can only intercept when they are being written. – Bozho Mar 07 '11 at 08:31
  • Just append them to some list instead which you expose by a public getter in your `HttpServletResponseWrapper` implementation which you **can** cast back later :) – BalusC Mar 07 '11 at 12:50
  • Firstly, let me say thanks for the suggestions, I now have a filter that writes out every time a response header is written. Unfortunately, this filter only "sees" the headers that I add, e.g. `code` response.addHeader("test","123"); `code`. It doesn't catch all the ones that the server writes, i.e. Date, Server, Content-Length, Content-Type, Set-Cookie, Cache-Control, etc. Is there a way to push the "order" of this filter so that it can see these headers, or does Tomcat just write those too late for me to access them? – ean Mar 07 '11 at 16:35
  • @ean - isn't a client-side solution an option? – Bozho Mar 07 '11 at 20:41
  • Yes it is an option, esp since this approach seems to have hit a dead end. :) I suppose I could write a java applet that would load in the page? What would you suggest? – ean Mar 08 '11 at 04:28
  • @ean http://stackoverflow.com/questions/220231/accessing-http-headers-in-javascript (the 2nd answer) – Bozho Mar 08 '11 at 07:11
0

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

adarshr
  • 61,315
  • 23
  • 138
  • 167
  • If @ean wants for educational purpouse and hasty, he can also use a simple sniffer as ngrep. – Gareve Mar 06 '11 at 23:19