How to read the parameters and value from the query-string using java?
Solution 1
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.
Solution 2
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");
Solution 3
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()));
Solution 4
public void doGet(HttpServletRequest request, HttpServletResponse response) {
String searchString = request.getParameter("searchString");
// process searchString
}
Decoding the parameter is done automatically.
thndrkiss
am a beginner ! "Time is a companion that goes with us on a journey. It reminds us to cherish each moment, because it will never come again. What we leave behind is not as important as how we have lived." - Jean Luc Picard
Updated on November 15, 2020Comments
-
thndrkiss over 3 years
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.
The other parameter is in proper percentage encoding an I am able to decode using the URL decode. Thanks in advance.
-
BalusC about 14 yearsWhat does
System.out.println(Collections.list(request.getParameterNames()))
say? Is thesearchString
there? (note that it is case sensitive). -
Tuno almost 8 yearsNice one. I have used
.collect(Collectors.toMap(o -> o[0], o -> o[1]))
to get a Map and retrieve by key. -
freedev almost 8 years@Tuno Your solution does not work when there are multiple values associated to a single field (e.g.
field1=value1&field1=value2&field1=value3
). -
freedev almost 8 yearsThis solution does not handle the case when there are multiple values associated to a single field (e.g. field1=value1&field1=value2&field1=value3).