Help coderanch get a
new server
by contributing to the fundraiser
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

getHeader and getHeaders methods

Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello group:

Here's a point that confused me a bit, but I think I understand now. HttpServletRequest.getHeader(String n) returns the first header of name "n" and HttpServletRequest.getHeaders(String n) returns all the headers of name "n" as an Enumeration.

What confused me was that I thought "multiple values" meant something like:

accept: text/html, image/gif

value1 = text/html
value2 = image/gif

But when I tried this out I got the whole string "text/html, image/gif" when calling getHeader("accept"). OK, so I figure that "multiple values" would then be:

accept: text/html
accept: image/gif

Now, my question is: when does the browser send its request in this form?
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

From getHeader of HttpServletRequest 1.4 API:

Returns the value of the specified request header as a String. If the request did not include a header of the specified name, this method returns null. If there are multiple headers with the same name, this method returns the first head in the request. The header name is case insensitive. You can use this method with any request header.

However the HTTP 1.1 RFC says this (and the 1.0 is similar):

Multiple message-header fields with the same field-name MAY be present in a message if and only if the entire field-value for that header field is defined as a comma-separated list [i.e., #(values)]. It MUST be possible to combine the multiple header fields into one "field-name: field-value" pair, without changing the semantics of the message, by appending each subsequent field-value to the first, each separated by a comma. The order in which header fields with the same field-name are received is therefore significant to the interpretation of the combined field value, and thus a proxy MUST NOT change the order of these field values when a message is forwarded.

My guess is that once upon a time there was some browser or other application that didn't play by HTTP rules and Java had to provide a way around it. From the looks of it, there's no good reason why anyone should ever use getHeaders... unless they miraculously find the app that submits HTTP headers that don't follow the rules.
No, tomorrow we rule the world! With this tiny ad:
We need your help - Coderanch server fundraiser
    Bookmark Topic Watch Topic
  • New Topic