During development, various Chinese garbled characters are always encountered. The following is a summary.
1. Database garbled characters
In case of garbled characters, the first response should be: whether there is a problem with the character encoding settings of the database. Take mysql as an example. After installing mysql, enter the installation directory and open the my. ini configuration file to view the encoding set by mysql. In my. ini, there are two character encoding settings: default-character-set in [mysql] and default-character-set in [mysqld. Anyone familiar with linux knows that the end of "d" indicates the server end. As you want, the character encoding set for [mysql] is the client end, [mysqld] The character encoding is the mysql server-side encoding. Our data is stored on the mysql server. In order to store any character encoding data, we generally set default-character-set = utf8 under [mysqld], not UTF-8 ). How to set the default-character-set under [mysql] on the client? A friend may also set it to utf8, which may cause problems. In this case, when we run cmd to open the DOS window and run mysql-uroot-p123 to enter the mysql client, we cannot insert Chinese characters. The reason is simple. This is because the character encoding in our operating system is GBK (or GBK compatible). We enter Chinese characters in the DOS window (for example, insert into test values ('China ')) naturally, it is GBK, But we set utf8 for [mysql], which may cause errors due to incompatibility. Of course, if we use the relevant mysql GUI client tool for related settings, even if [mysql] is set to utf8, it is okay. However, if you log on to the mysql client through the DOS window, set default-character-set = GBK under [mysql.
Conclusion: [mysql] default-character-set = GBK, [mysqld] default-character-set = utf8
2. garbled GET requests
If a request is in the GET mode, for example, if the request specified by the <a href = "url"> tag is submitted as a form in the GET mode, we can see a request similar to http in the address bar of the browser: // localhost/proj/. do? Title = % C4 % E3 % BA % C3. Like this two (% plus 2 hexadecimal numbers) represents a Chinese character, which is encoded with a ISO8859-1 (I .e. lantin1. We cannot solve this by setting request. setCharacterEncoding ("UTF-8.
There are two solutions:
The first method is to use title = new String (title. getBytes ("ISO8859-1"), "UTF-8"); to decode each request parameter with Chinese characters, this method is cumbersome;
The second method is to modify the tomcat server. xml configuration file, put <Connector port = "8080"...> change to <Connector port = "8080 "... URLEncoding = "UTF-8">, that is, specify its URLEncoding = "UTF-8" in the <Connector> element, which defaults to lantin1. This property specifies the URL encoding, after set to UTF-8, the address bar will no longer appear a. do? Title = % C4 % E3 % BA % C3, but a. do? Title = Chinese.
The disadvantage of this method is that other projects or other parts of the project may introduce new Chinese encoding problems due to modifications. For example, after using this method, in some cases, if the first method is used, the processed Chinese characters will be garbled again. This method is not commonly used and may trigger the whole body.
3. garbled characters in POST requests
For form requests submitted in POST, you can use request. setCharacterEncoding ("UTF-8. For the reason why this method is valid for POST requests and invalid for GET requests, see the difference between GET and POST requests in HTTP. In short, the content of the GET request is placed in the request header, POST requests are placed in the Request body. In general, we can add a character set filter to the project to solve Chinese garbled characters in POST requests at one time. The content in the filter is very simple, that is, the request. setCharacterEncoding (encoding) is called, and the encoding is the character set we want to set. You can write filters by yourself or use org. springframework. web. filter. CharacterEncodingFilter provided by spring.
To sum up, the general practice of Chinese Garbled text is as follows:
A. the cube uses UTF-8 instead of GBK
B. Add a character set filter to our application.
C. All Form forms are submitted using POST.
D. Use the link <a href = "a. do? Name = Chinese "> commit, it is best to use new String (title. getBytes (" ISO8859-1 ")," UTF-8 ") to solve instead of modifying the tomcat configuration file