Chinese garbled solution in JSP or servlet:
1. By Post Request method:
Set before getting the request parameter:
Request.setcharacterencoding ("Utf-8");
To set the output encoding:
Response.setcontenttype ("Text/html;charset=utf-8");
or <%@ page contenttype= "Text/html;charset=utf-8"%>
2. Request in a Get way
The settings requested in get mode setcharacterencoding are invalid.
We need to make a separate transcoding of each parameter 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 the filter to solve the Chinese garbled problem:
----------------------------------------------------------- 5. Upload and download of garbled or Chinese do not show 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");//UTF-8 encoding of the extracted file name,
The above can solve the upload file name garbled and browser display and stored in the database are displayed normal.
troubleshoot problems when downloading files that are not displayed in Chinese:
Response.setcontenttype ("Application/x-msdownload"); Response.setcontenttype ("Application/octet-stream");
Response.setheader ("Content-disposition", "attachment;filename=" +
New String (Filename.getbytes ("Utf-8"), " Iso-8859-1 "));
Cause analysis, because the HTTP header default encoding for iso-8859-1 and we upload files in the download file process, extracted to the file name through the HTTP header.
So we need to upload the file name to be extracted to the UTF-8, and then in the download we also have to reverse transcoding for iso-8859-1.
I blog Station Original: