The Chinese garbled processing in Java EE

Source: Internet
Author: User

Browser normal display

Response.setcontenttype ("text/html;charset= ' Utf-8 '")

Response.setcharacterencoding ("Utf-8");

Server-side Normal reception

There are three cases of garbled Chinese

1) Form submission (get mode, post mode): Input form entered as Chinese

2) hyperlink (get) Link? Parameter name = value (Chinese)

3) sendredict (get) link? parameter name = value (Chinese)

The root cause of garbled production

Request send: The browser has the default encoding, after the above settings, is utf-8.

When the Web server receives, there is also an encoding, because the server is generally developed abroad, so the default encoding is generally iso-8859-1

Browser and server encoding method is inconsistent, resulting in garbled

Problem Solving Methods

1, if the form is submitted, the method is post (form submission should try to select Post)

The parameters are passed through the HTTP request body, and the Request.setcharacterencoding method is used to specify how the Web server accepts the encoding, such as request.setcharacterencoding ("Utf-8")

2, if it is a form submission, the method is get

At this point pass the parameter through the link, for garbled case, can use

String Value=new string (request.getparameter ("parameter name"). GetBytes ("Iso-8859-1"), "Utf-8") to resolve

This scenario can be encapsulated as a tool class

Package Com.bobo.utils;import java.io.UnsupportedEncodingException; Public classMyTools { Public Staticstring getnewstring (String str) {Try {            return NewString (Str.getbytes ("iso-8859-1"),"Utf-8"); } Catch(unsupportedencodingexception e) {//TODO auto-generated Catch blockE.printstacktrace (); return NULL; }    }}
garbled Tool class

3, Hyperlink garbled, sendredict garbled is essentially a GET request, the solution with 2

4, if it is IE6 and the following version, the above method is related to the Get method is still possible garbled, this time the solution is:

Encode the Chinese that you want to send

String info=java.net.urlencoder.encode ("Hello", "utf-8");

Response.sendredict ("URL path? parameter name =" +info);

A third file is received

String Info=new string (request.getparameter ("parameter name"). GetBytes ("Iso-8859-1"), "Utf-8");

Add a point of knowledge:

When we download the file, the warning box may be garbled in Chinese, at this time the solution is:

String temp=java.net.urlencoding.encode ("Chinese file name", "Utf-8")

Response.setheader ("Content-disposition", "attachment;filename=" +temp);

The Chinese garbled processing in Java EE

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.