user2064769
user2064769

Reputation: 51

getParameter special characters

I'm trying to get an url parameter in jee. So I have this kind of url :

http://MySite/MySite.jsp?page=recherche&msg=toto

First i tried with : request.getParameter("msg").toString(); it works well but if I try to search "c++" , the method "getParameter()" returns "c" and not "c++" and i understand.

So I tried another thing. I get the current URL and parse it to get the value of the message :

String msg[]= request.getQueryString().split("msg=");
message=msg[1].toString();

It works now for the research "c++" but now I can't search accent. What can I do ?

EDIT 1

I encode the message in the url

String urlString=Utils.encodeUrl(request.getParameter("msg"));

so for the URL : http://MySite/MySite.jsp?page=recherche&msg=c++

i have this encoded URL : http://MySite/MySite.jsp?page=recherche&msg=c%2B%2B

And when i need it, i decode the message of the URL

String decodedUrl = URLDecoder.decode(url, "ISO-8859-1");

Thanks everybody

Upvotes: 2

Views: 4772

Answers (4)

Thorn
Thorn

Reputation: 4057

You need to escape special characters when passing them as URL parameters. Since + means space and & means and another parameter, these cannot be used as parameter values.

See this other S.O. question.

You may want to use the Apache HTTP client library to help you with the URL encoding/decoding. The URIUtil class has what you need.

Something like this should work:

String rawParam = request.getParameter("msg");
String msgParam = URIUtil.decode(rawParam);

Your example indicates that the data is not being properly encoded on the client side. See this JavaScript question.

Upvotes: 0

Ian Roberts
Ian Roberts

Reputation: 122364

if I try to search "c++" , the method "getParameter()" returns "c" and not "c++"

Query parameters are treated as application/x-www-form-urlencoded, so a + character in the URL means a space character in the parameter value. If you want to send a + character then it needs to be encoded in the URL as %2B:

http://MySite/MySite.jsp?page=recherche&msg=c%2B%2B

The same applies to accented characters, they need to be escaped as the bytes of their UTF-8 representation, so été would need to be:

msg=%C3%A9t%C3%A9

(é being Unicode character U+00E9, which is C3 A9 in UTF-8).

In short, it's not the fault of this code, it's the fault of whatever component is responsible for constructing the URL on the client side.

Upvotes: 1

Martin
Martin

Reputation: 3058

Anything you send via "get" method goes as part of the url, which needs to be urlencoded to be valid in case it contains at least one of the reserved characters. So, any character will need to be encoded before sending.

In order to send c++, you would have to send c%2B%2B. That would be interpreted properly at the server side.

Here some reference you can check:

http://www.blooberry.com/indexdot/html/topics/urlencoding.htm

Now the question is, how and where do you generate your URL? According to the language, you will need to use the proper method to encode your strings.

Upvotes: 1

rzymek
rzymek

Reputation: 9281

Call your URL with

msg=c%2B%2B

+ in a URL mean 'space'. It needs to be escaped.

Upvotes: 0

Related Questions