JSP and servlet to manipulate MySQL Chinese garbled problem solution

Source: Internet
Author: User
Tags mysql version mysql database

I do the test when used to Jsp/servlet to MySQL to write the data, but the Chinese are always garbled, this morning tangled up a long time to be done, to share my solution

First look at where to start from where the garbled, as long as the unified code, there will be no garbled, the following to Uft-8 (personally think the best) as an example, detailed description:

1, if the garbled is from the JSP page appears, JSP head page plus:

Add a label to the head tag.

2, if the garbled is in the servlet appears, there are two ways:

One is in each servlet doget and Dopost method head Plus

Request.setcharacterencoding ("Utf-8″");

The second most insurance, once and for all, is dedicated to write a filter class, also known as internationalization, class name Setcharacterencodingfilter content as follows

Then add the following code to the Web-inf web.xml:

Then add the following code to the Web-inf Web.xml: A that's it.

3, if there are garbled, is the problem of MySQL database

1 to ensure that the database when the database code selected is Utf-8, preferably in each table also specify the encoding format, mysql default is Latin1

2 If the MySQL version is more than 4.x, the database is still garbled, there are the following two ways to solve:

One is to specify the encoding in the code that connects the database:

String url = "JDBC:MYSQL://LOCALHOST:3306/TEST2?AUTORECONNECT=TRUE&USEUNICODE=TRUE&CHARACTERENCODING=GBK &mysqlEncoding=utf8″;

If it's still not working, it's

Show variables like ' collation_% ';

This command to view the default character set, if not Utf-8 My.ini (Windows) or MY.CNF (Linux) to modify the appropriate encoding to UTF8 after the restart of the MySQL server is OK

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.