Reputation: 14363
I have a url, something like this localhost:8080/foo.action?param1=7¶m2=8¶m3=6
When this is the Url (as it is), request.getParmeter("param2")
gives me 8
[Correct]
i) When the encoding converts this url to localhost:8080/foo.action?param1=7%26param2=8%26param3=6
In this case, request.getParameter("param1")
gives me 7¶m2=8¶m3=6
ii) When the encoding converts this url to localhost:8080/foo.action?param1=7&param2=8&param3=6
In this case, request.getParameter("param1")
gives me 7
and request.getParameter("param2")
gives me null
What is the correct way of retrieving the parameters? [Assuming that using one of the two Url encoding schemes is unavoidable]
(I am using struts actions)
Upvotes: 1
Views: 14774
Reputation: 4006
I had this happen to me today. Turns out I was passing the encoded url over the wire. When the request is made it should be made as http://localhost/foo?bar=1&bat=2
not as http://localhost/foo?bar=1&bat=2
.
In this case I had cut the url from an xml file and pasted it into a browser.
Upvotes: 0
Reputation: 5210
To prevent this do not encode parameters with delimeters, encode only parameters values. This will be the best way. If you cannot handle parameters encoding just do decoding on server side before parsing:
String queryString = request.getQueryString();
String decoded = URLDecoder.decode(queryString, "UTF-8");
String[] pares = decoded.split("&");
Map<String, String> parameters = new HashMap<String, String>();
for(String pare : pares) {
String[] nameAndValue = pare.split("=");
parameters.put(nameAndValue[0], nameAndValue[1]);
}
// Now you can get your parameter:
String valueOfParam2 = parameters.get("param2");
Upvotes: 3
Reputation: 910
Try using
String[] Parameters = = URLDecoder.decode(Request.getQueryString(), 'UTF-8').splitat('&') ;
Hope this helps.
Upvotes: 0
Reputation: 1784
You can call req.getQueryString()
to get the whole query parameters and then do server side decoding based on whatever encoding methods you choose.
Upvotes: 1