0

I have an html form which posts data to a servlet. However the order returned in getParameterNames() isn't the same as in the HTML Form.

How do i retrieve the parameters in the same order?

Kanishk Dudeja
  • 1,201
  • 3
  • 17
  • 33

2 Answers2

1

The parameters in the request are stored in a Map. A map do not warranty the order of iteration. If you really need to get the records in a particular order I suggest to add the order in the name property something like 01_firstName 02_last name and then order the map.

1

This came up as a bug in Apache Tomcat a little while ago. The short version of that bug report is:

  • Section 17.13.3 of the HTML specification (v4.01) titled "Processing form data", clearly states that "control names/values are listed in the order they appear in the document."
  • The Servlet specification does defer to W3C for all HTML matters.
  • Strictly, 17.13.3 applies to how the client presents the data to the server
  • That said, it seems reasonable to expect Servlet containers to retain this order when presenting parameters.

This has been fixed in Tomcat 8.0.x for 8.0.0-RC4 onwards and in 7.0.x for 7.0.45 onwards.

Mark Thomas
  • 16,339
  • 1
  • 39
  • 60