[英]Get Query String Values JAVA
I wanted to know if someone can help me with getting the values from a query string. 我想知道是否有人可以帮助我从查询字符串中获取值。
This is the example: partygo://qr/?partyId=XXX&uid=XXX&name=XXX 例如:partygo:// qr /?partyId = XXX&uid = XXX&name = XXX
I want to get the values from partyId, uid and name. 我想从partyId,uid和name获取值。 I really dont know if there is any encoder or sth like that. 我真的不知道是否有任何编码器或类似的东西。
Thanks! 谢谢!
Solution 1: 解决方案1:
By decoding the parameters like below, 通过解码如下参数 ,
public void getParametersFromRequest(HttpServletRequest request, HttpServletResponse response) {
String partyId = request.getParameter("partyId");
String uid= request.getParameter("uid");
String name= request.getParameter("name");
}
Solution 2: 解决方案2:
You can achieve this using below method. 您可以使用以下方法来实现。
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 partyId=(String) params.get("partyId");
String uid=(String) params.get("uid");
String name=(String) params.get("name");
This is simple GET request so you can directly get the value of query string using the request object in doGet method 这是简单的GET请求,因此您可以使用doGet方法中的request对象直接获取查询字符串的值
public void doGet(HttpServletRequest request, HttpServletResponse response) { 公共无效doGet(HttpServletRequest请求,HttpServletResponse响应){
String partyid = request.getParameter("partyId");
//similar do others
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.