Hibernate + MySQL Chinese problem solution.

Source: Internet
Author: User
1. Set the character set UTF-8 during MySQL installation. The JDBC driver is later than 3.0.15.

2. Add attributes to the hibernate configuration file.

<Property name = "connection. useunicode"> true </property>

<Property name = "connection. characterencoding"> UTF-8 </property>

3. Set filter in Web. xml

========================================================== ==========
<Filter>

<Filter-Name>

Set web application character encoding

</Filter-Name>

<Filter-class> cn.com. commnet. util. setencodefilter </filter-class>

<Init-param>

<Param-Name> defaultencoding </param-Name>

<Param-value> UTF-8 </param-value>

</Init-param>

</Filter>

<Filter-mapping>

<Filter-Name>

Set web application character encoding

</Filter-Name>

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

</Filter-mapping>

========================================================== =====

Setencodefilter. Java
Public class setencodefilter implements filter {


Protected filterconfig = NULL;


Protected string defaultencoding = NULL;


/** // * (Non-javadoc)

* @ See javax. servlet. Filter # Init (javax. servlet. filterconfig)

*/

Public void Init (filterconfig arg0) throws servletexception {

// Todo auto-generated method stub

This. filterconfig = arg0;

This. defaultencoding = filterconfig. getinitparameter ("defaultencoding ");

}


/** // * (Non-javadoc)

* @ See javax. servlet. Filter # dofilter (javax. servlet. servletrequest, javax. servlet. servletresponse, javax. servlet. filterchain)

*/

Public void dofilter (

Servletrequest request,

Servletresponse response,

Filterchain chain)

Throws ioexception, servletexception {

// Todo auto-generated method stub

Request. setcharacterencoding (selectencoding (request ));

Chain. dofilter (request, response );

}



Public void destroy (){



This. defaultencoding = NULL;

This. filterconfig = NULL;

}



Protected string selectencoding (servletrequest request ){


Return this. defaultencoding;

}


}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.