operate
We get the data from the front end and store it in the database. Here, postman is used to simulate the value transfer from the front end to the background, as follows:
Questions raised:
The record is added successfully, but the newly added record has garbled characters in Chinese, as follows: Obviously
, this does not meet the effect we want, so how to solve this problem?
Solution steps
1. Identify the problem
Step1: Check the database character encoding
- Select the database and
->右键 ->数据库属性
confirm that the character encoding of the database is utf-8.
- Check the character encoding of the database table,
选中数据库表->右键->对象信息
, confirm that the character encoding of the database table is utf-8
Step2: After we have checked the database and there is no problem, the next step is to debug what is wrong with the background program. Start the server and enable debug mode.
A problem has arisen~ You can see that when the request gets the parameters, the characters are already garbled, so the value stored in the database is also garbled~
2. Solve the problem
Let's check the server.xml configuration file of the tomcat server.
Found that there is no character encoding set
<Connector connectionTimeout="20000" port="8088" protocol="HTTP/1.1" redirectPort="8443"/>
So, let's try to add attributes Connector
in this tagURIEncoding
<Connector URIEncoding="UTF-8" connectionTimeout="20000" port="8088" protocol="HTTP/1.1" redirectPort="8443"/>
At this time, we restart the server, use the debug mode to debug our program again, and find that we have successfully obtained Chinese~
At this point, we check the records stored in the database, and the stored values are normal~perfect solution~