I. Summary of Java applications Program Especially in Web-based programs, character encoding problems are often encountered. To prevent garbled characters, you must first understand how Java processes the characters, so that you can add the necessary transcoding in the input/output step purposefully. Second, because various servers have different processing methods, more experiments are required to ensure that no garbled characters are involved. Ii. Basic Concepts 2.1 Java character expressions include char, byte, and string. Char refers to a Unicode character, which is a 16-digit integer. Byte is a byte, and the string needs to be converted to a byte array before network transmission or storage. After receiving data from the network or reading data from the storage device, convert the byte array to a string. String is a string and can be viewed as an array consisting of char. String and Char are in the memory format, and byte is the serialization form of network transmission or storage. Example: English string Ying = "English"; char Ying = Ying. charat (0); string yinghex = integer. tohexstring (Ying); 82 F1 byte yinggbbytes = Ying. getbytes ("GBK"); Brief introduction to the encoding method of the byte value D3 A2 2.2 encoded in GB encoding. When serializing a string to a byte array or deserialization, You must select the correct encoding method. If the encoding method is incorrect, some 0x3f values are obtained. Commonly used character encoding methods include iso8859_1, gb2312, GBK, UTF-8/UTF-16/UTF-32. Iso8859_1 is used to encode Latin. It consists of a single byte (0-255. Gb2312 and GBK are used to encode simplified Chinese characters. They are composed of single-byte and dual-byte combinations. The byte whose maximum bit is 1 and the next byte constitute a Chinese character. The byte whose maximum bit is 0 is ASCII code. UTF-8/UTF-16/UTF-32 is an international standard unicode encoding method. The most used is the UTF-8, mainly because it is about space during the Latin encoding. Unicode value UTF-8 encoding U-00000000-U-0000007F: 0 xxxxxxx U-00000080-U-000007FF: 110 XXXXX 10 xxxxxx U-00000800-U-0000FFFF: 1110 XXXX 10 xxxxxx U-00010000-U-001FFFFF: 11110xxx 10 xxxxxx 10 xxxxxx 10 xxxxxx U-00200000-U-03FFFFFF: 111110xx 10 xxxxxx 10 xxxxxx 10 xxxxxx 10 xxxxxx U-04000000-U-7FFFFFFF: 1111110x 10 xxxxxx 10 xxxxxx 10 xxxxxx 10 xxxxxx 10 xxxxxx 3. Related functions in j2se string STR = "English"; // obtain the gb2312 encoded characters Section byte [] bytesgb2312 = Str. getbytes ("gb2312"); // get the default platform encoding byte (Solaris is iso8859_1, Windows is gb2312) byte [] bytesdefault = Str. getbytes (); // convert bytes into string newstrgb = new string (bytesgb2312, "gb2312") using the specified encoding "); // convert the byte into a string (Solaris is iso8859_1, Windows is gb2312) string newstrdefault = new string (bytesdefault) with the default encoding of the platform ); // read the character inputstream in = xxx from the byte stream with the specified encoding; inputstreamreader reader = inputstreamreader (in, "G B2312 "); char Achar = reader. read (); 4. jsp and database encoding 4.1 encoding in JSP (1) Static Declaration: charset has two functions: JSP file encoding method: when reading JSP files and generating Java classes, encoding of Chinese characters in the source JSP file: encoding of data written to the response stream during JSP execution (2) dynamic Change: You can call response before writing data to the response stream. setcontenttype () to set the correct encoding type. (3) In tomcat, the parameters obtained by request. getparameter () are all encoded in iso8859_1. Therefore, if you enter a Chinese character "English" in the browser input box, you will get an iso8859_1 encoded (0x00, 0xd3, 0x00, 0xa2) on the server side ). Therefore, transcoding is usually performed when parameters are received: String wrongstr = response. getparameter ("name"); string correctstr = new string (wrongstr. getbytes ("iso8859_1"), "gb2312"); in the latest servlet specification, you can also execute the following before obtaining the parameter:Code : Request. setcharacterencoding ("gb2312"); 4.2 database encoding (1) database use UTF-16 if the string contains Unicode characters, no transcoding is required when writing read data (2) the database uses iso8859_1. If the string contains Unicode characters, transcoding and writing are required during read and write: String newstr = new string (oldstr. getbyte ("gb2312"), "iso8859_1"); read: String newstr = new string (oldstr. getbyte ("iso8859_1"), "gb2312"); V. encoding of source files 5.1 encoding of resource file resource files is related to the editing platform. Resource files written on Windows platforms are encoded in gb2312 format. Transcoding is required during compilation to ensure the correctness on each platform: native2ascii-encoding gb2312 source. properties reads the correct Unicode string from the resource file. 5.2 The source file encoding method is related to the editing platform. Source files developed on Windows platforms are encoded in gb2312 format. During compilation, You need to specify the encoding method of the source file: javac-encoding gb2312 the encoding of the byte file generated after Java compilation is UTF-8. ① The latest version of Tomcat 4.1.18 supports request. setcharacterencoding (string ENC) ② transcode the resource file to company. name =/u82f1/u65af/u514b ③ if the database uses UTF-16, this part of transcoding is not required ④ the page should have transcoded I: String S = new string (request. getparameter ("name "). getbytes ("iso8859_1"), "gb2312"); transcoding II: String S = new string (name. getbytes ("gb2312"), "iso8859_1"); transcoding III: String S = new string (name. getbytes ("iso8859_1"), "gb2312 ");