java 用Java Servlet解析关联数组请求参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6967258/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Associative array request parameter parsing with Java Servlet
提问by MetaChrome
Is it possible to parse the string keys from a request such as this, with the Java servlet API?
是否可以使用 Java servlet API 从诸如此类的请求中解析字符串键?
http://localhost:8080/?assocArray[key1]=value1&assocArray[key2]=value2&assocArray[key3]=value3
getParameterValues("assocArray") returns ["value3","value1","value1"]
The ordering of the values in the returned array is not order of the keys (not that it matters)
返回数组中值的顺序不是键的顺序(并不重要)
SOLVED: It is possible, the keys are interpreted as simple global key strings. Java doesn't recognize them as an array. Use regex
已解决:有可能将键解释为简单的全局键字符串。Java 不会将它们识别为数组。使用正则表达式
回答by JB Nizet
You may have several values for the same parameter name:
对于相同的参数名称,您可能有多个值:
http://localhost:8080/?param1=value1¶m1=value2¶m1=value3
In this case, getParameterValues("param1")
will return a String[]
containing 3 elements : "value1"
, "value2"
and "value3"
.
在这种情况下,getParameterValues("param1")
将返回一个String[]
包含 3 个元素的 : "value1"
,"value2"
和"value3"
。
In the example you gave, you defined 3 different parameters : assocArray[key1]
, assocArray[key2]
and assocArray[key3]
. The servlet API will consider them as 3 totally different parameters having nothing in common. You thus have to call getParameter("assocArray[key1]")
to get "value1"
, getParameter("assocArray[key2]")
to get "value2"
, etc.
在您给出的示例中,您定义了 3 个不同的参数:assocArray[key1]
,assocArray[key2]
和assocArray[key3]
. servlet API 会将它们视为 3 个完全不同的参数,没有任何共同之处。因此,您必须调用getParameter("assocArray[key1]")
get "value1"
、getParameter("assocArray[key2]")
get"value2"
等。
回答by BalusC
Not directly. The []
have no special meaning in HTTP request parameters and are by the Servlet API not recognized as array keys (you was perhaps a PHP programmer which indeed has a proprietary parser for this?). You need to parse and collect it yourselfin a loop.
不直接。将[]
在HTTP请求的参数没有特殊的含义,是由不被识别为数组键了Servlet API(你或许这的确有这个专有解析器PHP程序员?)。您需要自己在循环中解析和收集它。
For example,
例如,
Map<String, String> assocArray = new LinkedHashMap<String, String>();
for (Entry<String, String[]> entry : request.getParameterMap().entrySet()) {
String name = entry.getKey();
if (name.startsWith("assocArray[")) {
String key = name.substring(name.indexOf('[') + 1, name.indexOf(']'));
assocArray.put(key, entry.getValue()[0]);
}
}
回答by Bozho
Not possible, AFAIK. The http protocol specifies key & value only, no key[key]=value
不可能,AFAIK。http 协议仅指定键和值,没有key[key]=value
Use the string array by index - values[0]
, values[1]
, etc.
通过索引使用字符串数组- values[0]
,values[1]
等等。
You can make some preprocessing - use request.getParameterMap(..)
, parse the foo[bar]
syntax yourself, and put it in a Map<String, String>
您可以进行一些预处理 - 使用request.getParameterMap(..)
,foo[bar]
自己解析语法,然后将其放入Map<String, String>
回答by Stephen C
SOLVED: It is possible, the keys are interpreted as simple global key strings. Java doesn't recognize them as an array. Use regex
已解决:有可能将键解释为简单的全局键字符串。Java 不会将它们识别为数组。使用正则表达式
Java doesn't recognize them as (elements of) an array because the URL / URI specs don't say that they represent an array. You shouldn't expectJava (or other) libraries to implement custom syntaxes that you've essentially pulled out of the air.
Java 不会将它们识别为数组(的元素),因为 URL / URI 规范没有说它们代表数组。您不应该期望Java(或其他)库来实现您基本上从空气中提取的自定义语法。
Using regexes for parsing input is generally speaking a bad idea. For (complete) URLs it is a bad idea because there are a variety of tricky way that URLs can be encoded, and it it hard for a regex to take account of this.
使用正则表达式解析输入通常是一个坏主意。对于(完整的)URL,这是一个坏主意,因为可以通过多种棘手的方式对 URL 进行编码,并且正则表达式很难考虑到这一点。
(If you are using a regex to split apart a single request parameter, you should be OK because the Servlet infrastructure will have already dealt with the primary URL parsing and dealt with %-encoding, etc.)
(如果您使用正则表达式来拆分单个请求参数,则应该没问题,因为 Servlet 基础结构已经处理了主要的 URL 解析并处理了 %-encoding 等。)
I'd also like to point out that your query String syntax is fragile. You are using the '[' and ']' characters without escaping them. According to the URL / URI specs they are "reserved characters" and should be escaped when used as "data". You will probably get away with it in most contexts, but applications that require strictly conformant URIs will reject your URLs.
我还想指出您的查询字符串语法很脆弱。您正在使用 '[' 和 ']' 字符而没有转义它们。根据 URL / URI 规范,它们是“保留字符”,在用作“数据”时应进行转义。在大多数情况下,您可能会侥幸逃脱,但需要严格符合 URI 的应用程序将拒绝您的 URL。