Analysis of Java character encoding principles

Source: Internet
Author: User

What are characters and what are bytes?

It can be understood that the computer does not have the concept of character, only byte. Characters exist in the human language layer and are used for communication between people, because bytes are not readable to humans, however, the computer stores all data in bytes.
Therefore, to store characters in human consciousness in a computer, the characters must be converted to byte data. How to convert the characters must be subject to a ing rule, the ing rule here is the character encoding in the general sense. For example, the file is GBK encoded and can be said: the character data in this document is converted to byte storage according to the character byte ing rules such as GBK.

Therefore, all characters in human consciousness stored in a computer or transferred through a computer involve conversion between characters and bytes by some ing rule.

Converting a character into a byte according to the ing rules is called encoding, and vice versa, it is called decoding.

Understand the characters and bytes, and the significance of encoding and decoding. in Java, see where encoding is required:

1: Java source code file. After editing data using the editor, you need to select a ing rule to store the "characters" in the editor on your computer. When compiling the Java source code, javac reads the source code file and obtains the byte data of the source code file. to convert it to a character, it must follow the same ing rules selected when the source code is stored.
Ing rules can correctly restore these bytes to characters in human consciousness, and then map these characters to bytes in. Class Using UTF-16 ing rules. Therefore, when compiling Java source code, you must specify the character-byte ing rules (encoding) of the source code. If an error is specified, the ing-back characters will fail.
Note: by default, javac uses the code on the local compilation platform to read source code files. Therefore, if some developers are Japanese and some are Chinese systems, but they do not have the same source code encoding, after being uploaded to CVS, they will be compiled on UTF-8.

2: Console encoding: When we use system. Out. println () in the Java code, we pass data to the operating system for the operating system to display again. There is also an encoding in the middle. Remember: All characters involved involve encoding. Because the underlying layer is transmitted through bytes, You must select a character-byte ing rule to pass through bytes.
Imagine using a ing method on the Java side to map characters into bytes and send these bytes to the operating system. The operating system obtains byte data, then, some ing method is used to map bytes back to characters. If the ing method is incorrect, unpredictable data is displayed. This is not the expected result.
Java uses the default ing mode of the local operating system to map characters to bytes. The operating system also uses the default ing to map bytes back to characters, therefore, no errors will occur in this process. The garbled characters we see on the console are not what you want to see at this time when they are in Java memory or characters.
As mentioned above source code encoding, if the Encoding Error is specified at the time of compilation, then the source code bytes are mapped to the character has an error, and then the wrong character is mapped to the byte by UTF-16, these bytes are mapped to characters at run time, and the character at this time is already wrong (this character is originally a byte mapped to the UTF-16 ing rules, but later according to the GBK ing
Rules are mapped to characters. It is easy to confuse with the console in case of garbled characters, such as using SSH during remote control, at this time, an additional layer of SSH server is sent to the SSH client to send the byte data mapped to the characters according to some ing rules and the SSH client selects a ing rule to map the byte to the characters.

3: JSP file encoding. Use pageencoding in JSP to specify the encoding of the JSP source file. The principle is the same as the Java source code in the first article above. But why does JSP need it instead of a. Java file. Because JSP is compiled on the server. You write JSP files on the local machine and store the default encoding GBK. If the server is UTF-8 encoded after arriving at the server, will it go wrong if you adopt the same policy as. Java.

4: The Web server sends data to the browser. To transmit character data over the network, you need to map the characters to bytes in some way. On the Browser Side, when the browser receives the byte data, it selects a ing method to reflect it as a character for users to watch. Similarly, an error occurs if the selected method does not match. Therefore, the server can tell the client about the encoding method. This information can be contained in the HTTP header (such as Content-Type: text/html; charset = GBK), which can be returned through response. setcontenttype implementation (use the <% @ page contenttype = "" %> command in JSP to convert to response. setcontenttype code ). If pageencoding is specified in JSP but contenttype is not specified, the generated servlet Code uses the pageencoding encoding to set contenttype by default. If contenttype is not set, the server (Tomcat) defaults to iso-8859-1.

5: data sent by the browser to the server (related to the configuration of the browser and the browser): also transmitted over the network. Therefore, You Need To Know What ing rules the client uses to map user input characters into bytes and pass them to the server. generally, the client uses the code of the page to send data (I have not carefully tested this), but the Web server (Tomcat) uses the iso-8859-1 ing rule by default, therefore, the ing rules of both parties are inconsistent. The solution is
Request. setcharacterencoding sets the server's ing rules for the byte data sent by the browser (different servers may have different implementation policies for this method. In the project, Weblogic and tomcat are different, webLogic uses this method to set the ing rules for both HTTP header and content block data. However, in Tomcat, this ing rule is only used for content blocks, the packet header ing rules are on the server. XML connector uriencoding configuration ).
Different browsers may also be different. In IE6, I tested the server's UTF-8 encoding. Through packet capture, I found that the data sent by the server is indeed UTF-8. However, if the data is transmitted through a URL, such as <a href = "1.jsp? A = "/> The result shows that IE transmits E8, B6 (one byte is missing), while UTF-8 of" "is encoded as E8, B6, and 81. However, if the form is used for submission, no matter whether the get/POST method is used, all information transmitted is "% E8, % B6, % 81"
The encoding starting with "%" indicates that the binary is represented in hexadecimal notation. But in opera, all the above three methods pass "% E8, % B6, % 81". What if the server sends GBK encoding? Haha test it by yourself

6: javascript encoding: javascript is also related to browsers. in IE, for example, you can call open ("get", "2.jsp? A = person "); although the current page is UTF-8 encoded, JavaScript passes c8cb, Which is GBK encoding of" person. It is correct to pass % E4 % Ba in opera. if the server sends bytes in UTF-16 encoding, ie still transmits the GBK encoding of "person", and opera still delivers UTF-8 encoding.
I don't know where the browser can be configured.

7: Database encoding: the database is not tested well because it also involves the encoding of the database client. If the correct characters are stored in the database file according to the encoding specified by the database, the database server sends the correct characters to the client when querying the data, however, if the character byte ing rules on the client are set incorrectly, the user may mistakenly think that the database stores incorrect characters. The best solution is packet capture analysis, but the database protocol is very complicated and cannot capture packets,
However, if an error occurs in a process, the principle is the same as described above. It must be a browser <-> Web server, web server <-> database, database <-> database client, there is a problem between the SSH client and the SSH server.

To solve the garbled problem caused by inconsistent character-byte ing rules selected by both parties:
1: Tell the recipient what ing rules they adopt to convert characters into bytes.
2: In the agreement, both parties use a rule ing.
3: similar to what ing rules are used for data mapped under the first line of the XML mark
4: store special byte flag ing rules in the file header, such as Windows processing UTF-16 (will cause problems if the word "Unicom" appears at the beginning of the file, you can search for this situation on the Internet and analyze it)

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/edisondu1983/archive/2009/03/11/3979577.aspx

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.