1. Query “China” code table: common Chinese code tableGBKandUTF-8
The RESULTS of the UTF-8 bytecode decoding with ISO-8859-1 are: O-¹ U. The first part is that the UTF-8 bytecode decoding with ISO-8859-1 is the best partCopy the code
2. Build a database
Database structure
Field Type Null Key Default Extra
-------- ------------ ------ ------ ------- --------
uid varchar(32) NO PRI (NULL)
username varchar(100) YES UNI (NULL)
password varchar(100) YES (NULL)
email varchar(100) YES (NULL)
name varchar(100) YES (NULL)
sex varchar(10) YES (NULL)
Copy the code
3. The production belt<form action="/day15_test/RegisterServlet" method="post">The form ofhtmlWeb page, submit form toServlet,ServletGet form information and write it to the database.
Database garble
Symptom: Submit a form, useQueryRunnerAdd data to table, Chinese content garbled, display Western European characters
QueryRunner qr = new QueryRunner(C3p0Utils.getDataSource());
String name = request.getParameter("name");
int row = qr.update("insert into user values(uid,username,password,email,name,sex)");
Copy the code
UTF-8 ISO-8859-1
Possible cause 1: The internal encoding of the database is incorrect. Check the default character set of mysql in the installation directorymy.iniFile,ctrl+fTo find thedefault-character-setWhether it isutf8If not changedutf8; I looked it up. Yesutf8Character set, no problem
Possible cause 2: The request object uses yesUTF-8Character set encoding but used by the serverISO-8859-1Decoding, between the server and the database through the character stream transfer data, so the emergence of Western Europe garbled code, just add the request object coding information, requiring the server to useUTF-8Decode:
request.setCharacterEncoding("UTF-8");
Copy the code
According to the results
Console garble
Symptom: Garbled characters appear in the printed form information on the console
String name = request.getParameter("name");
System.out.println(username + "" + password + "" + email + "" + name + "" + sex);
Copy the code
Display result:
UTF-8 ISO-8859-1
Analysis 1: Obtain it from the browserUTF-8Encoded content is transmitted to the server after passingISO-8859-1Decodes western European characters (but the internal bytecode is unchanged) and transmits them through the character stream toeclipseConsole,eclipsewithUTF-8Decoding (personal habit setting), nature is a question mark; What doesn’t change all the way through this stream is the underlying bytecode, so just useISO-8859-1Decode, in useUTF-8Just recode it.
String name = request.getParameter("name");
name = new String(name.getBytes("ISO-8859-1"), "UTF-8");
Copy the code
UTF-8
request.setCharacterEncoding("UTF-8");
Copy the code
HTML pages are garbled
Phenomenon:Response.getwriter ().write("<font color='green'> Login successful! </font>");Output appears as a question mark on the pageDisplay result:
The default code set for the cause analysis server isISO-8859-1, the browser default encoding isGBKAfter being passed to the browser, the browser decodes incorrectly and a question mark appears.
The solution
Solution 1:Use the response objectGBKEncoding, sent to the browser, the browser using the default character set decoded
response.setCharacterEncoding("GBK");
Copy the code
Display result:
Solution 2:The response object is usedUTF-8Code and notify the browser to use itUTF-8Decode: If only the response object is set to useUTF-8Encoding (3 bytes), usedGBKDecoding (2 bytes) page will have more characters
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
Copy the code
Display result:
Solution 3: UTF-8
response.setContentType("text/html; charset=UTF-8");
Copy the code
Display result:
Download file Chinese garbled
Phenomenon: Make a download hyperlink<a href="/day15_test/DownLoadServlet? Filename = hello.rar "> hello.rar </a>Download file, when Chinese is, file name is not displayed.DownLoadServletThe source code
String filename = request.getParameter("filename");
filename = new String(filename.getBytes("ISO-8859-1"), "UTF-8"); // Force download response.setContentType(this.getServletContext().getMimeType()"/resource/" + filename));
response.setHeader("Content-Disposition"."attachment; filename ="+ filename); InputStream is = this.getServletContext().getResourceasStream ()"/resource/" + filename);
OutputStream os = response.getOutputStream();
byte[] b = new byte[1024 * 8];
int len = 0;
while((len = is.read(b)) ! = -1) { os.write(b, 0, len); } os.flush(); is.close(); os.close();Copy the code
throughString filename = request.getParameter("filename");Get the file’s full name, and thenSystem.out.println(filename);Garbled characters are output on the console. For details, see The second Cause of Garbled characters on the console.
response.setContentType()Get the extension name of the file, the extension name of the file is western European characters, no garbled characters.
response.setHeader("Content-Disposition", "attachment; filename =" + filename);This code means to upload the file name to the browser, the server default will befilename.getBytes("ISO-8859-1")Decoded to the server, andfilenameisUTF-8Coded, sent to the server will naturally be garbled.
InputStream is = this.getServletContext().getResourceAsStream("/resource/" + filename);;filenameHas been decodedUTF-8, and no external transmission, will not occur the phenomenon of garbled code.
The solutionMethod 1: 将filenamewithUTF-8Decode, in useISO-8859-1Code, the server willfilenamewithISO-8859-1Decoded and sent to the browser where the browser is in useUTF-8Decoding.
String filenameDownlaod = new String(filename.getBytes("UTF-8"), "ISO-8859-1");
InputStream is = this.getServletContext().getResourceAsStream("/resource/" + filenameDownlaod);
Copy the code
String filename = request.getparameter (“filename”); String filename = request.getparameter (“filename”); String newFilename = new String(filename.getBytes(“ISO-8859-1”), “UTF-8”); Create a filename locally and send it to the browser using filename. The complete code is as follows
String filename = request.getParameter("filename");
String newFilename = new String(filename.getBytes("ISO-8859-1"), "UTF-8");
response.setContentType(this.getServletContext().getMimeType("/resource/" + newFilename));
response.setHeader("Content-Disposition"."attachment; filename =" + filename);
InputStream is = this.getServletContext().getResourceAsStream("/resource/" + newFilename);
OutputStream os = response.getOutputStream();
byte[] b = new byte[1024 * 8];
int len = 0;
while((len = is.read(b)) ! = -1) { os.write(b, 0, len); } os.flush(); is.close(); os.close();Copy the code
According to the results
summary
The main idea to solve Chinese garbled code is to solve the code table and code table consistent;
This article only describespostThe method is garbled,getThe way is not discussed, actually can use the console garble solution;
There is also a problem that has not been solved for the time being.
Response.getwriter ().write(request.getMethod()); response.getwriter (); request.getRequestDispatcher("FailToRegist.html").forward(request, response); And the following two methods appear at the same time no garble, confused? Response.getoutputstream ().write(request.getMethod().getBytes()); request.getRequestDispatcher("FailToRegist.html").forward(request, response)
Copy the code
Reference Servlet Chinese garble problem and solution analysis (Bloggarden – Xiazdong);
In fact, the problem of garbled code has a once and for all method, temporarily not discussed.