Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpServletRequest - Get query string parameters, no form data

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.

like image 390
JasonStoltz Avatar asked Jul 27 '11 15:07

JasonStoltz


3 Answers

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"); 
like image 152
coastline Avatar answered Oct 16 '22 21:10

coastline


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.

like image 26
DoubleMalt Avatar answered Oct 16 '22 21:10

DoubleMalt


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.

like image 24
Ola Berg Avatar answered Oct 16 '22 20:10

Ola Berg