3

When I'm reading the value of a cookie, the part after '@' is being ignored. So, if my cookie value was "abc@xyz", I'm just getting "abc" when I'm retrieving value by

  Cookie cookies [] = request.getCookies ();
    pwd=cookies[0].getValue();

whereas, in javascript I'm able to easily read it as "abc@xyz" and even in browser cookies, I can see the value of cookie to be "abc@xyz". What could be wrong here?

RMT
  • 7,040
  • 4
  • 25
  • 37
Atul Goyal
  • 3,511
  • 5
  • 39
  • 59
  • See http://stackoverflow.com/questions/1969232/allowed-characters-in-cookies – home Jul 20 '11 at 17:33
  • You mean '@' is not allowed as part of cookie? If yes, isn't it strange that while saving, it is being saved with '@' and even Javascript is able to read it as whole! – Atul Goyal Jul 20 '11 at 17:37

1 Answers1

1

My first guess would be a problem related to character encoding. Have you tried to urlencode and -decode the cookie value?

EDIT:

You would retrieve the cookie value by using URLDecoder.decode (cookies[0].getValue(), "utf-8").

In order for that to work, the value must of course be encoded in the first place: Use URLEncoder.encode("abc@xyz", "utf-8"), if you're setting the cookie value from Java, or encodeURIComponent("abc@xyz") to set the value from JavaScript. I don't know how the cookie is set, so you might have to figure this one out for whatever platform you're working on.

weltraumpirat
  • 22,544
  • 5
  • 40
  • 54
  • Perhaps you can help me with how to do that? will defo try it. Thanks – Atul Goyal Jul 20 '11 at 17:28
  • yes, it worked! Many many Thanks! earlier I was using [escape()](http://www.w3schools.com/jsref/jsref_escape.asp) in javascript for encoding the value, but just got to know that it doesn't encodes '@'; encodeURIComponent() encodes '@' as well, so it could be encoded properly and then decoded with URLDecoder.decode() in java as described by you. :) – Atul Goyal Jul 20 '11 at 18:42