HttpServletRequest - Get query string parameters, no form data

前端 未结 6 750
南笙
南笙 2020-12-05 09:08

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

相关标签:
6条回答
  • 2020-12-05 09:38

    Java 8

    return Collections.list(httpServletRequest.getParameterNames())
                      .stream()
                      .collect(Collectors.toMap(parameterName -> parameterName, httpServletRequest::getParameterValues));
    
    0 讨论(0)
  • 2020-12-05 09:44

    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;
    }
    
    0 讨论(0)
  • 2020-12-05 09:47

    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.

    0 讨论(0)
  • 2020-12-05 09:51

    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

    0 讨论(0)
  • 2020-12-05 09:57

    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"); 
    
    0 讨论(0)
  • 2020-12-05 10:03

    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.

    0 讨论(0)
提交回复
热议问题