Go A solution that is garbled or not displayed in a JSP or servlet (as well as uploading a download file)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.