• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

addHeader and setHeader Methods

 
Narendra Dhande
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

The addHeader(..) method in HttpServletResponse is used to set muliple headers. The setHeader(..) is used to set the Header in the response. The setHeader(..) method, if the header already has been set, the new value overwirites the previous one.

Suppose we had set multiple values to a header using addHeader method and then execute setHeader method to set the new value, which value is replaced ?

Thanks
 
Sai Patnala
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lets just say:
response.addHeader("abc", "value1");
response.addHeader("abc", "value2");

At this moment, "abc" header has values [value1, value2].
Now if you do response.setHeader("abc", "newValue");

then header "abc" becomes "newValue".

Hope this helps.
 
Vrunda Joshi
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as per servlet specs

the setHeader() sets a header with given name & value,previous header is replaced with new header.
Where a set of header values exist for the name,the values are cleared and replaced with the new value
so does this mean that the set of [value1,value2] wud be replaced by newvalue?? and header remains the same since the names are same?

thanks
-vrunda
 
Sai Patnala
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is right. [value1, value2] get replaced with newvalue. So even if some one makes a call like getHeaderValues("abc"), you will get [newvalue].
 
Narendra Dhande
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

There is no getHeaderValues("abc") to check the values of headers for response object. I am also thinking that the setHeader replace all the previous values, but I want to check it. Is there is any way to observe the response headers at server/client side.

Thanks
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try using the JSP implicit object "headerValues"
 
Narendra Dhande
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

headerValues -- A map that maps header names to a String[] of all values for that header (obtained by calling ServletRequest.getHeaders(String name)).

So, it is not related to response headers. It gives values of request headers. We can not set the request headers. I am interested in the headers set by me, in response.


Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic