Solve the problem of storing Chinese data in database garbled, garbled pages, etc.

Source: Internet
Author: User
Tags i18n

When a database or page appears garbled in Chinese, it is often necessary to analyze it from the following places

1. Page encoding

2. Database connection statement (if the data connection's properties file encoding inconsistent with the database encoding format will result in data stored in the database garbled regardless of Chinese and English consent garbled)

    • Set characterencoding to UTF-8 such as jdbc.mysql.url=jdbc:mysql://localhost:3306/db?useunicode=true& Characterencoding=utf8
    • If you use Hibernate, change the encoding format of all configuration file headers to UTF-8.

3.Tomcat

    • In order to ensure that the Get/post data uses the same UTF8 encoding, we set the following settings in Server.xml: <connector port= "8080" protocol= "http/1.1" Connectiont imeout= "20000" redirectport= "8443" uriencoding= "UTF-8"/>

4. STRUTS2 Configuration

    • Struts2 add Struts.i18n.encoding=utf-8 to the configuration struts.properties file or add <constant name= in Struts.xml Struts.i18n.encoding "value=" UTF-8 "/>

Configure the filter in the Web. xml file

<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

5. Database (MySQL)

You can first enter the query statement show VARIABLES like ' character_set_% ';, to see if all the encodings are UTF-8.

If you are not able to use the Server Instance Config to set the default character set to Utf-8 or modify DEFAULT-CHARACTER-SET=GBK in/mysql/mysql Server 5.0/my.ini

CHARACTER-SET-SERVER=GBK;

And then restart the MySQL service.

Solve the problem of storing Chinese data in database garbled, garbled pages, etc.

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.