Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt about addHeader()

 
Fabian Angy
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi !

I have a problem with addHeader().

In my servlet:


In my JSP:


Results:
myHeader: first value


I don't understand why I have only "first value", where is the "second value" ?
If I do a "response.setHeader("myHeader", "third value") after the addHeader(), the setHeader() overrides the value and it's normal but why addHeader doesn't work ?.
What's wrong with this code ?

Thanks!

Angy.
 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Pie
Posts: 2416
95
Android Chrome Eclipse IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Angy,

There is nothing wrong with your code, but the getHeader() method only gives you the first value.

To see all the values you could use the EL implicit object headerValues (a Map of HttpServletRequest header names and all values), like:
returns the third value of "Accept".

Regards,
Frits
 
Fabian Angy
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My apologies !
Thank you !
 
Fabian Angy
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But.. in that case, if I do a setHeader()/addHeader() on the response, I can't acces to the value with ${headerValues.myHeader[0]}/${headerValues.myHeader[1]}.

Servlet:


JSP:


Results:
myHeader:
myHeader2:


Because the header is in the response and not in the request :/
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that request and response headers are different. What you're setting/adding are response headers, whereas headerValues is part of the request - so they're two completely different sets of headers.

Update: Which, I now see, you just figured out :-)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic