time 2014-04-14 14:33:44 csdn Blog Original http://blog.csdn.net/xby1993/article/details/23677375 ThemeServletJSP
Solutions for garbled Chinese in JSP or servlet:
1. In the POST request mode:
Set before getting the request parameters:
Request.setcharacterencoding ("Utf-8");
Set Output encoding:
Response.setcontenttype ("Text/html;charset=utf-8");
or <%@ page contenttype= "Text/html;charset=utf-8"%>
2. Request by Get method
The set setcharacterencoding requested by get is not valid.
We need to transcode each parameter separately after getting the parameters.
String name =request.getparameter ("name");
Name=new String (Name.getbytes (), "utf-8");
3. Garbled in the database:
Set the encoding of the database to Utf-8
4. Use filter to solve Chinese garbled problem:
-----------------------------------------------------------
5. Upload and download garbled or Chinese do not display problem resolution:
JSP or servlet upload file Chinese name garbled solution:
String Filen=item.getname (); //string filename=new String (filen.getbytes ("iso-8859-1"), "Utf-8"); String Filename=new String (Filen.getbytes (),"Utf-8"); The extracted file name is UTF-8 encoded,
The above can resolve the upload file name garbled and browser display and stored in the database are displayed normal. Troubleshoot problems with Chinese not appearing when downloading files:
Response.setcontenttype ("application/x-msdownload"); Response.setcontenttype ("Application/octet-stream"); response.setheader ("attachment;filename=" +New String (Filename.getbytes ("Utf-8"), "Iso-8859-1"));
Cause analysis, because the HTTP header default encoding is iso-8859-1 and we upload files in the process of downloading files, the extracted file name is passed through the HTTP header.
So we need to transcode the extracted file name to UTF-8 at the time of uploading, and then we have to reverse transcode to iso-8859-1 when we download it.
Go A solution that is garbled or not displayed in a JSP or servlet (as well as uploading a download file)