Tomcat, Cocoon, AJAX, and Unicode
Posted by mholmes on 25 Apr 2007 in Activity log, Documentation
We have had a series of problems in our Cocoon projects submitting AJAX requests containing data which is not ASCII. Today, after much research, I came up with a solution, based on this useful explanation.
This is the basic setup required to make Unicode work on Cocoon:
- The important bit we learned today: Tomcat defaults to treating request data as 8859-1, so Cocoon needs to be configured to work around this. In Cocoon's
web.xmlfile, you need to set this:<init-param> <param-name>container-encoding</param-name> <param-value>ISO-8859-1</param-value> </init-param> <init-param> <param-name>form-encoding</param-name> <param-value>UTF-8</param-value> </init-param>
- The Web page itself must be UTF-8. For maximum reliability, this should be specified in the HTTP headers (so make sure whatever Cocoon serializer you're using to send out the page is set to use UTF-8) and also include a meta tag like this:
<meta http-equiv="Content-Type" content="text/xhtml; charset=utf-8"/>
- The XMLHttpRequest object must be using GET; POST is flaky with regard to Unicode.
- You must set the request header for the XMLHttpRequest like this:
setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'); - You must encode any data which has non-ascii characters in it prior to transmission, using the JavaScript
encodeURIComponent()function.
Provided all this is done, then Unicode characters will be reliably sent back to the server, and understood correctly in the Cocoon pipeline (so the XQuery code which handles them, in the case of our projects, will work properly).