Problems with unicode encoding in spring projects

I had a problem with the encoding of Unicode characters. I developed a simple web application with support for multiple languages​​, which is called internationalization or i18n. In this application, the data were saved in a database, and then re-shown. But I get exceptions on production and it was a unicode utf-8 character encoding problem.
By the way, my application was developed using spring and hibernate and jpa2. It was kind of a nightmare and I was getting the exceptions frequently and I was completely no where-zone. At least, after searching and using many sources, I found out the solutions.

So here is the solutions–

First you need add some extra parameter in your database url. For example–

jdbc\:mysql\://localhost\:3306/mydatabase?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8

Secondary you’ve to add a filter in your web.xml

<filter>

<filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

And finally you have to make sure you have added following line in your jsp pages

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

and finally follow the command in your terminal

You should now be logged into the MySQL prompt. Here type the following commands :

mysql> use user_db;

mysql> alter database user_db default character set utf8 collate utf8_unicode_ci;

Query OK, 1 row affected (0.00 sec)

This should have done it.

okay, that’s how I solved the issue.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s