Solve the Chinese garbled problem of httpservletresponse output

Source: Internet
Author: User

First, response returns two kinds, one is byte stream outputstream, one is character stream printwrite.

Disclaimer: For the sake of convenience, all outputs are uniformly UTF-8 encoded.

First of all, to output "China", the output stream must be converted to Utf-8 "China", but also to tell the browser, with UTF8 to parse the data

// let the browser use UTF8 to parse the returned data   . Response.setheader ("Content-type", "Text/html;charset=utf-8");   = "China";   = Response.getoutputstream ();   // The meaning of this sentence, so that the data is UTF8 format   Ps.write (Data.getbytes ("UTF-8"));

Again character stream, to export China, need to set response.setcharacterencoding ("UTF-8");

// let the browser use UTF8 to parse the returned data   . Response.setheader ("Content-type", "Text/html;charset=utf-8");   // The meaning of this sentence is to tell the servlet to transcode with UTF-8 instead of using the default ISO8859   Response.setcharacterencoding ("UTF-8");   = "China";   = Response.getwriter ();  Pw.write (data);

Experience: 1, if Chinese return appears?? character, which indicates no addition of response.setcharacterencoding ("UTF-8");

2, if the return of the Chinese is "烇" this garbled, explain the browser resolution problem, should check whether to forget to add Response.setheader ("Content-type", "text/html;charset=utf-8");

Solve the Chinese garbled problem of httpservletresponse output

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.