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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem utf8 encoding in httpservletresponse

Status
Not open for further replies.

eyecon1

Programmer
Sep 8, 2003
2
AU
Basically, I have set up a chat program using http request. One person sends a message to the servlet, it gets stored in a StringBuffer. Other end of chat polls using http requests, and if a message is sitting there for them, it sends it back in a response header.

It has to be multilingual, and my problem is that CJK type characters just don't seem to be encoding. When message is orignally sent, it is URLEncoder'ed. Servlet receives these characters, and they are fine (at this point I write them to a log, which works). When the request comes in for the message though, I figured I should just be able to
send the characters straight back, but it doesn't work. If I URLEncoder them here, they become ???.

I have put a little method in there which escapes the foreign string to unicode, so I can check what's up. In the servlet side the string is fine just before I send it (eg \u30CF\u30A4) but when it gets to the other side it's not fine (eg \00CF\00A4). Looks like problem with encoding right? I've tried everything...that I can think of. I need help.

In the servlet, it's stored in a StringBuffer, and I simply return it in a header:
res.setContentType("text/html; charset=utf8");
res.setHeader( "responseMessage", responseMessage);
(where res = HttpServletResponse)

In the app:
connection = ( HttpsURLConnection ) url.openConnection();
responseMessage =
connection.getHeaderField("responseMessage");
(where connection = HttpUrlConnection and responseMessage = String)

Just suggestions would be good. It should just be a simple process, but I'm worried I may end up having to override ContentHandlers, or figuring out a method to escape the "\u" strings back to the foreign characters.. eep.
 
i think this went back a while back. It might help you.

thread269-657004

i have the same problem - only with different flavor - with JNI. I tried to send some string to C code but when it got to "C"-land, it turned out to be a diff char.

Hence, I start using byte [] array. It solves my problem every time.



~za~
You can't bring back a dead thread!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top