Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unicode and response.addCookie() problem 1

Status
Not open for further replies.

BabyJeffy

Programmer
Sep 10, 2003
4,189
GB
Good (GMT) afternoon!

I'm currently running into a problem with some code I've recently put together. I'm running Tomcat (4.X) with Apache (2.X) and mySQL on a Windows 2003 Server platform.

I'm bringing back a string from a mySQL database and displaying it on the page... and this works fine for Unicode characters too (specifically I can display accented "e" character).

If I add the following code to the page, with the intent of setting this string into a session cookie, the page breaks:

Code:
response.addCookie(new Cookie("cookiename", _myString));

If I comment out that line, then the page renders correctly and I see the unicode content appearing fine.

I can find reference about cookie names not allowing unicode characters... but no details about the cookie data.

Does anyone out there have a solution?

Jeff
 
Well I've trawled a lot more online resources since my last post... and I have no solution. I have discovered that the code also breaks if I use a comma "," character as well.

Is there nobody out there that can even post some keywords or something that would lead me to something online? I'm not leeching... I'm actively researching... but I can't believe that I am the only person that has need to store commas and unicode characters into a cookie!

Thanks in advance,
Jeff
 
If cookie values are only set and read at server side, you can use URLEncoder/URLDecoder to encode and decode cookie value.

e.g. to encode the cookie value:
Code:
response.addCookie(new Cookie("cookiename", java.net.URLEncoder.encode(_myString, "UTF-8)));

when getting the value back, use the URLDecoder to decode it.


 
byam - thank you very much for this.

It was specifically the syntax for URLEncoder/URLDecoder that I was lost on. Once I added this into the mix everything came to life.

I was able to very quickly integrate this into my existing code and had my UTF8 characters safely going into (and out of) the session cookies. With no problems.

Thanks again - have a star on me *grin*
Jeff
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top