In general, we can choose to set mysql encoding during installation, but it is not as convenient as windows in liunx. Next I will introduce how to modify mysql encoding in liunx.
# Vi/etc/my. cnf
Open my. cnf and modify the 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 do not know where MYSQL is, run
The Code is as follows: |
Copy code |
# Whereis mysql |
You can find the location where MYSQL is installed.
To check whether the setting is successful, 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 preceding Code settings are successfully displayed.
Add the following statement to the JSP page (preferably all pages) that sends or reads Chinese characters:
The Code is as follows: |
Copy code |
<% @ Page pageEncoding = "gbk" %> <% @ Page contentType = "text/html; charset = gbk" %> <% Request. setCharacterEncoding ("gbk"); %>
|
Add the following to the
The Code is as follows: |
Copy code |
<Meta http-equiv = "Content-Type" content = "text/html; charset = gbk">
|
Note that the methods for responding to requests in servlet must also include:
The Code is as follows: |
Copy code |
Response. setContentType ("text/html; charset = gbk ");
|
(If the request sent by firefox is still latin1 for unknown reasons)
Before inserting the request parameters into the database, you must perform another transcoding (convert latin1 encoding to UTF-8), 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, and can be transcoded every time before the request data is inserted.