java - How to set UTF-8 properly in Hibernate? -


i'm using hibernate jee solution. , need database connection utf-8. here're tried :

database side

mysql> select c.character_set_name information_schema.tables t, information_schema.collation_character_set_applicability c c.collation_name = t.table_collation , t.table_schema = "ir2016" , t.table_name = "personne"; +--------------------+ | character_set_name | +--------------------+ | utf8               | +--------------------+ 1 row in set (0.01 sec) 

i've inserted example data mysql workbench. results encoded in utf-8. problem must come jee server side.

jee 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&amp;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 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 executedsql 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 ?

this jee solution, need check web.xml whether configured accept utf-8 , forget it. need filter 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


Comments