Because I have never believed that Java will have a BUG that does not allow mixed display of multiple languages, this weekend I studied the problem of multi-language display of Servlet and Jsp, that is, the problem of multi-character set of Servlet, since I am not very clear about the character set concept, what I wrote may not be accurate. I understand the character set in Java in this way: at runtime, each string object contains servlet
Because I have never believed that Java will have a BUG that does not allow mixed display of multiple languages, this weekend I studied the problem of multi-language display of Servlet and Jsp, that is, the problem of multi-character set of Servlet, since I am not very clear about the character set concept, what I wrote may not be accurate. I understand the character set in Java in this way: at runtime, each string object stores UNICODE internal codes (I think all languages are encoded accordingly, because strings are always represented by internal codes in the computer, generally, the platform is related to string encoding in computer languages, while Java uses platform-independent UNICODE ).
When Java reads a string from a byte stream, it converts platform-related bytes into platform-independent Unicode strings. During output, Java will convert the Unicode string to the byte stream related to the platform. if a Unicode character does not exist on a platform, '? '. For example, in Chinese windows, Java reads a "GB2312" encoded file (which can be any stream) to the memory to construct a string object, the GB2312 encoded text will be converted to a Unicode-encoded string. if this string is output, the Unicode string will be converted to a byte stream or array of GB2312: "Chinese test" -----> "\ u4e2d \ u6587 \ u6d4b \ u8bd5" -----> "Chinese test ".
Example:
Byte [] bytes = new byte [] {(byte) 0xd6, (byte) 0xd0, (byte) 0xce, (byte) 0xc4, (byte) 0xb2, (byte) 0xe2, (byte) 0xca, (byte) 0xd4}; // GBK-encoded "Chinese test"
Java. io. ByteArrayInputStream bin = new java. io. ByteArrayInputStream (bytes );
Java. io. BufferedReader reader = new java. io. BufferedReader (new java. io. InputStreamReader (bin, "GBK "));
String msg = reader. readLine ();
System. out. println (msg)
This program is placed in a system containing the words "Chinese test" (such as a Chinese system), which can print the words correctly. The msg string contains the correct Unicode code for the "Chinese test": "\ u4e2d \ u6587 \ u6d4b \ u8bd5", which is converted to the default character set of the operating system during printing, whether the operating system-dependent character set can be correctly displayed. our information can be correctly output only in systems that support the corresponding character set. Otherwise, the result will be garbage.
Let's take a look at the multi-language problems in Servlet/Jsp. Our goal is that clients in any country send information to the Server through Form. the Server saves the information to the database, and the client still can see the correct information sent by itself during retrieval. In fact, we must ensure that the SQL statement stored on the Server contains the correct Unicode encoding of the text sent by the client; the encoding method used for communication between DBC and the database can contain the text information sent by the client. In fact, it is best to allow JDBC to directly use UNICODE/UTF8 to communicate with the database! In this way, the information will not be lost. the Server must adopt a non-lost encoding method when sending the information to the client, or Unicode/Utf8.
If the Enctype attribute of Form is not specified, Form submits the input content according to the urlencode of the encoding character set on the current page. the server obtains the urlencoding string. The encoded urlencoding string is related to the page encoding. for example, if the gb2312 encoding page submits a "Chinese test ", the result is "% D6 % D0 % CE % C4 % B2 % E2 % CA % D4". each "%" is followed by a hexadecimal string; in UTF8 encoding, "% E4 % B8 % AD % E6 % 96% 87% E6 % B5 % 8B % E8 % AF % 95 ", because a Chinese character in GB2312 encoding is 16 bits, while a Chinese character in UTF8 is 24 bits. Internet Explorer 4 and later browsers in China, Japan, and South Korea all support UTF8 encoding. this solution certainly covers these three languages. Therefore, if we enable UTF8 encoding for Html pages, we can support at least these three languages.
However, if the html/Jsp page is UTF-8 encoded, the application server may not know this situation, because if the information sent by the browser does not contain charset Information, at most, the Server knows to read the Accept-Language request for bidding. we know that the browser uses no encoding only for this bidding. Therefore, the application Server cannot correctly parse the submitted content. why? Because all strings in Java are Unicode16-bit encoded, HttpServletRequest. the request (String) function is to convert the Urlencode encoding information submitted by the client to a Unicode String. some servers can only consider that the client encoding is the same as that of the Server platform and simply use URLDecoder. the decode (String) method is directly decoded. if the client encoding is exactly the same as that of the Server, the correct String can be obtained. Otherwise, if the submitted String contains local characters, this will cause junk information.