I’m having trouble with UTF-8.
common.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
typical.jsp
<%@ include file="common.jsp" %>
Page Head
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
Form
<form id="screenObject" accept-charset="UTF-8" action="/SiteAdmin/articleHeaderEdit?articleId=15" method="post">
I enter non latin1 characters into a text field and click Save. Validator complains about another field and stops the submission. This never gets to the database, so database ability to handle UTF-8 is not in this picture. The page redisplays with appropriate error but the text that had been entered is all messed up. All non latin1 characters are converted to some gibberish.
I’m using Spring 3 MVC, in case that matters…
Attempts
Adding this to my view resolver didn’t help:
<property name="contentType" value="text/html;charset=UTF-8" />
Solution
Add encoding filter to web.xml.
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Someplace in the data flow you are using something other than UTF-8 to either encode a string as bytes or to decode a byte stream as a string. This typically happens when you use some API call that uses the default character encoding. Without knowing your code, that’s all we can tell you.
Also see this link for character encoding problems in Spring, or just search the forum for
[spring] character encodingfor lots of other postings.