I try to make a cookie's value generated from the server side (in Java) identical to a cookie's value generated from this jquery's cookies librarie, which use :
encodeURIComponent( value )
to encode the cookie value.
Let's take this example string :
a b,c;d*~'"()!±
I use this code, in Java, to encode a cookie value :
String result = URLEncoder.encode(str, "UTF-8")
.replaceAll("\\+", "%20")
.replaceAll("\\%21", "!")
.replaceAll("\\%27", "'")
.replaceAll("\\%28", "(")
.replaceAll("\\%29", ")")
.replaceAll("\\%7E", "~");
I then add the cookie to the response :
Cookie cookie = new Cookie("testCookieServerSide", encodeValue("a b,c;d*~'\"()!±"));
response.addCookie(cookie);
This adds double-quotes around the value! :
"a%20b%2Cc%3Bd*~'%22()!%C2%B1"
When encoding the same value using the jquery library, I get the same value but without the double-quotes :
a%20b%2Cc%3Bd*~'%22()!%C2%B1
Is it possible to tell Java to not generate the double-quotes? I've heard of cookie.setVersion(0) or cookie.setVersion(1) but this doesn't seem to change anything.
When retrieving those two values on the server-side, they are identical after a :
URLDecoder.decode(cookie.getValue(), "UTF-8");
... the double-quotes are removed. Great.
But on the client side, the two values are differents!
alert($.cookies.get('testCookieServerSide')); => "a b,c;d*~'"()!±"
alert($.cookies.get('testCookieClientSide')); => a b,c;d*~'"()!±
Any idea on how to generate the exact same value? How do you deal with this?