简体   繁体   中英

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

I want to get the values from partyId, uid and name. I really dont know if there is any encoder or sth like that.

Thanks!

Solution 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:

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

public void doGet(HttpServletRequest request, HttpServletResponse response) {

   String partyid = request.getParameter("partyId");
   //similar do others

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM