Hi,
I'm using tomcat 5.5.15.
I have a servelt which reads parameters from a form , using
HttpServletRequest.getParameter().
The catch is, parameters data (filled in by the user) is in Chinese.
I noticed that even though my page encoding is unicode, the browser (IE6)
seems to send the parameters in utf-8 (I'm not sure whether it's part of the
HTTP spec, or is it and IE6 thing ?).
Unfortunately, Tomcat's implementation for HttpServletRequest.getParameter()
returns garbage, because it does *not* assume that parameters are encoded in
utf-8 (it seems to assume latin1, or something very similar).
I did manage to forcefully convert the data, using the following ugly code:
// supposed to be a chinese city. I get garbage, because browser sends
// the city in utf-8, but tomcat seems to assume its 'latin1' :
String city=request.getParameter(city);
// ugly convertion to utf-8:
byte[] bytes=new byte[paramValue.length()];
for(int i=0; ibytes.length; i++)
bytes[i]=(byte) paramValue.charAt(i);
String converted=new String(bytes, utf-8);
// now converted seems to be the correct city
I was wondering, is there a nicer way to do it ?
E.g. is there some tomcat configuration file, where you can tell it the
request.getParameter() should parse the data assuming the browser sent it using
utf-8 ?
Thanks very much.
-
Yahoo! Mail
Bring photos to life! New PhotoMail makes sharing a breeze.