Cacheing
Cacheing

Reputation: 3471

java servlet: request parameter contains plus

The request parameter is like decrypt?param=5FHjiSJ6NOTmi7/+2tnnkQ==.

In the servlet, when I try to print the parameter by String param = request.getParameter("param"); I get 5FHjiSJ6NOTmi7/ 2tnnkQ==. It turns the character + into a space. How can I keep the orginal paramter or how can I properly handle the character +.

Besides, what else characters should I handle?

Upvotes: 4

Views: 8179

Answers (4)

Aurelius Baier
Aurelius Baier

Reputation: 121

Allthough the question is some years old, I'd like to write down how I fixed the problem in my case: the download link to a file is created in a GWT page where

com.google.gwt.http.client.URL.encode(finalurl)

is used to encode the URL. The problem was that the "+" sign a customer of us had in the filename wasn't encoded/escaped. So I had to remove the URL.encode(finalurl) and encode each parameter in the url with

URL.encodePathSegment(fileName)

I know my question is bound to GWT but it seems, URLEncoder.encode(string, encoding) should be applied to the parameter only aswell.

Upvotes: 0

Amir T
Amir T

Reputation: 2758

You have two choices

URL encode the parameter

If you have control over the generation of the URL you should choose this. If not...

Manually retrieve the parameter

If you can't change how the URL is generated (above) then you can manually retrieve the raw URL. Certain methods decode parameters for you. getParameter is one of them. On the other hand, getQueryString does not decode the String. If you have only a few parameters it shouldn't be difficult to parse the value yourself.

request.getQueryString();
//?param=5FHjiSJ6NOTmi7/+2tnnkQ==

Upvotes: 6

Mark Thomas
Mark Thomas

Reputation: 16625

If you want to use the '+' character in a URL you need to encode it when it is generated. For '+' the correct encoding is %2b

Upvotes: 2

NishM
NishM

Reputation: 1726

Use URLEncoder,URLDecoder's static methods for encoding and decoding URLs.

For example : - Encode the URL param using

    URLEncoder.encode(url,"UTF-8") 

Back in the server side , decode this parameter using

    URLDecoder.decode(url,"UTF-8") 

decode method returns a String type of the decoded URL.

Upvotes: 0

Related Questions