Http://blog.csdn.net/simon_1/article/details/9092747
First, there are two types of response returned: one is the byte stream outputstream and the other is the bytes stream printwrite.
Declaration: For convenience, all outputs are coded in UTF-8.
First, let's talk about the byte stream. to output "China", the output stream must be "China" converted to UTF-8, And the browser must be told to parse the data using utf8.
Note: Response. setheader ("Content-Type", "text/html; charset = UTF-8 ");
To be written in: printwriter PW = response. getwriter (); or: outputstream PS = response. getoutputstream ();!!!!! Before !!!!!
[HTML]View plaincopy
- // This sentence means that the browser uses utf8 to parse the returned data
- Response. setheader ("Content-Type", "text/html; charset = UTF-8 ");
- String data = "China ";
- Outputstream PS = response. getoutputstream ();
- // The meaning of this sentence, so that the data into the stream is in utf8 format
- PS. Write (data. getbytes ("UTF-8 "));
Besides, to output the response stream to China, you need to set response. setcharacterencoding ("UTF-8 ");
[HTML]View plaincopy
- // This sentence means that the browser uses 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 use the UTF-8 for transcoding, instead of using the default iso8859
- Response. setcharacterencoding ("UTF-8 ");
- String data = "China ";
- Printwriter PW = response. getwriter ();
- PW. Write (data );
Experience: 1. If the returned result is ?? Character, which indicates that response. setcharacterencoding ("UTF-8"); is not added.
2. If the returned Chinese characters are garbled characters such as "Invalid Response", it indicates the browser's parsing problem. Check whether the response is missing. setheader ("Content-Type", "text/html; charset = UTF-8.