While testing my app, I noticed the following issue where a string containing a unicode character, when passed to the controller and then back to the page, becomes garbled - or at least is not represented consistently - from the user's perspective.
1. Xáeso
2. Xßeso
3. X%E1eso
- Text that I pass as a
RequestParam
from the page to the controller. - How that is output in the log file immediately开发者_高级运维 after reading it from the
RequestParam
in the controller - The text passed from the controller back to the client after the request handler exits.
This is the only valid representation of the text as far as what the user would expect to see:
1. Xáeso
How can I ensure that the unicode character in position 2 of this string is represented consistently to the client-side user of the app?
There are multiple places where encoding can go wrong.
A quick fix might be to use spring's CharacterEncodingFilter
(or the equivalent interceptor), and set the encoding to utf-8
. Also, your JSP pages better have <%@ page pageEncoding="utf8" %>
My guess is that part of the problem is that the web server (e.g. Tomcat) is using the wrong character set when translating the request parameters into UTF-16.
If you are using Tomcat, read the Character Encoding FAQ, and check each of the issues listed. (Pay particular attention to the bit that talks about the effects of filters and valves ...)
精彩评论