• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Use of .setHeader() and .addHeader() methods

 
muktesh tripathi
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone please explain me the use of response.setHeader("foo","bar");method and also response.addHeader("foo","bar"); method...

I am interested in knowing their use only..I know the difference between them...
 
Vishal Chugh
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

We set the headers as you know like :
response.setContentType("text/html"); ( convinience method made for us)

which can be set as response.addHeader("Content-Type" , "text/html"); or response.setHeader("Content-Type" , "text/html");

But we have bulk of headers which we can reset as per need , for example :
Content-Length , Status , Cache-Control etc.

You can check the name of setters ( to know name of headers ,out of which some are set by container in response by default like Server from which response is coming , Date , time of response , Connection type of response etc. )in ServletResponse or HttpServletResponse API.

Below links of help :
1

2

3

I hope this may clear your doubt
 
muktesh tripathi
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the information..It helped me a lot...But I still have a silly doubt...
We use addHeader("foo","bar"); to add new header value..

Question is: do we add only value for predefined header set(name,value) ..
If we set a completely new Header value like addHeader("foo","bar"); is that of any practical use?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic