HttpServletRequest.getParameterValues()
returns a String[]
containing all values of a given HTTP request parameter. Does anyone know if the order of the values in this array is guaranteed by specification to by the same as the order which those values were passed through in the request?
For example, if I have the GET query string x=1&x=2&x=3
, am I guaranteed to receive the String[] {"1", "2", "3"}
when I call getParameterValues()
? It seems to work in practice, but I can't find anything which specifies that this must be the case, so I'm reluctant to rely on it.
I've got a problem to extract param-value map from HttpServletRequest in order in accordance with elements on the JSP. I wrote an OrderedRequestMap that parsers an application/x-www-form-urlencoded POST request body.
And call it like this
Hope, it helps.
It's indeed not explicitly definied in the Servlet spec, but at least the HTML forms spec definies it explicitly in the application/x-www-form-urlencoded section:
So, that part is safe. Now the servletcontainer, most logically a decent and efficient implementation would process the HTTP input stream immediately as it comes in, so the parameters would be processed in the order as they appear in the request URI (GET) or request body (POST). Collecting them in a
String[]
is the most straightforward choice as it is also used as-is in the Servlet API, so I really don't see any reason to collect it in aHashSet
like structure first, or do aCollections#shuffle()
or whatever and then convert it toString[]
afterwards.I can at least tell from experience, Tomcat does it the right way, so all major containers/appservers which are built on top of Tomcat/Catalina (IBM Websphere, JBoss AS, Sun Glassfish, etc) will also behave so. I only don't have hands on experience with Weblogic, but I would be surprised if it processes it differently (read: less efficiently).
Only the ordering of the parameter names is not guaranteed, logically because it's backed by a
HashMap
.Summarized: the parameters are collected in a
HashMap<String, String[]>
. The names are quaranteed not ordered due to the nature of theHashMap
. The values (one parameter name can have multiple values, e.g.foo=bar1&foo=bar2&foo=bar3
) are in turn however ordered due to the nature ofString[]
, although this is not explicitly specified in the Servlet API.To be on the safe side, you'd like to use a different approach, e.g.
with
The javadoc for ServletRequest (v2.5 javadoc) does not mention anything about the ordering of values for that method. As such, I wouldn't rely on the order being preserved.
Update: also checked the spec document for 2.5, contains the following information relating to getParameterValues(). It does not mention anything about ordering with respect to the query string, so I think the behaviour you are seeing is implementation detail, not defined as part of the interface.
For future reference, the Java Servlet specs can be downloaded from Sun, I mean Oracle's website. You can double check the specific servlet version you're interested in there.
Yes, the order of values returned by
getParamterValues(String)
and entries ingetParameterMap()
is guaranteed by the Servlet Specification. Here's the passage:(This is from the "HTTP Protocol Parameters" section within "The Request" chapter of the Servlet Specifications (SRV.4.1 in version 2.4, SRV.3.1 in version 2.5).)
There doesn't appear to be a clean way to get the names in order (
getParameterNames()
does not return names in the order that the browser gave). I could, I suppose, parse the raw GET string fromgetQueryString()
or parse the raw POST string fromgetInputStream()
, but instead I think I will add another hidden parameter and then usegetParameterValues(String)
to get its order.If you're curious why I want to the parameters in order, it's because I have controls that the user can rearrange using jQuery, and I want to know the order that the user has chosen:
It depends on HttpServletRequest interface implementation.