Solve the Problem of JSP + MySQL multi-language text encoding with UTF-8

Source: Internet
Author: User

Solve the Problem of JSP + MySQL multi-language text encoding with UTF-8

This problem was solved by myself after several days of hard work. For reprinting, please note the source:
Http://blog.donews.com/ralix/

The following are some notes:
At any time in the file UTF-8, UTF-8, utf8, utf8 its role is the same. Tomcat, resin, and MySQL are not as stupid as we are. Many articles on the Internet have pointed out their differences, which is purely nonsense.

My software environment:
Chinese Windows XP (Linux can be modified accordingly)
Apache Tomcat 5.5.6 Server
PhpMyAdmin 2.6.4-pl4
MySQL 5.0.19-NT

Follow these steps:

1. Correctly display the UTF-8 In the JSP static file:

1. jsp files are edited with a text editor that supports UTF-8, such as editplus, ultraedit, and convert the original ASCII to UTF-8 (available in the menu ).

2. Change the JSP file header:
<% @ Page contenttype = "text/html; charset = UTF-8" Language = "Java" Import = "Java. SQL. *" %>
There is no such sentence (but it is necessary in the .html file ):
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
</Head>

Ii. Correct submission of form:

1. Before getparameter, add:
Request. setcharacterencoding ("UTF-8 ");
You can also use the filter method in this step. See other articles.

2. For a request, you only need to write it in the simplest form:
String name1 = new string (request. getparameter ("username "));
String pwd1 = new string (request. getparameter ("password "));

3. Connect to MySQL:

1. MySQL configuration file:
Modify MySQL's default-character-set = UTF-8 in Windows/My. ini.

2. MySQL Databases and tables are also set to utf8_unicode_ci.

3. Database Connection: JDBC: mysql: // localhost/mydb? Useunicode = true & characterencoding = UTF-8
Note: '&' is not '& amp;' in the middle of this sentence. '& amp;' is written in banq at www.jdon.com and many articles by cool people ;'. This may be automatically converted to HTML when an article is published. But it does bring us a lot of trouble. The following error or question mark is returned:
Java. SQL. sqlexception:
Syntax error or access violation, message from server: "You have
Error in your SQL syntax; check the manual that corresponds to your
MySQL Server version for the right syntax to use near ''asdf ''at line
1"

At this point, Chinese characters are displayed on the page, and Chinese characters are displayed, inserted, and modified from the database.
And because it is UTF-8, you can try Inputting Chinese, English, Japanese, and Korean at the same time in form.

PS:
Although I have not fully tested it, the encoding of the operating system, JDK, tomcat, or resin in my use does not affect this problem.
During the test, the JDK displayed by resin Initialization is GBK and ZH, but there is no problem.
In addition, the. Java file is UTF-8 or ASCII encoded, And the generated. class can be used as well.

As a reminder from a friend downstairs, I need to add:
1. During database connection, the & sign should be used in the. jsp and. java files, and the & amp; should be used in the XML file, which is also the intention of banq. It's just that we couldn't comprehend it directly.
2. In my test, connector-J is already above 3.0, but if useunicode = true & characterencoding = UTF-8 is not added to access the database, there will still be problems. Therefore, we recommend that you add.

PS2:
Modify tomcat/CONF/server. xml
<Connector acceptcount = '000000' connectiontimeout = '000000'.../> Add uriencoding = 'utf-8'

PS3:
Recently, MySQL must be written as utf8 in Windows/My. ini, which corresponds to the encoding in MySQL/share/charsets.

All done, language.

 

------------------------------------------------- The above is an online reprinted ----------------------------------------

 

Supplement:

If it is a GET request, the request uses the container's default encoding method, so the parameter encoding will go wrong. For post, the encoding of the Request Source Page prevails. Therefore, if you want to use a GET request, modify the Tomcat server. XML

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.