Spring, Hibernate, MySQL and Tomcat Unicode Problem solving
1. Spring:
In web.xml need to configure Unicode Encoding filter to accept Unicode in request and response. This should be the first filter in 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>
<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>
2. Hibernate:
In hibernate.cfg.xml the following lines should be added:
<property name="hibernate.connection.CharSet">utf8</property> <property name="hibernate.connection.characterEncoding">utf8</property> <property name="hibernate.connection.useUnicode">true</property>
And in database URL for MySQL add the following:
UseUnicode=true&characterEncoding=utf8
3. MySQL:
Apply to Column and Table >
CHARACTER SET utf8 COLLATE utf8_unicode_ci
4. Tomcat:
It's necessary to configure that the connector uses UTF-8 to encode url (GET request) parameters:
port="8080" maxHttpHeaderSize="8192"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true"
URIEncoding="UTF-8"
/>
5. Tomcat with Apache
One more thing If you are using Apache + Tomcat + mod_JK connector then you also need to do following changes:
a) Add URIEncoding="UTF-8" into tomcat server.xml file for 8009 connector, it is used by mod_JK connector.
b) Goto your apache folder i.e. /etc/httpd/conf and add AddDefaultCharset utf-8 in httpd.conf file. Note: First check that it is exist or not. If exist you may update it with this line. You can add this line at bottom also.
Comments