How to solve Java/J2EE Chinese problems

Source: Internet
Author: User

How to solve Java/J2EE Chinese problems

Most programmers have encountered Java Chinese problems in programming, but as long as you understand the Chinese problem principle of the Java System, we can say goodbye to Chinese problems.

The oldest solution is to use String bytecode conversion, which is inconvenient. We need to break the object encapsulation and perform bytecode conversion.

Another method is to encode the J2EE container. If the J2EE application system leaves the container, garbled characters will occur, and the specified container configuration does not conform to the principles of J2EE application and container separation.

In Java internal operations, all strings involved are converted to UTF-8 encoding for computation. So what kind of character set is a string before it is converted to Java? Java always determines the initial encoding of a string based on the default encoding Character Set of the operating system, and the input and output of the Java System are all encoded by the default of the operating system.

Therefore, if we can unify the character set of the input, output, and operating system of the Java System, the Java System can correctly process and display Chinese characters. This is a principle for processing Chinese characters in the Java System, but in actual projects, it is difficult to grasp and control the input and output parts of the Java system correctly. In J2EE, because external browsers and databases are involved, Chinese garbled characters are very prominent.

A J2EE application runs in a J2EE container. In this system, there are many input methods: one is to package a page form into a request) to the server, the other is to read data through the database, and the other 3rd types of input are more complex, JSP is always compiled into Servlet at the first run. JSP often contains Chinese characters. When javac is compiled, Java uses the default Operating System encoding as the initial encoding. Unless otherwise specified, for example, you can specify the default character set in Jbuilder/eclipse.

There are also several ways of output: the first is the output of JSP pages. Since the JSP page has been compiled into a Servlet, the output encoding will also be selected based on the default encoding of the operating system during the output, unless the output encoding method is specified, and the output path is the database, output the string to the database.

From this point of view, the input and output of a J2EE system are very complex and dynamic, while Java runs across platforms. In actual compilation and running, different operating systems may be involved. If Java is allowed to determine the encoding Character Set of the input and output based on the operating system, this will lead to uncontrollable garbled characters.

Java is a cross-platform language, so that the character set problem must be solved by a specific system. Therefore, in a Java application system, the fundamental solution to Chinese Garbled text is to specify the unified character set of the entire application system.

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.