HttpServletRequest - Get query string parameters, no form data

徘徊边缘 提交于 2019-12-17 15:53:20

问题


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.


回答1:


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.




回答2:


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"); 



回答3:


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.




回答4:


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.

public static Map<String, String[]> getQueryParameters(HttpServletRequest request) {
    Map<String, String[]> queryParameters = new HashMap<>();
    String queryString = request.getQueryString();

    if (StringUtils.isEmpty(queryString)) {
        return queryParameters;
    }

    String[] parameters = queryString.split("&");

    for (String parameter : parameters) {
        String[] keyValuePair = parameter.split("=");
        String[] values = queryParameters.get(keyValuePair[0]);
        values = ArrayUtils.add(values, keyValuePair.length == 1 ? "" : keyValuePair[1]); //length is one if no value is available.
        queryParameters.put(keyValuePair[0], values);
    }
    return queryParameters;
}



回答5:


Java 8

return Collections.list(httpServletRequest.getParameterNames())
                  .stream()
                  .collect(Collectors.toMap(parameterName -> parameterName, httpServletRequest::getParameterValues));



回答6:


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



来源:https://stackoverflow.com/questions/6847192/httpservletrequest-get-query-string-parameters-no-form-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!