I have a problem with UTF-8. I am using Spring MVC and MySQL.
In Spring, I configured web.xml with
<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>
and in my bean data source i have:
<bean id="dataSource" class="it.roundtable.db.manager.CustomDataSource" init-method="init"> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean>
where is CustomDataSource:
import org.apache.tomcat.dbcp.dbcp.BasicDataSource; public class CustomDataSource extends BasicDataSource { private void init() { addConnectionProperty("useUnicode", "true"); addConnectionProperty("characterEncoding", "UTF-8"); }
}
On my layout page, I:
<%@page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%> <meta http-equiv="Content-Type" content="text/html; charset=utf8" charset="utf8" >
In MySQL, I have:
mysql> SHOW VARIABLES LIKE 'char%'; +--------------------------+----------------------------+ | Variable_name | Value | +--------------------------+----------------------------+ | character_set_client | utf8 | | character_set_connection | utf8 | | character_set_database | utf8 | | character_set_filesystem | binary | | character_set_results | utf8 | | character_set_server | utf8 | | character_set_system | utf8 | | character_sets_dir | /usr/share/mysql/charsets/ | +--------------------------+----------------------------+ 8 rows in set (0.08 sec)
and
mysql> SHOW VARIABLES LIKE 'colla%'; +----------------------+-----------------+ | Variable_name | Value | +----------------------+-----------------+ | collation_connection | utf8_general_ci | | collation_database | utf8_general_ci | | collation_server | utf8_general_ci | +----------------------+-----------------+ 3 rows in set (0.00 sec)
and in my.cnf:
[client] port = 3306 socket = /var/run/mysqld/mysqld.sock default-character-set=utf8 .... [mysqld] character-set-server=utf8 default-character-set=utf8 default-collation=utf8_general_ci init-connect='SET NAMES utf8' character-set-client=utf8 skip-external-locking ....
Now that I have written in a form a phrase like this:
This is a prove: àèìòù
in Spring controller I am typing in a log:
... logger.info("text --> " + text); ...
and the result printed in the eclipse console is correct.
Then I save my text in a table in mysql with a data source, and if I connect to mysql using bash, I can read the correct text stored in my table. In the next step, the Spring controller retrieves the data from mysql with the query, but I can see from the logger (eclipse console) that the letter "àèìù" is incorrectly encoded:
This is a prove:
so the result on my webpage will be the same. Can you help me?
I found an error: the type I used to save the text was BLOB, now I use the TEXT type and everything works fine. Excuse me!