• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

addHeader & setHeader doubt

 
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
 
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.
 
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
 
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().
 
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)
 
There’s no place like 127.0.0.1. But I'll always remember this tiny ad:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic