Ordering of values in HttpServletRequest.getParameterValues()

后端 未结 5 1670
日久生厌
日久生厌 2020-12-03 01:31

HttpServletRequest.getParameterValues() returns a String[] containing all values of a given HTTP request parameter. Does anyone know if the order o

5条回答
  •  一生所求
    2020-12-03 01:56

    Yes, the order of values returned by getParamterValues(String) and entries in getParameterMap() is guaranteed by the Servlet Specification. Here's the passage:

    Data from the query string and the post body are aggregated into the request parameter set. Query string data is presented before post body data. For example, if a request is made with a query string of a=hello and a post body of a=goodbye&a=world, the resulting parameter set would be ordered a=(hello, goodbye, world).

    (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 from getQueryString() or parse the raw POST string from getInputStream(), but instead I think I will add another hidden parameter and then use getParameterValues(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:

    • hello
    • world

提交回复
热议问题