Roger McCarthy
Roger McCarthy

Reputation: 11

Users seeing inconsistent representation of unicode text across request cycle in Spring 3 MVC

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
  1. Text that I pass as a RequestParam from the page to the controller.
  2. How that is output in the log file immediately after reading it from the RequestParam in the controller
  3. 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?

Upvotes: 1

Views: 164

Answers (2)

Bozho
Bozho

Reputation: 597076

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" %>

Upvotes: 1

Stephen C
Stephen C

Reputation: 718768

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 ...)

Upvotes: 0

Related Questions