File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes addHeader & setHeader doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "addHeader & setHeader doubt" Watch "addHeader & setHeader doubt" New topic

addHeader & setHeader doubt

Radmika Arunachalam
Ranch Hand

Joined: Mar 29, 2004
Posts: 45
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,
PNS Subramanian
Ranch Hand

Joined: Jul 13, 2004
Posts: 150
This is from API documentation

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

Joined: Dec 01, 2004
Posts: 16
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

Joined: May 12, 2004
Posts: 167
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

Joined: Aug 02, 2005
Posts: 27
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

Joined: May 12, 2004
Posts: 167
I misread the request.getHeaders() as

I guess I assumed the question to be the same as yesterday mock test I took. (question 32)
I agree. Here's the link:
subject: addHeader & setHeader doubt
It's not a secret anymore!