Solution to Chinese garbled JSP found in MySQL Manual

Source: Internet
Author: User
Tags mysql manual

There are many solutions to JSP Chinese garbled characters. First, a StringConvert beanGBtoISO () and ISOtoGB () methods are implemented to solve the problem of some Chinese garbled characters when interacting with the MySQL database: Reading Chinese MySQL content in the JSP program, these two methods can solve the garbled problem.

However, Chinese content written from JSP to MySQL is garbled and displayed "?? ", Here we should see the loss of character information during the encoding conversion process. Sadly, after logging on to MySQL in the command line window, execute "insert into customer VALUES ('character ',...) "when such a statement is written to the data table, the Chinese content is displayed normally !!! The character set used by the database is utf8.

After hitting the wall multiple times, I finally found a solution: when I checked the MySQL manual, I saw a statement like this: To allow multiple character sets to be sent from the client, the "UTF-8" encoding shoshould be used, either by grouping "utf8" as the default server character set, or by locking ing the JDBC driver to use "UTF-8" through the characterEncoding property.

In addition, you can use this syntax in the query statement to convert a string to a given character set: _ charset str.

Charset must be a character set supported by the server. In this example, the default character set used by the shopdb database is utf8, so the test is started:

Enter INSERT into publish Values ('8', _ gb2312 'Higher Education Press') to write the data "?? "

Try INSERT into publish Values ('8', _ gbk 'Higher Education Press ').

INSERT into publish Values ('8', _ utf8' Higher Education Press '). This is even more simple and there is nothing !!

JSP Chinese garbled characters still exist. No way. Use the show character set; command to view the character sets supported by MySQL. I thought I would try it all again. Browse a bit, found that there are not a few familiar character set, there is only one latin1 (ISO-8859-1) is more common, it won't be it, a try really is.

INSERT into publish Values ('8', _ latin1 'higher education publishers.

This finally found the JSP Chinese garbled problem solution, Tomcat under the configuration of the database connection pool url to "... characterEncoding = UTF-8", and then write the database Chinese content

String s2 = new String (s1.getBytes ("gb2312"), "ISO-8859-1") for transcoding, where s1 is a Chinese String. Then write to the database and everything is normal.

In order to solve this problem, I checked n multi-data. Now I will make a summary: because of the differences in character sets and character encoding methods, we can transfer data between OS and programs, especially the data in multiple character sets) garbled characters and character information are lost. the key to solving this problem is to understand the character sets and character encoding methods used at the data output end and the receiver end. If the two encoding methods are different, transcoding is required at the data exit or entrance. Generally, character data is transmitted during code writing, compilation, and running, so you need to be careful.

When writing code, you may use a development tool, such as the Eclipse. maybe everything works normally during writing, but once you open the document again after saving it, all Chinese characters become garbled. This is because these character data are stored in a stream in the memory during writing and OK. This is OK, but the data in this stream will be written to the hard disk during storage, the use of your development tool is the default encoding method, if unfortunately your development tool default encoding method is ISO-8859-1, Chinese character information cannot be properly stored. In Eclipse, you can view and modify the default character encoding method: Project-> Properties-> info, where "default
Encoding for text file ". If it is set to GBK, write the code and save it.

For JSP programs, after the code is compiled, it is handed over to the Container. First, they will be converted. java file, and then compiled. class can be submitted to the server for execution. this process also has character encoding problems. the java compiler (javac) uses the language Environment of the operating system as the default character encoding method, as does JRE (Java Runtime Environment. Chinese characters can be correctly displayed only when the encoding method of the compiling and running environment is the same as that of the source file. Otherwise, you need to perform transcoding at runtime to make them use compatible encoding. The settings here can be divided into several layers: The Language supported by the operating system layer, which is the most important, because it will affect the default JVM character encoding mode and display the characters at the same time, for example, fonts have a direct impact. On the J2EE server layer, most servers can customize character encoding. For example, Tomcat can use the web. set the javaEncoding parameter in xml to set the character encoding, which defaults to the UTF-8.

IE can also be set to always send requests using UTF-8 encoding. at the application layer, each program configured on the server can set its own encoding method. I have not used this method yet and will study it later.

During transcoding and running, applications may need to interact with external systems, such as reading and writing databases and reading and writing external files. in these cases, applications are inevitably required to exchange data with external systems. For Chinese characters, the encoding method of data entrances and exits is particularly important, which is often the main cause of Chinese garbled characters in JSP ). In general, the external system has its own character encoding method, and MySQL configured in my example is the UTF-8 encoding used. On the JSP page, set "charset = gb2312 ",

When using gb2312 encoding, explicit transcoding is required when it interacts with the database to correctly process Chinese characters.

  1. JSP Chinese garbled Problem description and Solution
  2. Example Description: how to print a report using JSP
  3. Use JSP to print the report Word and Excel)
  4. Brief Introduction to JSP Technology
  5. Introduction to JSP Technology

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.