Javaweb the ultimate solution to all kinds of Chinese garbled characters

Source: Internet
Author: User
Tags base64

One, the servlet output garbled

1. Use the Servlet.getoutstream byte stream to output Chinese, assuming that the output is string str = "The Diaoyu Islands are Chinese, Shameless is Japan".

1.1 If the local server and the local client this is needless to say, directly can Out.write (Str.getbytes ()) can output no problem. Because the server uses Str.getbytes () is the default local encoding, such as GBK. and the browser also parse with the local default encoding, the two are unified, so there is no problem.

1.1 If the server output is used, Out.write (Str.getbytes ("Utf-8")). While the local default encoding is GBK (scale in China), it will be garbled when opened with a browser. Because the server sent over the Utf-8 1010 data, and the client browser with the GBK to decode, the two codes are not uniform, is certainly garbled. Of course, you can also manually call the code of the client browser (IE menu is: query view-> encoding encoding->utf-8), but this operation is bad, preferably by the server output response header to tell, the browser with which encoding to decode. So in the server servlet, add Response.setheader ("Content-type", "Text/html;charset=utf-8"), Of course, the simple Response.setcontenttype ("Text/hmtl;charset=utf-8") can also be used directly. The two operations are the same.

2. Use the Servlet.getwirter character stream to output Chinese, assuming that the output is string str = "Diaoyu is Chinese, shameless is Japanese".

2.1 When written as Out.print (str) output, the client browser will display all of the multiple????? character, which means that the corresponding characters must not be found in the encoding table. The reason is: Servlet.getwriter () to get the character output stream, the default character output is the use of iso-8859-1, and Iso-8859-1 certainly does not support Chinese. So the first thing you must do first: is to have the server object output character to support Chinese. Second, the response header that the server writes back to the client tells the client which encoding table is used to encode. To achieve both requirements, only Response.setcontenttype ("Text/hmtl;charset=utf-8") is required. It's done. Special Note: Response.setcontenttype ("Text/html;charset=utf-8") is placed in front of printout out = Response.getwriter () code, Otherwise just have to tell the client with what Code table encoding function, and the server side still with iso-8859-1 code. In particular: in the Doget or Dopost method in the same servlet, Can not both use Response.getoutputstream and Response.getwriter, because these two response response output byte stream and character stream is conflict, can only use one.

Second, the servlet file download, Chinese garbled situation.

The key is the Attachment;filename= file name in the response header content-disposition when downloading. This file name filename cannot be a string containing Chinese characters, to be encoded with urlencoding encoding, in order to carry out the HTTP transmission. The following code example:

Third, the servlet's response increase the value of the Addcookie,cookie in the Chinese code problem resolution method.

For the principle of cookies, see http://blog.csdn.net/chenshufei2/article/details/8009992. If you want to store the value of Chinese in the cookie, it must be encoded with Base64 and sent to the client's browser to enter the storage. The next time the client browsing access is the value of the cookie that is brought back, it is Base64 encoded, so it needs to be decoded by Base64. BASE64 encoding is mainly to solve the re-encoding of special characters, encoded into a-B, a-B, 0-9, + and/, character 52, 10 digits with a +, A/a total of 64 characters. Its principle is to encode the original 3 bytes of content into 4 bytes. The main is to take a byte after 6 bits, in front of the complement 00 to compose a new byte. So the original 3 bytes Total 24, is encoded into 4 bytes 32 bits.

The specific code examples are as follows:

Four, get the request parameter garbled

Get the way garbled:

such as <a href= "/demo5/servlet/rd2?name= China" >CN</A> Directly with the Request.getparameter string STRCN will be garbled, this is also because the Get method is the URL of the HTTP pass through the default ISO-8859-1 encoded, so the first to get STRCN to use iso-8859-1 code to get the original text, then use utf- 8 (see what the CharSet of the specific page is utf-8 or GBK) to decode. New String (Strcn.getbytes ("iso-8859-1"), "UTF-8");

The trouble with this way of operation is that there is one parameter to decode once with iso-8859-1 encoding.

The Post way garbled: only need to request.setcharacterencoding ("UTF-8"): Can.

Javaweb the ultimate solution to all kinds of Chinese garbled characters

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.