I guess, every programmer who ever did localization (or i18n) wonders why the creators of first computers haven’t inroduced a common character encoding once and for all. This article will discuss an approach for making url encoding/decoding transparent.
I have noticed that for example cyrillic characters do not arrive from the web form to the server as they are - every time you need to excercise caution when dealing with anything other than ASCII, so that the characters soup %D1%80%D1%83%D1%81%D1%81%D0%BA%D0%B8%D0%B9 is converted to - русский.
For web forms using POST method everything is quite simple: browsers usually encode characters using the encoding specified in Сontent-type meta tag. Then you have to decode the characters on the server. For this purpose you can use a filter, which will set request and response encoding, for example utf-8.
However, this works well only for web forms which use POST, as for the GET and links you will have to use some other approach. The problem is that the query string - the text that goes after ? in the URL, is processed by Tomcat separately, and the encoding used is ISO-…., which is ASCII.
All in all the solution is simple - in Tomcat’s connector you have to add the attribute - useBodyEncodingForURI=“true”. Or URIEncoding, if you are sure what encoding is used for parameters. I chose the first way + a filter, and am happy now. When using the useBodyEncodingForURI the encoding used to decode request parameters is the same as used for decoding the request body - before accessing any request parameters set request encoding, eg. request.setCharacterEncoding(“utf-8”).
Here is how the hacked Tomcat connector looks like (file server.xml):
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" useBodyEncodingForURI="true"/>First published on the old anydoby.com blog on 2007-09-14.