Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does getHeaderNames returns an Enumeration in HttpServletRequest but a Collection in HttpServletResponse?

HttpServletRequest.getHeaderNames() returns Enumeration<String> and HttpServletResponse.getHeaderNames() returns a Collection<String>.

Why this discrepancy?

like image 473
user454322 Avatar asked Jan 09 '14 09:01

user454322


People also ask

What does request getHeader do?

getHeader. 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.

How to get header in servlet?

The getHeaderNames() of ServletRequest interface returns an Enumeration object, containing all the header names. The getHeader() method of ServletRequest interface returns the header value for the given header name. In this example, we are displaying all the header information of a request in the servlet page.


2 Answers

Enumeration was used in combination with Hashtable and Vector. HttpServletRequest had the getHeaderNames method since jdk 1.2 and has not since been altered.

However HttpServletResponse did not have the getHeaderNames method until jdk 6 and by that time Collection was obviously preferred over the legacy Enumeration.

like image 88
kaputabo Avatar answered Sep 28 '22 04:09

kaputabo


The request method had implemented in java version 1.2. and while collected were comes from jdk 6.

so by the time improvement of java they have used two different things collection and enumeration.

if you see response header values there will be no change in them if we modified collection.

like image 38
Mitul Maheshwari Avatar answered Sep 28 '22 03:09

Mitul Maheshwari