• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

addHeader and setHeader Methods

 
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try using the JSP implicit object "headerValues"
 
Narendra Dhande
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
It means our mission is in jeapordy! Quick, read this tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic