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