Use response to output Chinese data to the client (garbled Problem Analysis)

Source: Internet
Author: User

When the Web server receives an HTTP request from the client, it creates a request object representing the request and a response object for each request. Since the request and response objects represent requests and responses, we only need to find the request object to obtain the data submitted by the client. To output data to the client, you only need to find the response object.

Common response applications (1): Output Chinese data to the client

1 package COM. yyz. response; 2 3 Import Java. io. ioexception; 4 Import Java. io. outputstream; 5 6 Import javax. servlet. servletexception; 7 Import javax. servlet. HTTP. httpservlet; 8 Import javax. servlet. HTTP. httpservletrequest; 9 Import javax. servlet. HTTP. httpservletresponse; 10 // Chinese output 11 public class responsedemo extends httpservlet {12 13 public void doget (httpservletrequest request, httpservletrespo Neuron response) 14 throws servletexception, ioexception {15 string data = "China"; 16 outputstream out = response. getoutputstream (); 17 out. write (data. getbytes (); 18/** 19 * out. write (data. getbytes (); this Code involves two queries to the code table. When the 20 * "China" character data is changed to byte data, you can view the gb2312 code table. 21 * when the data is sent to the browser for display, You need to view the code table again. The code table to view is related to the browser settings. 22 */24} 25 26 Public void dopost (httpservletrequest request, httpservletresponse response) 27 throws servletexception, ioexception {28 doget (request, response); 29} 30 31}

Test result when the browser encoding is set to gb2312:


Test results when browser encoding is set to UTF-8:

To make our website accessible to foreign users, we need to specify the conversion code table as a UTF-8 when converting character data into byte data. However, if the browser is opened with gb2312, garbled characters may occur. Although you can solve this garbled problem by changing the browser settings, it is not conducive to enhancing the user experience. Therefore, we need to use a program to tell the browser which code table to view and display data.

1 package COM. yyz. response; 2 3 Import Java. io. ioexception; 4 Import Java. io. outputstream; 5 6 Import javax. servlet. servletexception; 7 Import javax. servlet. HTTP. httpservlet; 8 Import javax. servlet. HTTP. httpservletrequest; 9 Import javax. servlet. HTTP. httpservletresponse; 10 // Chinese output 11 public class responsedemo extends httpservlet {12 13 public void doget (httpservletrequest request, httpservletrespo Neuron response) 14 throws servletexception, ioexception {15 // on the server side, which table is the data output, you need to control which table the browser opens. 16 string data = "China"; 17 response. setheader ("Content-Type", "text/html; charset = UTF-8"); 18 outputstream out = response. getoutputstream (); 19 out. write (data. getbytes ("UTF-8"); 20} 21 22 public void dopost (httpservletrequest request, httpservletresponse response) 23 throws servletexception, ioexception {24 doget (request, response); 25} 26}

Learn more:

Use the <meta> label in the HTML language to control browser behavior.

<Meta http-equiv = "Content-Type ''content ='' text/html; charset = UTF-8 ">
HTTP-equiv simulates the HTTP response header and tells the browser to open it in a UTF-8 code table. The real response header takes precedence over the Response Header simulated by HTTP-equiv.

In actual development, the server should use the ghost stream to write text data to the browser. But through the response getwriter method to get the pipeline flow default code table is ISO8859-1, this code table is not Chinese corresponding encoding, so will? The corresponding encoding is sent to the browser. The browser opens with question marks. You can use the setcharacterencoding method of response to modify the data reading table when the server sends data.

1 package COM. yyz. response; 2 3 Import Java. io. ioexception; 4 Import Java. io. printwriter; 5 6 Import javax. servlet. servletexception; 7 Import javax. servlet. HTTP. httpservlet; 8 Import javax. servlet. HTTP. httpservletrequest; 9 Import javax. servlet. HTTP. httpservletresponse; 10 // Chinese output 11 public class responsedemo extends httpservlet {12 13 public void doget (httpservletrequest request, httpservletrespon Se response) 14 throws servletexception, ioexception {15 // on the server side, which table is the data output, you need to control which table the browser opens. 16 string data = "China"; 17 response. setheader ("Content-Type", "text/html; charset = UTF-8"); 18 response. setcharacterencoding ("UTF-8"); 19 printwriter out = response. getwriter (); 20 out. write (data); 21} 22 23 public void dopost (httpservletrequest request, httpservletresponse response) 24 throws servletexception, ioexception {25 doget (request, response); 26} 27}

Note the following details:
1. response. setcharacterencoding ("UTF-8"); needs to be written before printwriter out = response. getwriter. It is useless to set the encoding after getting the response stream.

2. response. setheader ("Content-Type", "text/html; charset = UTF-8"); There is a simpler way to write response. setcontenttype ("text/html; charset = UTF-8 ");.

3. response. setcontenttype ("text/html; charset = UTF-8"); this Code actually has two roles: notification response to UTF-8 output and browser to UTF-8 open. It is equivalent to response. setheader ("Content-Type", "text/html; charset = UTF-8"); and response. setcharacterencoding ("UTF-8.

4. After reading the above, the reader should be able to understand why response. getoutputstream. Write (1); the output of this code in the browser is not 1. Because the browser is a text editor, after receiving the data, it will take 1 to check the code table and then display the corresponding characters. To output a number in the browser, convert the number into a string, response. getoutputstream. Write (1 + ""). getbytes ());.

 

Use response to output Chinese data to the client (garbled Problem Analysis)

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.