1

I have very simple problem

I'm forming one GET request as follows, parameters are

para1=abc+xyz

Notice the '+' sign here. Now when I url encode this I get para1=abc%2Bxyz. Which is okay!

Now on servlet side, I have code like following

String para1 = request.getParameter("para1")

Content of para1 are abc xyz (notice the space).

Shouldn't it be abc+xyz? I want the value to be as it was sent from the source, not the messed up one.

Ganesh Satpute
  • 3,664
  • 6
  • 41
  • 78

3 Answers3

2

+ is decoded as space after url decoding. If you want to pass +, you need to encode it.

Java

 String ecodedValue = URLEncoder.encode("abc+xyz", "UTF-8");
 String decodedValue = URLDecoder.decode(ecodedValue, "UTF-8");

Ajax

var encoded = encodeURIComponent(str);

Javascript

var uri = "my test.asp?name=ståle&car=saab";
var res = encodeURI(uri);

or

var res = encodeURIComponent(uri);
Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116
1

They're equivalent. Both the + sign and space are translated to spaces. If you want to send a literal + sign, you need to encode it.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
0

You should always encode your parameter values while posting it to a URL. You just need to encode your parameter value using,

URLEncoder.encode(paramValue, "UTF-8");

Ashish Bhosle
  • 609
  • 5
  • 18