HttpServletRequest - Get query string parameters, without form data

In HttpServletRequest , getParameterMap returns a map of all query string parameters and data parameters after sending.

Is there a way to get string parameter information for ON only? I am trying to avoid using getQueryString and parsing values.

+67
java query-string servlets
Jul 27 '11 at 15:41
source share
5 answers

Unlike what cularis said, there can be both in the parameter map.

The best way I see is the parameterMap proxy file and for each parameter search it checks if queryString contains "&? <parameterName> =".

Note that the parameter name must be URL encoded before this check can be performed, as pointed out by Qerub.

This saves your parsing and still gives you only URL parameters.

+20
Jul 27 '11 at 15:53
source

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

 username=james&password=pwd 

To get a name, you can do it

 request.getParameter("username"); 
+75
Feb 12 '14 at 10:14
source

The servlet API does not have this function because it was created at a time when many believed that the query string and message body were just two different ways of sending parameters, not realizing that the goals of the parameters are fundamentally different.

Query string options? foo = bar are part of the URL because they are involved in identifying the resource (which can be a collection of many resources), for example, "all persons aged 42":

GET / people? age = 42

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

Put / people? age = 42

hair = gray

Thus, definitely RESTful uses both query parameters and body parameters at the same time, separated so that you can use them for different purposes. This feature is certainly not available in the Java Servlet API.

+19
Sep 08
source

According to other answers, there is no way to get query string parameters using api servlet.

So, I think the best way to get query parameters is to parse the query string yourself. (It’s harder to repeat the parameters and check if the query string contains the parameter)

I wrote the code below to get the 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; } 
+11
Aug 10 '15 at 20:41
source

I am afraid that it is not possible to get query string parameters parsed separately from message parameters. By the way, the fact that such an API is missing may mean that you should probably check your design. Why are you using the query string when sending POST? If you really want to send more data to a URL, use a REST convention, for example. instead of sending

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

they say:

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

+2
Jul 27 '11 at 15:55
source



All Articles