http://www.blogdriver.com/strive/283538.html (Picking)
Before resolving garbled problems, you must first figure out several related issues.
Set its encoded format (UTF-8) for each JSP page, but still displayed as garbled when data to another page?
First of all, we must understand that the default code of the web container is ISO-8859-1, one Chinese character takes up two bytes, and one Chinese character takes up three bytes in UTF-8. Therefore, during the data transfer process, you must manually set the container coding format, otherwise the word position is lost. That is, 3 bytes become two bytes, naturally become garbled.
Why is the data saved in the database becomes garbled?
In principle, first, we must first understand the default encoding format used by the database, such as MySQL default character encoding is ISO-8859-1. So you must manually modify its default encoding format.
After clearing the above issues, then see how to solve the problem of garbled code of UTF-8.
1. Set the encoding format of the web container. Add the following code for your servlet's Doget or dopost method:
Request.SetCharacterencoding ("UTF-8"); Response.setCharacterencoding ("UTF-8");
2. Specify its encoding format for each JSP page. <% @ Page PageEncoding = "UTF-8"%>
3. Add: useunicode = true; characterEncoding = UTF-8 is as:
URL = "JDBC: mysql: /// DB1? Useunicode = true; characterencoding = UTF-8",
4. To specify the database default encoding format. Locate the My.ini file in the C: / Windows directory, and add default-character-set = GBK in [MySQLD] to restart the MySQL service.
At this point, all garble questions are all resolved. At first, I got up. Why set the default encoding format of MySQL to GBK, and later learned that the UTF-8 default input coding method is GBK, the default output encoding is UTF-16BE.
Personally, the purpose of UTF-8 is encoded (the Chinese code is%), which is mainly to prevent data loss (such as MSN) when data transmission between multi-layer services. Ordinary web programs are clearly not necessary, as long as the above methods are used, they can effectively solve garbled problems, thus setting storage space. If all like the company's SC system, after the Chinese characters are encoded into 18 characters, then store it in the database, and the outlet efficiency does not say, the waste of the storage space is enough, know that it is a silver land. !