Go deep into Java Chinese problems and the optimal solution-above

Source: Internet
Author: User
Go deep into Java Chinese problems and the optimal solution -- On
Source: csdn Author: abnerchai

(This article is the author of the original, the author contact address: josserchai@yahoo.com. Because the Chinese problem in Java programming is a common problem, after reading a lot of solutions to the Java Chinese problem, combined with the author's programming practices, I found that many of the methods mentioned in the past cannot clearly describe the problem and solve the problem, especially the Chinese problem during cross-platform communication. So I will give this article about the Chinese problems in the class, servelets, JSP, and EJB classes running on the console. I will analyze and suggest solutions. I hope you can advise me.

Abstract: This article provides an in-depth analysis of the Java compiler's coding/decoding process for Java source files and JVM class files in Java programming, through the analysis in this process, the root cause of Chinese problems in Java programming is identified. Finally, the recommended optimal solution to Java Chinese problems is provided.

1. Source of Chinese problems

The computer's initial operating system supports single-byte character encoding. Therefore, in the computer, all processing programs are initially processed in English based on the single-byte encoding. With the development of computers, in order to adapt to the languages of other nations in the world (including our Chinese characters, of course), we have proposed unicode encoding, which uses dual-byte encoding, it is compatible with double-byte encoding of English characters and other nationalities. Therefore, most international software currently adopts unicode encoding, it obtains the default supported encoding formats of the Local Support System (most of the time is the operating system), and then converts the Unicode in the software to the supported formats by the local system by default. The same is true for Java's JDK and JVM. JDK here refers to the international version of JDK. Most of our programmers use the international version of JDK, all of the following JDK versions refer to the international JDK version. Our Chinese characters are double-byte encoding languages. In order to allow computers to process Chinese characters, we have developed standards such as gb2312, GBK, and gbk2k to meet the requirements of computer processing. Therefore, most operating systems have customized Chinese operating systems to meet our Chinese processing needs. They use GBK and gb2312 encoding formats to correctly display our Chinese characters. For example, the Chinese Win2k adopts GBK encoding display by default. When saving a file in Win2k, the encoding format of the saved file is also GBK, that is, the internal encoding of all files stored in Win2k by default adopts GBK encoding. Note: GBK is extended based on gb2312.

Because the Java language uses unicode encoding internally, when Java is running, there is a problem of converting the encoding formats supported by Unicode encoding and the corresponding operating system and the browser, this conversion process involves a series of steps. If any of these steps fails, the displayed Chinese characters are garbled, which is a common Java Chinese problem.

At the same time, Java is a cross-platform programming language, that is, the programs we write can not only run on Chinese Windows, but also on Chinese Linux and other systems, at the same time, it is required to run on systems such as English (we often see that some people have transplanted Java programs written on the Chinese Win2k to English Linux to run ). This kind of porting operation will also cause Chinese problems.

In addition, some people use English operating systems, Internet Explorer and other browsers to run programs with Chinese characters and browse Chinese Web pages. They do not support Chinese characters and may also cause Chinese problems.

Almost all browsers PASS Parameters in the UTF-8 encoding format by default, rather than by Chinese encoding, so, when passing Chinese parameters will also have problems, resulting in garbled phenomenon.

In short, the above aspects are the main source of Chinese problems in Java. We call the problems caused by the failure of the program to run correctly due to the above reasons: Java Chinese problems.

2. detailed process of Java encoding and conversion

Common Java programs include:
* Classes that run directly on the console (including visual interface classes)
* JSP code class (Note: JSP is a variant of the servlets class)
* Servelets class
* EJB class
* Other support classes that cannot be directly run

These class files may contain Chinese strings, and we often use the first three types of Java programs to directly interact with users for output and input characters, such: we get the characters sent from the client in JSP and Servlet, which also contain Chinese characters. Regardless of the role of these Java classes, the lifecycle of these Java programs is as follows:

* The programmer selects an appropriate editing software on a certain operating system to implement the source code and. the Java extension is stored in the operating system. For example, you can use NotePad to edit a Java source program in Win2k;
* Programmers use javac.exe in JDK to compile the source code to form a. Class class (JSP files are compiled by the container by calling JDK );
* Directly run these classes or deploy these classes to Web containers for running and output the results.
In these processes, how does JDK and JVM encode, decode, and run these files?

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.