如何使用java从查询字符串中读取参数和值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2271800/
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
How to read the parameters and value from the query-string using java?
提问by thndrkiss
I am using Ciui from google code and all the requests are only GET requests and not POST. The calls are made by the ajax (I am not sure). I need to know how to read the "searchstring" parameter from this URL. When I read this in my Servlet using the getQueryString() method I am not able to properly form the actual text. This unicode (when % replaced by /) like text is actually in Chinese. Please let me how to decode search string and create the string.
我正在使用谷歌代码中的 Ciui,所有请求都只是 GET 请求而不是 POST。调用是由 ajax 发出的(我不确定)。我需要知道如何从此 URL 读取“searchstring”参数。当我使用 getQueryString() 方法在我的 Servlet 中阅读此内容时,我无法正确形成实际文本。这个unicode(当%被/代替)之类的文字其实是中文的。请让我如何解码搜索字符串并创建字符串。
The other parameter is in proper percentage encoding an I am able to decode using the URL decode. Thanks in advance.
另一个参数是适当的百分比编码,我可以使用 URL 解码进行解码。提前致谢。
采纳答案by Michael Borgwardt
Your encoding scheme for those chinese characters actually violates web standards (namely RFC 3986): the percent sign is a reserved character that may not be used except for the standard percent encoding.
您对这些中文字符的编码方案实际上违反了 Web 标准(即RFC 3986):百分号是保留字符,除了标准百分号编码外,不得使用。
I'd strongly advise you to use the standard encoding scheme (UTF-8 bytes and percent encoding); then you can simply use the standard getParameter()
method. If you insist on violating the standard, it may well be impossible to solve your problem within a standards-compliant servlet container.
我强烈建议您使用标准编码方案(UTF-8 字节和百分比编码);那么您可以简单地使用标准getParameter()
方法。如果您坚持违反标准,则很可能无法在符合标准的 servlet 容器中解决您的问题。
回答by Bozho
public void doGet(HttpServletRequest request, HttpServletResponse response) {
String searchString = request.getParameter("searchString");
// process searchString
}
Decoding the parameter is done automatically.
解码参数是自动完成的。
回答by Steven Lizarazo
Ok in my case I had the querystring from another source so you maybe need this:
好的,就我而言,我从另一个来源获得了查询字符串,因此您可能需要这个:
public static Map<String, String> getQueryMap(String query)
{
String[] params = query.split("&");
Map<String, String> map = new HashMap<String, String>();
for (String param : params)
{ String [] p=param.split("=");
String name = p[0];
if(p.length>1) {String value = p[1];
map.put(name, value);
}
}
return map;
}
So then you can use:
那么你可以使用:
Map params=getQueryMap(querystring);
String id=(String)params.get("id");
String anotherparam=(String)params.get("anotherparam");
回答by freedev
A solution with Java 8.
Java 8 的解决方案。
This piece of code transform the query string in a key/value list:
这段代码转换键/值列表中的查询字符串:
List<AbstractMap.SimpleEntry<String, String>> list =
asList(queryString.split("&")).stream()
.map(s -> copyOf(s.split("="), 2))
.map(o -> new AbstractMap.SimpleEntry<String, String>(o[0], o[1]) )
.collect(toList());
the comment of @Tuno inspired me to write a new answer, this alternative (and shorter) solution use groupingBy
method:
@Tuno 的评论启发我写了一个新的答案,这个替代(和更短)的解决方案使用groupingBy
方法:
Map<String, List<String>> llist =
asList(queryString.split("&")).stream()
.map(s -> copyOf(s.split("="), 2))
.collect(groupingBy(s -> s[0], mapping(s -> s[1], toList())));
I suggest to decode the values with URLDecoder.decode(String s, String enc)
我建议用 URLDecoder.decode(String s, String enc)
Both the solutions prints collection content with this line:
这两种解决方案都使用以下行打印集合内容:
list.stream().forEach(s -> System.out.println(s.getKey() + " = " + s.getValue()));