To solve the JSP garbled code, you must first understand the reasons for the JSP garbled code.
1. When setting up the server and installing MYSQL, you will be asked to choose an encoding. If this encoding is inconsistent with your web page, it may cause the JSP page to be garbled.
2. When creating a database in systems such as PHPMYADMIN or mysql-front, you will be asked to choose an encoding. If this encoding is inconsistent with your web page, it may also cause garbled JSP pages.
3. When creating a table, you will be asked to choose an encoding. If this encoding is inconsistent with your web page encoding, it may also cause the JSP page to be garbled.
4. You can choose the encoding when adding fields when creating a table. If this encoding is inconsistent with the encoding of your web page, it may also cause the JSP page to be garbled.
5. The encoding of the JSP page submitted by the user is inconsistent with the encoding of the JSP page that displays the data, which will definitely cause the JSP page to be garbled.
For example, the JSP page where the user inputs data is in big5 code, but the JSP page where the user input is displayed is in gb2312. This will 100% cause the JSP page to be garbled.
6. Please note that the character set is incorrect:
1. The text you usually see on some websites may have several encodings. For example, if you see a traditional Chinese character, it may be big5 encoding, it may be utf-8 encoding, or it may be gb encoding. That's right, that is to say, there are traditional Chinese characters encoded in simplified Chinese, and there are also simplified Chinese characters encoded in traditional Chinese. You must understand this.
If you are making a webpage with simplified Chinese encoding, the encoding is set to GB2312. If visitors from Hong Kong and Taiwan submit traditional Chinese information, it may cause garbled codes. The solution is (1) Set the website encoding to utf-8, so that It is compatible with all characters in the world. (2) If the website has been running for a long time and has a lot of old data, and the Simplified Chinese settings cannot be changed, it is recommended to set the page encoding to GBK.
The difference between GBK and GB2312 is that GBK can display more characters than GB2312. To display traditional characters in simplified code, you can only use GBK
7.The encoding specified in the JSP connection statement to the MYSQL database is incorrect.
8. If the JSP page does not specify the encoding for data submission, it will cause garbled code:
Therefore, the reasons for JSP garbled code are nothing more than the above. After knowing the reasons, it is much easier to solve the JSP garbled code. Let us express them one by one:
1. If the code for installing MySQL cannot be changed, many friends buy virtual hosts to build websites and do not have the right to change the installation code for MYSQL. We can skip this step, because as long as the following steps are correct, the garbled code problem can still be solved.
2. Modify the database encoding. If the database encoding is incorrect: You can execute the following command in phpmyadmin: ALTER DATABASE `test` DEFAULT CHARACTER SET utf8 COLLATE utf8_bin
The above command is to set the encoding of the test database to utf8
3. Modify the encoding of the table: ALTER TABLE `category` DEFAULT CHARACTER SET utf8 COLLATE utf8_bin
The above command is to change the encoding of a table category to utf8
4. Modify the encoding of the field:
ALTER TABLE `test` CHANGE `dd` `dd` VARCHAR( 45 ) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL
The above command is to change the field encoding of dd in the test table to utf8
5. If this situation is easy to solve, just check the page and modify the charset of the source file.
, //If this is correct, there will be no problem
6. In this case, you can also modify the page charset.
7. In the JSP statement to connect to the database,
private String url="jdbc:mysql://localhost/"+DB_NAME+" user="+LOGIN_NAME+"&password="+LOGIN_PASSWORD+"&characterEncoding=GBK"; //The key depends on characterEncoding
8. In this case of garbled characters, just add request.setCharacterEncoding("GBK") at the beginning of the page; just specify the submitted
data. Note: After modification according to the above method, you can only ensure that your newly inserted data will not be garbled. For example Example: If the data your user has submitted is BIG5, but you want to use the above method to change it to be displayed correctly on the GB2312 web page, it is impossible. This kind of text code transformation can only be solved by writing another program, please WEB745 .com other related articles
Summary: First, you need to determine whether the JSP garbled characters are garbled after being inserted into the database, or whether the JSP page is garbled just after it is submitted. If the JSP is garbled just after receiving the data from the previous page, it must be that the character set is wrong, but request.setCharacterEncoding is not added. ("GBK");
If it is due to the database, please refer to the 2, 3, and 4 methods above.
It may be an encoding problem. Connect to the database like this and see.
jdbc:mysql://localhost:3306/xxxx useUnicode=true&characterEncoding=UTF-8
See if it works