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