Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read the parameters and value from the query-string using java?

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.

http://xxxx.com?searchString=%u8BF7%u5728%u6B64%u5904%u8F93%u5165%u4EA7%u54C1%u7F16%u53F7%u6216%u540D%u79F0&button=%E6%90%9C%E7%B4%A2

The other parameter is in proper percentage encoding an I am able to decode using the URL decode. Thanks in advance.

like image 623
thndrkiss Avatar asked Feb 16 '10 09:02

thndrkiss


People also ask

How do you get parameters in Java?

To get all request parameters in java, we get all the request parameter names and store it in an Enumeration object. Our Enumeration object now contains all the parameter names of the request. We then iterate the enumeration and get the value of the request given the parameter name.

What is a query string parameter?

What are query string parameters? Query string parameters are extensions of a website's base Uniform Resource Locator (URL) loaded by a web browser or client application. Originally query strings were used to record the content of an HTML form or web form on a given page.


2 Answers

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");
like image 93
Steven Lizarazo Avatar answered Oct 08 '22 15:10

Steven Lizarazo


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.

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.

like image 27
Michael Borgwardt Avatar answered Oct 08 '22 15:10

Michael Borgwardt