Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

No header can be added to the response

 
Himai Minh
Ranch Hand
Posts: 1361
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a servlet and try to add header to the response:

The size of the header is 0.
I even try , and I also get size of the response header is 0.
 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adding Response headers can be tricky sometimes.

Basically:
1) Make sure nothing else before yourself has already written to the OutputStream. It is impossible to set headers once the stream has already been written to. Similarly, have a look at what writes at the stream after you if anything.

2) The actual HttpServletResponse implementation is provided by your servlet engine. You can find the class name of the implementation and then look at the source if available by doing:
System.out.println(response.getClass().getName()); or something similar.

3) The getHeaderNames() specification in HttpServletResponse:


java.util.Collection<java.lang.String> getHeaderNames()

Gets the names of the headers of this response.

This method considers only response headers set or added via setHeader(java.lang.String, java.lang.String), addHeader(java.lang.String, java.lang.String), setDateHeader(java.lang.String, long), addDateHeader(java.lang.String, long), setIntHeader(java.lang.String, int), or addIntHeader(java.lang.String, int), respectively.

Any changes to the returned Collection must not affect this HttpServletResponse.

Returns:
a (possibly empty) Collection of the names of the headers of this response
Since:
Servlet 3.0

I put the interesting parts above in bold. I remember something similar to what you are experiencing.

Just try to get your header with getHeader(name) or other methods to see what happens.

Even if you can't read back your header with available methods, debug what is sent to the browser and you might be surprised to find out that your header is there after all.
 
Himai Minh
Ranch Hand
Posts: 1361
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, AJ. Thanks for your response.
I tried this:

The response.getClass().getName() returns this : org.apache.catalina.connector.ResponseFacade
But the getHeader still returns : null.


Based on this
Any changes to the returned Collection must not affect this HttpServletResponse.
, does it mean the ResponseFacade reference is not the same as response reference?
That may be why response.getHeader returns null ?
 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Himai Minh wrote:Hi, AJ. Thanks for your response.
I
The response.getClass().getName() returns this : org.apache.catalina.connector.ResponseFacade
But the getHeader still returns : null.


Based on this
Any changes to the returned Collection must not affect this HttpServletResponse.
, does it mean the ResponseFacade reference is not the same as response reference?
That may be why response.getHeader returns null ?


It could be, I have seen award implementation in Servlet engines regarding headers. Try telnetting to the port of your servlet engine to see if your header is there, it might be.
Here is example to show the headers sent by my servlet engine, the port number is 8093, press enter twice after typing "host: 192.168.1.1" just adjust for the correct on the path GET line,
example "GET /yourapp/yoursetheaderservletURL":

You may also download tomcat source code and start debugging at org.apache.catalina.connector.ResponseFacade.java





 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic