java HttpServletRequest getParameter 无法使用 & 检索参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11045916/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 03:34:42  来源:igfitidea点击:

HttpServletRequest getParameter unable to retrieve parameters with &

javajspservletsstrutshttprequest

提问by TJ-

I have a url, something like this localhost:8080/foo.action?param1=7&param2=8&param3=6

我有一个网址,像这样 localhost:8080/foo.action?param1=7&param2=8&param3=6

When this is the Url (as it is), request.getParmeter("param2")gives me 8[Correct]

当这是网址时(原样),request.getParmeter("param2")给我8[正确]

i) When the encoding converts this url to localhost:8080/foo.action?param1=7%26param2=8%26param3=6

i) 当编码将此 url 转换为 localhost:8080/foo.action?param1=7%26param2=8%26param3=6

In this case, request.getParameter("param1")gives me 7&param2=8&param3=6

在这种情况下,request.getParameter("param1")给我7&param2=8&param3=6

ii) When the encoding converts this url to localhost:8080/foo.action?param1=7&param2=8&param3=6

ii) 当编码将此 url 转换为 localhost:8080/foo.action?param1=7&param2=8&param3=6

In this case, request.getParameter("param1")gives me 7and request.getParameter("param2")gives me null

在这种情况下,request.getParameter("param1")给我7request.getParameter("param2")给我 null

What is the correct way of retrieving the parameters? [Assuming that using one of the two Url encoding schemes is unavoidable]

检索参数的正确方法是什么?【假设使用两种Url编码方案中的一种是不可避免的】

(I am using struts actions)

(我正在使用 struts 操作)

采纳答案by Yudong Li

You can call req.getQueryString()to get the whole query parameters and then do server side decoding based on whatever encoding methods you choose.

您可以调用req.getQueryString()以获取整个查询参数,然后根据您选择的任何编码方法进行服务器端解码。

回答by alexey28

To prevent this do not encode parameters with delimeters, encode only parameters values. This will be the best way. If you cannot handle parameters encoding just do decoding on server side before parsing:

为了防止这种情况,不要使用分隔符对参数进行编码,只对参数值进行编码。这将是最好的方法。如果您无法处理参数编码,请在解析前在服务器端进行解码:

String queryString = request.getQueryString();
String decoded = URLDecoder.decode(queryString, "UTF-8");
String[] pares = decoded.split("&");
Map<String, String> parameters = new HashMap<String, String>();
for(String pare : pares) {
    String[] nameAndValue = pare.split("=");
    parameters.put(nameAndValue[0], nameAndValue[1]);
}

// Now you can get your parameter:
String valueOfParam2 = parameters.get("param2");

回答by Ankit

Try using

尝试使用

String[] Parameters = = URLDecoder.decode(Request.getQueryString(), 'UTF-8').splitat('&') ;

Hope this helps.

希望这可以帮助。

回答by John

I had this happen to me today. Turns out I was passing the encoded url over the wire. When the request is made it should be made as http://localhost/foo?bar=1&bat=2not as http://localhost/foo?bar=1&amp;bat=2.

我今天遇到了这种情况。原来我是通过网络传递编码的 url。当提出请求时,它应该http://localhost/foo?bar=1&bat=2http://localhost/foo?bar=1&amp;bat=2.

In this case I had cut the url from an xml file and pasted it into a browser.

在这种情况下,我从 xml 文件中剪切了 url 并将其粘贴到浏览器中。