Java Chinese problem-analysis

Source: Internet
Author: User
Question | What is the most difficult problem to handle in Chinese JSP? Younger brother in a few days ago want to install Jsp+javabean forum Acjspbbs, after studying for 3 days, understand the answer is Java to deal with the problem of Chinese.

Acjspbbs use of the character encoding is GBK, I used the Win2K system, the normal should be natural, but, unexpectedly was I found that its javabean in the database statement is used is the ISO8859-1 code, Directly resulting from the data from the database all garbled, including: Forum name constants, user posts title and content and so on.

In fact, within the Java system, all strings are uniformly UTF-8 encoded, and the operating system default encoding is used before the internal transformation, including input and output. To not garbled, only need "unified Java system input, output and the operating system of the character set", which is to deal with the Java system of Chinese characters a principle. The reason is that because of the cross-platform nature of Java, the character set must be given by the specific operating system.

This can be a simple way to solve the JSP garbled, which mainly need to grasp three links:
1, the development and compilation of code to specify a character set, such as Iso-8859-1 (Western system Most of the use of this)
2, determine the operating system of the character set and 1 unified, such as also with iso-8859-1, so the OS there are many, Linux is a
3, if the development of JSP, each page explicitly specify the character set:
<%@ page contenttype= "Text/html;charset=iso8859-1"%>

This achieves the previous goal and unifies the character encoding of the three.

Although this system is very good, there is no garbled problem, but it is obviously to destroy Cross-platform features for the price. To solve the problem fundamentally, it seems that only the Java system is unified into UTF-8 encoding, which requires finding all the entrances and exits.

Specific need to pay attention to the following aspects:
1, development and compile time specify character set as UTF-8
2, using the filter to convert all requests to UTF-8 character set encoding
If you are using a servlet to control the allocations, write them in the servlet filter
Request.setcharacterencoding ("UTF-8")
If you are using a JSP page, define it in the header
<%@ page contenttype= "Text/html;charset=utf-8"%>
3, on all the output including the database connection on the specified use of character set UTF-8

Also: If you are opening two times, you will need to recompile the package, specifying the use of UTF-8 encoding

Attached: The syntax for specifying character sets at compile time is javac-encoding <encoding> <sourcefile>

Description: There are many points of view that refer to others, but I have used them, is an absolutely effective solution.



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.