This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

No header can be added to the response  RSS feed

 
Ranch Hand
Posts: 1701
12
  • 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.
 
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: 1701
12
  • 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
Boost this thread!