Recently oneself busy to do a small project, build the Environment framework Ssh+mysql database, encountered a problem: JSP page Chinese display garbled, database insert data and update data when the Chinese also show garbled, later on the internet to find a lot of solutions, or toss two days to solve the problem, The following summarizes the complete solution of Chinese garbled problem:
In the project I use the UTF-8 encoding method uniformly
1, know that you install the MySQL database installation configuration Choose what encoding method, if installed before, it is not clear now, it is recommended to reinstall MySQL database, install the configuration when the choice of UTF8
(Here's a tip: instead of uninstalling MySQL data, find the installation MySQL database directory: D:\MySQL Server 5.1\ MySQLInstanceConfig.exe under Bin, double-click Run can be reconfigured to install, and select UTF8 encoding method),
2, after the successful installation, look at the encoding method, SQL command as follows:
Show variables like ' character% ';
All coding methods here must be unified for UTF8, if one is not UTF8 encoding, it is necessary to set the Utf8,sql command as follows: Set Character_set_results=utf8
3. Create a database and specify how the database is encoded, SQL commands are as follows:
Create database Yourdb character set UTF8;
4. Create a database table, and also specify how to encode it
5. If your MySQL version is below 5.0, you also need to specify the encoding of the database access connection in the Hibernate.cfg.xml configuration URL:
such as:url=jdbc:mysql://localhost/db?user=user&password=123456&useunicode=true& Characterencoding=utf8
If the MySQL version is more than 5.0, the URL here does not add? user=user&password=123456&useunicode=true&characterencoding= UTF8 's okay.
6, write a character encoding filter, encoding method must be consistent with the MySQL character set is UTF-8
Note: When you configure this filter in Web. XML, you must place the <filter-mapping> of the filter in front of the struts map, or it will not be filtered
7, all JSP pages to specify the encoding format, to ensure that the MySQL character set is consistent with UTF-8
Note: The page is uniform in case, such as <%@ page pageencoding= "UTF-8" contenttype= "text/html; Charset=utf-8 "%>, cannot be pageencoding=" UTF-8 ", contenttype=" text/html; Charset=utf-8 ", add data page also to display data page encoding format to be consistent, otherwise it will appear to be added to the database is not garbled, from the database display garbled situation
How to completely solve the JSP page Chinese garbled and database garbled