HttpServletRequest.getParameterValues()
返回一个 String[]
包含给定 HTTP 请求参数的所有值。有谁知道这个数组中值的顺序是否由规范保证与这些值在请求中传递的顺序相同?
例如,如果我有 GET 查询字符串 x=1&x=2&x=3
,我是否保证收到 String[] {"1 ", "2", "3"}
当我调用 getParameterValues()
时?它似乎在实践中有效,但我找不到任何指定必须是这种情况的东西,所以我不愿意依赖它。
最佳答案
ServletRequest (v2.5 javadoc) 的 javadoc 没有提及该方法的值排序。因此,我不会依赖被保留的顺序。
更新:还检查了 2.5 的规范文档,包含以下与 getParameterValues() 相关的信息。它没有提到任何关于查询字符串的排序,所以我认为你看到的行为是实现细节,没有定义为接口(interface)的一部分。
The parameters are stored as a set of name-value pairs. Multiple parameter values can exist for any given parameter name. The following methods of the ServletRequest interface are available to access parameters:
- 获取参数
- 获取参数名称
- 获取参数值
- 获取参数映射
The getParameterValues method returns an array of String objects containing all the parameter values associated with a parameter name. The value returned from the getParameter method must be the first value in the array of String objects returned by getParameterValues. The getParameterMap method returns a java.util.Map of the parameter of the request, which contains names as keys and parameter values as map values.
Java Servlet 规范可以从 Sun, I mean Oracle's website 下载以供将来引用。 .您可以在那里仔细检查您感兴趣的特定 servlet 版本。
关于java - HttpServletRequest.getParameterValues() 中的值排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2317281/