javaservletsgetgetparameter

Servlet: "%2B" is converted into space


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.


Solution

  • + 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);