JAVA/J2EE the ultimate solution to Chinese problems

Source: Internet
Author: User
Tags filter character set header mysql linux
j2ee| Solution | problem | Chinese Java Chinese problem has been plagued by many beginners, if you understand the Java system in Chinese problem principle, we can take a fundamental solution to the Chinese problem.

The oldest solution is to use string bytecode conversion, which is inconvenient, and we need to break the encapsulation of the object and do bytecode conversion.

There is another way to the Java-EE container coding settings, if the application system from the Java-EE container, it will be garbled, and the specified container configuration does not conform to the application of Java EE and the principle of separation of containers.

In the Java internal operation, all the strings involved are converted to UTF-8 encoding for operation. So, before being translated into Java, what kind of character set is the string? Java always determines the initial encoding of strings based on the operating system's default coded character set, and the input and output of the Java system is the default encoding of the operating system.

Therefore, if the unified Java system input, output and operating system 3 of the coded character set, will be able to enable the Java system to correctly handle and display Chinese characters. This is a principle to deal with the Chinese characters in the Java system, but in the actual project, it is difficult to grasp and control the input and output parts of the Java system correctly. Java EE, because it involves external browsers and databases, so the Chinese problem garbled is very prominent.

The Java EE application is run in the Java EE container. In this system, there are many types of input: one is sent to the server through the page form Baocheng requests (request), the second is read through the database, and the 3rd kind of input is more complicated, the JSP is always compiled into servlet,jsp in the first run and often contains Chinese characters, Then when compiling using Javac, Java will be encoded as the initial encoding based on the default operating system encoding. You can specify a default character set, such as in Jbuilder/eclipse, unless specifically specified.

There are several ways to output: The first is the output of JSP pages. Since the JSP page has been compiled into a servlet, the output encoding is also selected according to the operating system's default encoding, unless the output encoding is specified, and the output path is the database, which outputs the string to the database.

From this, the input and output of a Java EE system is very complex, and dynamic changes, and a cross-platform operation, in the actual compilation and operation, may involve different operating systems, if let the Java free according to the operating system to determine the input and output of the coded character set, This will not be controlled to appear garbled.

It is because of the cross-platform nature of Java that the character set problem must be solved by the specific system, so in a Java application system, the fundamental way to solve the Chinese garbled is to explicitly specify the uniform character set of the whole application system.

Specify Iso8859_1, GBK, or UTF-8 when specifying a uniform character set?

(1) If the unified designation as Iso8859_1, because most of the software is currently developed by Westerners, their default character set is Iso8859_1, including operating system Linux and database MySQL and so on. In this way, if you specify Jive Unified encoding for Iso8859_1, then there are 3 links that must be grasped:

Specifies the character set as iso8859_1 when developing and compiling code.

The default encoding for running the operating system must be iso8859_1, such as Linux.

In the JSP header statement:.

(2) If the unified designation for the GBK Chinese character set, the above 3 links also need to do, the difference is only run in the default encoding for GBK operating system, such as Chinese windows.

Unified coding for Iso8859_1 and GBK is convenient for coding, but they can only run on the appropriate operating system. But it also destroys the advantages of Java Cross-platform operation, and it is only in a certain range of experts. For example, to enable GBK encoding to run on Linux, set up Linux encoding as GBK.

Is there a basic solution for Chinese encoding that does not require any additional settings other than the application system?

The unified coding of the JAVA/J2EE system is defined as UTF-8. UTF-8 encoding is a compatible encoding for all languages, the only trouble is to find all the entrances to the application system and then use UTF-8 to "tie" it.

A Java EE application system needs to do the following steps:

Specifies the character set as UTF-8 when developing and compiling code. Both JBuilder and Eclipse can be set in the project properties.

Using a filter, if all requests are passed through a servlet control allocator, use the filter execution statement of the servlet to convert all requests from the browser (request) to UTF-8 because the request package sent by the browser is encoded according to the operating system on which the browser resides. may be various forms of coding. Key sentence:

Request.setcharacterencoding ("UTF-8").

There is the source of this filter on the Internet, Jdon framework source code in Com.jdon.util.SetCharacterEncodingFilter
You need to configure Web.xml to activate the filter.

In the JSP header statement:.

In the JSP's HTML code, declare UTF-8:

Setting the database connection method is UTF-8. For example, when connecting to MySQL, configure the URL as follows:

Jdbc:mysql://localhost:3306/test?useunicode=true&characterencoding=utf-8
General database can be set by admin settings UTF-8

Other interacting with the outside can be set when the encoding set UTF-8, such as reading files, operating XML and so on.

The author used to take this principle in Jsp/servlet, and later use struts, Tapestry, EJB, Hibernate, Jdon and other frameworks, has never been garbled, can be said to be suitable for a variety of architectures. It is hoped that this program will be shared by more beginners, reduce the first stumbling block of JAVA/J2EE, and avoid the adoption of some temporary solutions, resulting in Chinese problems always appearing in the new technical framework.



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.