98

In HttpServletRequest, getParameterMap returns a Map of all query string parameters and post data parameters.

Is there a way to get a Map of ONLY query string parameters? I'm trying to avoid using getQueryString and parsing out the values.

RMT
  • 7,040
  • 4
  • 25
  • 37
JasonStoltz
  • 3,040
  • 4
  • 27
  • 37

6 Answers6

121

You can use request.getQueryString(),if the query string is like

username=james&password=pwd

To get name you can do this

request.getParameter("username"); 
coastline
  • 1,722
  • 2
  • 12
  • 17
26

Contrary to what cularis said there can be both in the parameter map.

The best way I see is to proxy the parameterMap and for each parameter retrieval check if queryString contains "&?<parameterName>=".

Note that parameterName needs to be URL encoded before this check can be made, as Qerub pointed out.

That saves you the parsing and still gives you only URL parameters.

DoubleMalt
  • 1,263
  • 12
  • 17
  • 2
    Note that `parameterName` needs to be URL encoded before this check can be made. – qerub May 31 '13 at 13:01
  • 17
    The query string never contains `?`. That's the separator character between URI and query string. – BalusC Jun 01 '13 at 14:14
  • Hi @DoubleMalt, I have the same requirement, but can't get the solution you provided. Can you provide example code? – Vishal Zanzrukia Dec 17 '14 at 10:42
  • @VishalZanzrukia: What is your code that doesn't work? – DoubleMalt Dec 18 '14 at 11:22
  • 1
    I like the idea, but there is not always a "=" character in the query string. If parameter value is empty there is only in the query string. I personally think splitting by "&" and "=" would be a better solution. – Bren Aug 10 '15 at 19:51
25

The servlet API lacks this feature because it was created in a time when many believed that the query string and the message body was just two different ways of sending parameters, not realizing that the purposes of the parameters are fundamentally different.

The query string parameters ?foo=bar are a part of the URL because they are involved in identifying a resource (which could be a collection of many resources), like "all persons aged 42":

GET /persons?age=42

The message body parameters in POST or PUT are there to express a modification to the target resource(s). Fx setting a value to the attribute "hair":

PUT /persons?age=42

hair=grey

So it is definitely RESTful to use both query parameters and body parameters at the same time, separated so that you can use them for different purposes. The feature is definitely missing in the Java servlet API.

Bennik2000
  • 1,112
  • 11
  • 25
Ola Berg
  • 251
  • 3
  • 2
17

As the other answers state there is no way getting query string parameters using servlet api.

So, I think the best way to get query parameters is parsing the query string yourself. ( It is more complicated iterating over parameters and checking if query string contains the parameter)

I wrote below code to get query string parameters. Using apache StringUtils and ArrayUtils which supports CSV separated query param values as well.

Example: username=james&username=smith&password=pwd1,pwd2 will return

password : [pwd1, pwd2] (length = 2)

username : [james, smith] (length = 2)

public static Map<String, String[]> getQueryParameters(HttpServletRequest request) throws UnsupportedEncodingException {
    Map<String, String[]> queryParameters = new HashMap<>();
    String queryString = request.getQueryString();
    if (StringUtils.isNotEmpty(queryString)) {
        queryString = URLDecoder.decode(queryString, StandardCharsets.UTF_8.toString());
        String[] parameters = queryString.split("&");
        for (String parameter : parameters) {
            String[] keyValuePair = parameter.split("=");
            String[] values = queryParameters.get(keyValuePair[0]);
            //length is one if no value is available.
            values = keyValuePair.length == 1 ? ArrayUtils.add(values, "") :
                    ArrayUtils.addAll(values, keyValuePair[1].split(",")); //handles CSV separated query param values.
            queryParameters.put(keyValuePair[0], values);
        }
    }
    return queryParameters;
}
Niv
  • 271
  • 1
  • 7
  • 16
Bren
  • 2,148
  • 1
  • 27
  • 45
  • I consider this much better that accepted answer, don't know why to avoid parsing query string because creating proxy is not better solution – Pavel Niedoba Feb 12 '17 at 22:36
  • 1
    I am just wondering,if any of the query parameters contains `&` character in its value, whether it will give you the correct result – amdg May 03 '18 at 07:06
  • 1
    @amdg - that character would be URL encoded, so that code is good. If it weren't encoded, the string would be unparsable. – marathon Aug 17 '18 at 22:08
  • Consider using `URLDecoder.decode(keyValuePair[0])` (or `[1]`) as well: https://docs.oracle.com/javase/6/docs/api/java/net/URLDecoder.html – Pluto Feb 28 '20 at 21:28
  • Also this solution can be combined with one of these to do the opposite (if using x-www-form-urlencoded): https://stackoverflow.com/questions/8100634/get-the-post-request-body-from-httpservletrequest – Pluto Feb 28 '20 at 21:43
  • 1
    query params with csv separated values isn't being considered here. for ex: `username=james&username=smith&password=pwd1,pwd2` will have a map with 2 values for username (which is correct) but only 1 value for password (which is incorrect) – Niv Jun 17 '20 at 21:51
8

Java 8

return Collections.list(httpServletRequest.getParameterNames())
                  .stream()
                  .collect(Collectors.toMap(parameterName -> parameterName, httpServletRequest::getParameterValues));
pushkin
  • 9,575
  • 15
  • 51
  • 95
Giani Segatto
  • 99
  • 1
  • 3
2

I am afraid there is no way to get the query string parameters parsed separately from the post parameters. BTW the fact that such API absent may mean that probably you should check your design. Why are you using query string when sending POST? If you really want to send more data into URL use REST-like convention, e.g. instead of sending

http://mycompany.com/myapp/myservlet?first=11&second=22

say:

http://mycompany.com/myapp/myservlet/11/22

AlexR
  • 114,158
  • 16
  • 130
  • 208
  • I'm using query string parameter in a POST because i want to maintain any and all query strings passed to the original form through the POST. This is primarily for tracking purposes... i'm not sure that the REST-like convention would work in this case, because I'm pretty sure the tracking library looks for specific parameters in the query string. – JasonStoltz Jul 27 '11 at 16:16
  • Any reason in particular that you would recommend using the REST-like convention to a query string parameter? Not sure I understand what the advantage is? – JasonStoltz Jul 27 '11 at 16:41
  • 8
    "BTW the fact that such API absent may mean that probably you should check your design." In this case it means Oracle should check their design :) – Stijn de Witt Nov 04 '13 at 12:34