6

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.

3
  • Are you using a servlet? Why not do request.getParameter(...) method? (request is of type HttpServletRequest). Commented Feb 16, 2010 at 9:43
  • No. i did not get that parameter when i used request.getParameter(). So I tried using query string :( Commented Feb 16, 2010 at 9:51
  • give the javascript code that is calling the servlet. Commented Feb 16, 2010 at 10:05

4 Answers 4

8

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");
Sign up to request clarification or add additional context in comments.

1 Comment

This solution does not handle the case when there are multiple values associated to a single field (e.g. field1=value1&field1=value2&field1=value3).
2

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.

Comments

2

A solution with 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:

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)

Both the solutions prints collection content with this line:

list.stream().forEach(s -> System.out.println(s.getKey() + " = " + s.getValue()));

2 Comments

Nice one. I have used .collect(Collectors.toMap(o -> o[0], o -> o[1])) to get a Map and retrieve by key.
@Tuno Your solution does not work when there are multiple values associated to a single field (e.g. field1=value1&field1=value2&field1=value3).
1
public void doGet(HttpServletRequest request, HttpServletResponse response) {
    String searchString = request.getParameter("searchString");
    // process searchString
}

Decoding the parameter is done automatically.

1 Comment

What does System.out.println(Collections.list(request.getParameterNames())) say? Is the searchString there? (note that it is case sensitive).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.