• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

addHeader & setHeader doubt

 
Radmika Arunachalam
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if u have code like this


Will setHeader replace both values of "myHeader" or only last one? if i try request.getHeaders("myHeader") for the above response, what will i get, only "baz" or more than that.. Hope this is clear of what i am trying to ask?

Thanks & Regards,
Radmika
SCJP, SCBCD
 
PNS Subramanian
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is from API documentation


setHeader
public void setHeader(String name,
String value)
Sets a response header with the given name and value. If the header had already been set, the new value overwrites the previous one. The containsHeader method can be used to test for the presence of a header before setting its value.


Hope this helps.
 
Sumeet Raheja
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SET means overriding existing value if exist else add value .
ADD means add value irrespective if name exist or not , if name did not exist creat name and add value to that name , If name Exist add one more value i.e NO overridding
 
Aruneesh Salhotra
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I saw this question last night, and also got confused for this.
addHeader is like adding values to the header with the key.

the moment you call setHeader, it replaces all the values.

Also there is no getHeaders() method, but getHeader().
 
Alexandre Cervieri
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Aruneesh Salhotra:
Also there is no getHeaders() method, but getHeader().


There is a getHeaders(String) method in the javax.servlet.http.HttpServletRequest interface. It returns a Enumeration of String containing all the values associated with the specified header.
 
Aruneesh Salhotra
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I misread the request.getHeaders() as
response.getHeaders().

I guess I assumed the question to be the same as yesterday mock test I took. (question 32)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic