Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Hands On Software Engineering with Python this week in the Jython/Python 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

No header can be added to the response  RSS feed

 
Ranch Hand
Posts: 1703
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: 1703
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





 
It will give me the powers of the gods. Not bad for a tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!