Content Summary:
1. Database garbled characters
2. Console garbled characters
3. HTML pages are garbled
4. Download the file name is garbled
The preparatory work
1. Query “China” code table: common Chinese code tableGBK
andUTF-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 ofhtml
Web page, submit form toServlet
,Servlet
Get form information and write it to the database.
Database garble
- Symptom: Submit a form, use
QueryRunner
Add 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 directory
my.ini
File,ctrl+f
To find thedefault-character-set
Whether it isutf8
If not changedutf8
; I looked it up. Yesutf8
Character set, no problem - Possible cause 2: The request object uses yes
UTF-8
Character set encoding but used by the serverISO-8859-1
Decoding, 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-8
Decode:
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 browser
UTF-8
Encoded content is transmitted to the server after passingISO-8859-1
Decodes western European characters (but the internal bytecode is unchanged) and transmits them through the character stream toeclipse
Console,eclipse
withUTF-8
Decoding (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-1
Decode, in useUTF-8
Just 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 is
ISO-8859-1
, the browser default encoding isGBK
After being passed to the browser, the browser decodes incorrectly and a question mark appears. - The solution
- Solution 1:Use the response object
GBK
Encoding, 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 used
UTF-8
Code and notify the browser to use itUTF-8
Decode: If only the response object is set to useUTF-8
Encoding (3 bytes), usedGBK
Decoding (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.DownLoadServlet
The 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
- through
String 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, andfilename
isUTF-8
Coded, sent to the server will naturally be garbled.InputStream is = this.getServletContext().getResourceAsStream("/resource/" + filename);
;filename
Has been decodedUTF-8
, and no external transmission, will not occur the phenomenon of garbled code.
- The solutionMethod 1: 将
filename
withUTF-8
Decode, in useISO-8859-1
Code, the server willfilename
withISO-8859-1
Decoded and sent to the browser where the browser is in useUTF-8
Decoding.
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 describes
post
The method is garbled,get
The 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.