1. Causes of Chinese garbled text
The character set used for encoding does not match the character set used for decoding.
2. Locations where garbled text may occur during web development and solutions
① Request garbled text
When transmitting data to the server, the transmitted Chinese characters may become garbled.
POST request (protocol body)
Solution:
request.setCharacterEncoding("UTF-8");
GET request (address bar)
Solution: Additional configuration is required in Tomcat (I use the Tomcat server).
Configure the following code in Tomcat's conf/server.xml:
connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8"
② Response garbled text
If Chinese characters appear in the dynamic web pages returned to the client, garbled text may occur.
Solution:
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8")