This week's giveaway is in the Programmer Certification (OCPJP) forum.
We're giving away four copies of Java Mock Exams (software) and have David Mayer on-line!
See this thread for details.
Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • 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
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!