#vi/etc/my.cnf
Open MY.CNF Modify Encoding
Vi/etc/my.cnf
Add under [mysqld]
| The code is as follows |
Copy Code |
Default-character-set=utf8 or DEFAULT-CHARACTER-SET=GBK. Add under [client] Default-character-set=utf8 or DEFAULT-CHARACTER-SET=GBK. |
Restart MySQL.
If you don't know where MySQL is, run
| The code is as follows |
Copy Code |
| #whereis MySQL |
You can find where MySQL is installed.
If you want to check if the settings are successful, you can run the following command:
| The code is as follows |
Copy Code |
# mysql-u Root-proot Welcome to the MySQL Monitor. Commands End With; or G. Your MySQL Connection ID is 2 to server version:5.0.22 Type ' help, ' or ' h ' for help. Type ' C ' to clear the buffer. Mysql> Show variables like '%char% '; +--------------------------+----------------------------+ | variable_name | Value | +--------------------------+----------------------------+ | character_set_client | GBK | | character_set_connection | GBK | | Character_set_database | GBK | | Character_set_filesystem | binary | | Character_set_results | GBK | | Character_set_server | GBK | | Character_set_system | UTF8 | | Character_sets_dir | /usr/share/mysql/charsets/| +--------------------------+----------------------------+ 8 rows in Set (0.00 sec) |
The above has already shown that the encoding setup was successful.
Add the following statement to the JSP page (preferably all pages) where you want to send Chinese or read Chinese:
| The code is as follows |
Copy Code |
<% @page pageencoding= "GBK"%> <%@ page contenttype= "TEXT/HTML;CHARSET=GBK"%> <% request.setcharacterencoding ("GBK"); %>
|
Add the following in the
| The code is as follows |
Copy Code |
<meta http-equiv= "Content-type" content= "text/html; CHARSET=GBK ">
|
Note that the same method of responding to requests in the servlet is:
| The code is as follows |
Copy Code |
Response.setcontenttype ("TEXT/HTML;CHARSET=GBK");
|
(This step is performed because the request that Firefox sends is still latin1 for unknown reasons)
Before inserting the requested parameter into the database, another transcoding (converting the latin1 encoding to Utf-8) is performed, for example:
| The code is as follows |
Copy Code |
String utf8_str = new String (latin_str. GetBytes ("Latin1"), "GBK");
|
This step can be written in a filter, not afraid of trouble or you can make a transcoding every time before inserting the request data.