5. We have to know about Java's compiler:
Javac? Encoding
We often have no encoding parameters. In fact, ENCODING is important for cross-platform operations. If you do not specify eNCoding, follow the system's default eNCoding, the GB platform is GB2312, and the English platform is ISO8859_1. Java's compiler actually calls Sun.Tools.javac.main class, compiles files, this class has an encoding variable in the middle of the Compile function, and -Encoding parameters are actually transmitted to the Encoding variable. The compiler is based on this variable, and then compiles the UTF-8 form into a Class file. Example code:
String str = "you";
FileWriter Writer = New FileWriter ("text.txt");
Write.write (STR);
Writer.close ();
If you compile with GB2312, you will find the field of E4 BD A0;
If you compile with 8859_1, the binary of 00c4 00e3:
0000, 0000, 1100, 0100, 0000, 0000, 1110, 0011
Because each character is greater than 7 bits, so use 11-bit encoding:
1100,000, 0100, 1100, 0011, 1010,0011
C1 - 84 - C3 - A3
You will find C1 84 C3 A3
But we tend to ignore this parameter, so this often has a cross-platform problem:
Sample code is compiled on the Chinese platform to generate zhclass
Sample code compiles on English platform, output enclass
(1) ENCLASS executes OK on the Chinese platform, but can't do it in English platform
(2) Enclass executes OK on the English platform, but not on the Chinese platform
The reason is that:
(1) After compiling on the Chinese platform, the STR runs on the running state, running on the Chinese platform, and the default code of FileWriter is GB2312, so the CHARTOBYTECONVERTER will automatically use the CONVERTER that calls GB2312 to convert the STR to BYTE Enter into FileOutputStream, and 0xC4, 0XE3 is put into the file. But if it is in the English platform, the default value of Chartobyteconvert is 8859_1. FileWriter automatically calls 8859_1 to transform STR, but he can't explain, so he will output "?"
(2) After compiling on the English platform, the STR runs on the running state of char [] is 0x00c4 0x00E3, running on the Chinese platform, there is no way to identify, so there will be ??; on the English platform, 0x00c4 -> 0xc4, 0x00e3-> 0xE3, the 0xC4, 0XE3 is placed in the file.
Sixth, other reasons:
<% @ page contenttype = "text / html; charset = GBK"%>
Set the display code of the browser, if the data of Response is UTF8 encoding, the display will be garbled, but garbled and the above reasons are still different.
Seven, where the encoding occurs:
1. From the database to Java programs byte -> char
2. From the Java program to the database char -> byte
3. From the file to the Java program Byte -> char4. From the Java program to file char -> byte
5. From the Java program to the page display char -> byte
6. Submit data from page form to Java programs BYTE -> char
7. From flow to Java programs Byte -> char
8. From the Java program to flow char -> Byte
You can use the method of configuring a filter to solve Chinese garbled:
init-param>
filter>
filter-mapping>
web-app>
Public Void Dofilter (ServletRequest Req, ServletResponse Res,
Filterchain fchain) throws ioException, servletexception {
HTTPSERVLETREQUEST REQEST = (httpservletRequest) Req;
HTTPSERVLETRESPONSE RESPONSE = (httpservletResponse) res;
HttpSession session = request.getations ();
String userid = (string) session.getattribute ("UserID");
Req.setCharacterencoding (this.filterConfig.GetinitParameter ("charset");
/ / Set the character set?
In fact, it is set byte-> char's Encoding
Try {
IF (userid == null || userid.equals (")) {
if (! Request.getRequestURL (). TOSTRING (). matches.
"* / uirs / logon / logon (controller) {0,1} // x2ejsp $")) {
session.INVALIDATE ();
Response.sendredirect (Request.GetContextPath ()
"/uirs/logon/logon.jsp");
}
}
Else {
// Take a look at the permissions of information reporting system
IF (! Net.golden.uirs.util.uirschecker.check (userid, "information reporting system",
NET.GOLDEN.UIRS.UTIL.UIRSCHECKER.ACTION_DO)) {
if (! Request.getRequestURL (). TOSTRING (). matches.
"* / uirs / logon / logon (controller) {0,1} // x2ejsp $")) {
Response.sendRedirect (Request.getContextPath () "/ uirs / logon / logontroller.jsp");
}
}
}
}
Catch (Exception EX) {
Response.sendredirect (Request.GetContextPath ()
"/uirs/logon/logon.jsp");
}
Fchain.dofilter (REQ, RES);
}