views:

20

answers:

2

Hello, I have a web app that gathers some data from the user and saves them to a mysql database. The problem is that for a String like "Ajánlat kiküldése", what gets stored to the database is "Ajánlat kiküldése".

For my database, i have DEFAULT CHARACTER SET utf8. For my tables i have DEFAULT CHARSET=utf8.

In my hibernate.cfg.xml i have:

<property name="hibernate.connection.useUnicode">true</property>
<property name="hibernate.connection.characterEncoding">UTF-8</property>
<property name="hibernate.connection.charSet">UTF-8</property>

If I enter "Ajánlat kiküldése" directly into the database using a mysql client, the text gets stored correctly. SO somewhere along the way inside my application the text gets changed.

I have this in my jspx page:

<meta content="text/html; charset=UTF-8" http-equiv="Content-Type"/>

For my form I use sutrts html taglib and the code is very simple:

<html:form action="/submitAddProject">
            <table>
                <tr>
                    <td>name</td>
                    <td>
                        <html:text property="projectNameToAdd"/> 
                    <td>
                 etc.

On submit, when I do myForm.getProjectNameToAdd, I have a different text than the one I enter in the form. (Ajánlat kiküldése instead of Ajánlat kiküldése )

A: 

Is that in PHP? Try using

mysql_query("SET NAMES utf8");

for starters... Do you have a link to the app?

Claudiu
I doubt it's PHP what the OP is using, but the rest is o.k. :)
Col. Shrapnel
I don't use php. I use apache struts (java servlets).
mihaela
@mihaela just run this query. every time your app connects to mysql
Col. Shrapnel
Sorry for that then. Try setting them as _utf8_unicode_ci_ This did the trick for me... but using a PHP app, hope there aren't any differences.
Claudiu
Claudiu `utf8_unicode_ci` is collation, it will affect encoding too, but actually it is encoding should be set, which is `utf8`
Col. Shrapnel
Thanks for your interest to help, but the solution was the one pointed out by Roman. The problem was not with the database or the way I was accessing it. The parameters of the request were being interpreted by my servlet using a wrong encoding. I had no control over them because things were being done automatically by struts. Problem was solved by placing a filter in web.xml.
mihaela
+1  A: 

You can try to create the following filter:

public class Utf8EncodingFilter implements Filter {

 public void init(FilterConfig config) throws ServletException {}

 public void doFilter(ServletRequest request, ServletResponse response, FilterChain next)
 throws IOException, ServletException {
  request.setCharacterEncoding("utf-8");
  next.doFilter(request, response);
 }

 public void destroy(){}
}

UPDATE: You need to create Utf8EncodingFilter class, import Filter interface, exception, etc. (any modern IDE will do this for you). Then you need to add this filter into your deployment descriptor (can be found at WEB-INF/web.xml) using the following syntax:

  <filter>
    <filter-name>UTF-8 Encoding Filter</filter-name>
    <filter-class>com.example.Utf8EncodingFilter</filter-class>
  </filter>

  <filter-mapping>
    <filter-name>UTF-8 Encoding Filter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

You can read more about filters here: http://www.ibm.com/developerworks/java/library/j-pj2ee10.html

Roman
Sry but how should I use this class? Where to put it ? :) In struts, there is a servlet called controller I think, that calls reset and setters for my form object. When the setter is called, it already receives a wrong char encoding. So, form has good data, setter receives bad data. Somewhere along the way someone twists the data.
mihaela
You need to create Utf8EncodingFilter class, import Filter interface, exception, etc. (any modern IDE will do this for you). Then you need to add this filter into your deployment descriptor (can be found at WEB-INF/web.xml) using the following syntax:
Roman
no, the syntax is missing :) please try again. stack overflow editing is a little strange :)
mihaela
So every web application that wants to use strange chars has to do this thing with the filter?
mihaela
Sorry for this, couldn't insert syntax, so just updated my answer.
Roman
Yes, this filter helped me to fix this problem.
Roman
Thank you very much. I've tried lots of possible solutions. This was the only thing to help.
mihaela
You are very welcome! Thank you for your vote!
Roman