Solution for garbled Chinese Characters in JSP + MySQL Input and Output!

Source: Internet
Author: User
Tags mysql code
When the JSP page is used to add Chinese data to the MySQL database, it is found to be garbled or garbled when reading Chinese data from mysql. The root cause of these problems is the inconsistency of character encoding. We need to know the source of the disease before we can take the right medicine, so I will briefly describe the problem generation process (if any, please kindly advise me)
Here is an example of reading data from the form and writing data to the MySQL database;

HTML page parameters-(sent in browser encoding mode)-"JSP page-(compiled in JSP-defined encoding mode)-" are compiled by the JSP Engine *. class file -- run in JSP Container -- pass to MySQL database-(read and store data by database encoding)-read mysql-(read by database encoding) -The JSP page is displayed in the browser after it is accepted (decoded by the browser );

Note: The "browser encoding", "JSP page encoding", "JSP page output encoding", "JSP page input encoding", and "database encoding" may not always exist, if any of them is different, garbled characters may appear. Just like a messaging game, James needs to pass the words to Lee IV, and Lee needs to send the words he heard to Wang Wu, to let Wang Wu know what James said, he must use the same language as the three of them. If Jack is a Martian, he certainly cannot understand what James said, then he said nothing to Wang Wu about Mars (garbled characters). As a result, Wang Wu could only hear garbled characters. (Compare to sweat ~~ ^_^ !)

Now we can change the encoding methods to the same one;
// ------------------------- Browser code -----------------
<HTML>
<Head> <meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"> </Html>
// --------------------------- JSP page -------------------
<% @ Page pageencoding = "gb2312" %> // JSP page code:
<% @ Page contenttype = "text/html"; charset = gb2312 "%> // JSP page output code:
<% Request. setcharacterencoding ("gb2312"); %> // enter the encoding on the JSP page:
// ------------------------ MySQL code -------------------
Connection connect = java. SQL. drivermanager. getconnection (
"JDBC: mysql: // localhost/mydb? User = login account & Password = your password & useunicode = true & characterencoding = gb2312 ");
//-------------------------------------------------------
Note: The gb2312 writing method (the red part) should be differentiated in the browser and database!
The configuration is successfully tested! (Tomcat5.5 + mysql5.0)
// I have read a lot of data online. Article For example, function transcoding ~~ Modify the configuration file ~~ It is not as simple as this and the success rate is high!
Recommended!

 

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.