Reputation: 5552
I'm using Hibernate for a JEE solution. And I need to database connection to be UTF-8. Here're what I tried :
mysql> select c.character_set_name from information_schema.tables as t, information_schema.collation_character_set_applicability as c where c.collation_name = t.table_collation and t.table_schema = "ir2016" and t.table_name = "personne";
+--------------------+
| character_set_name |
+--------------------+
| utf8 |
+--------------------+
1 row in set (0.01 sec)
I've also inserted example data from MySQL Workbench. Results are well encoded in UTF-8. So the problem must come from the JEE server side.
hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!--Databaseconnectionsettings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/ir2016?useUnicode=true&characterEncoding=utf-8</property>
<property name="connection.username">root</property>
<property name="connection.password">xxxx</property>
<property name="connection.useUnicode">true</property>
<property name="connection.characterEncoding">utf8</property>
<property name="connection.CharSet">utf8</property>
<!--JDBC connectionpool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!--SQL dialect-->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!--EnableHibernate'sautomaticsession contextmanagement -->
<property name="current_session_context_class">thread</property>
<!--Disablethe second-levelcache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!--Echo all executedSQL to stdout-->
<property name="show_sql">true</property>
<mapping resource="Quiz.hbm.xml"/>
<mapping resource="Proposition.hbm.xml"/>
<mapping resource="Question.hbm.xml"/>
<mapping resource="Personne.hbm.xml"/>
<mapping resource="Choisir.hbm.xml"/>
</session-factory>
</hibernate-configuration>
server.xml
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="UTF-8"/>
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" URIEncoding="UTF-8"/>
JSP pages
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="UTF-8"%>
Can somebody help ?
Upvotes: 2
Views: 6379
Reputation: 5552
This is a JEE solution, so you need to check your web.xml
whether it is configured to accept UTF-8 and I just forget about it. I need a filter to do it
web.xml
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>com.yourcompany.yourapp.util.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>requestEncoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
CharacterEncodingFilter.java
package com.yourcompany.yourapp.util;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Struts 1.3.10 encoding problem
*
* @see http://www.coderanch.com/t/557874/Struts/Struts-encoding
*/
public class CharacterEncodingFilter implements Filter {
private FilterConfig fc;
@Override
public void destroy() {
// TODO Auto-generated method
}
@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp;
request.setCharacterEncoding("UTF8");
response.setCharacterEncoding("UTF8");
chain.doFilter(request, response);
request.setCharacterEncoding("UTF8");
response.setCharacterEncoding("UTF8");
}
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
Source: Struts 1.3.10 encoding problem
Upvotes: 1