Analysis of Chinese data garbled problem in Java EE using response to client _java

Source: Internet
Author: User

The Web server receives the HTTP request from the client, creating a Request object for each request, and a response object that represents the response. The request and response objects, since they represent requests and responses, we need to get the data submitted by the client, just to find the request object. To output data to the client, just look for the response object.

Copy Code code as follows:

Package com.yyz.response;
Import java.io.IOException;
Import Java.io.OutputStream;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Problems with exporting Chinese
public class Responsedemo extends HttpServlet {
public void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
String data = "China";
OutputStream out = Response.getoutputstream ();
Out.write (Data.getbytes ());
/**
* Out.write (Data.getbytes ()); This code involves two lookup code tables.
* "China" will refer to the GB2312 Code table when it is converted from character data to byte data.
* When the data is sent to the browser to display, you need to refer to the Code table again, then the Lookup Code table and browser settings.
*/
}
public void DoPost (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
Doget (Request,response);
}
}

Test results when the browser encoding is set to GB2312:

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

To make our site accessible to foreign users, we specify the converted code table as UTF-8 when converting character data into byte data. But then if the browser to open GB2312, there will be garbled problems. Although you can change the browser settings to solve this garbled problem, but not conducive to enhance the user experience. So we need to use the program to tell the browser what code table to display the data.

Copy Code code as follows:

Package com.yyz.response;
Import java.io.IOException;
Import Java.io.OutputStream;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Problems with exporting Chinese
public class Responsedemo extends HttpServlet {
public void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
On the server side, the data is in which code table output, it is necessary to control the browser to open which code table.
String data = "China";
Response.setheader ("Content-type", "text/html;charset=utf-8");
OutputStream out = Response.getoutputstream ();
Out.write (Data.getbytes ("UTF-8"));
}
public void DoPost (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
Doget (Request,response);
}
}

Learn a trick:

Use the <meta> tags inside 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 with a UTF-8 code table. The real response head takes precedence over the response head modeled with HTTP-EQUIV.

In actual development, the server writes text data to the browser using a character stream. But through the response Getwriter method to get the character stream the default code table is iso8859-1, this table is not the corresponding code in Chinese, so will put? The corresponding encoding is sent to the browser, and the browser opens with a full question mark. The response setcharacterencoding allows you to modify the code table that is consulted when the server sends data.

Copy Code code as follows:

Package com.yyz.response;
Import java.io.IOException;
Import Java.io.PrintWriter;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Problems with exporting Chinese
public class Responsedemo extends HttpServlet {
public void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
On the server side, the data is in which code table output, it is necessary to control the browser to open which code table.
String data = "China";
Response.setheader ("Content-type", "text/html;charset=utf-8");
Response.setcharacterencoding ("UTF-8");
PrintWriter out = Response.getwriter ();
Out.write (data);
}
public void DoPost (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
Doget (Request,response);
}
}

Here are a few small details to note:
1. Response.setcharacterencoding ("UTF-8"); to be written in the front of printwriter out = Response.getwriter (); It is no use to set the code when you get the character stream.

2. Response.setheader ("Content-type", "text/html;charset=utf-8"); there is a simpler way of writing response.setcontenttype ("text/html; Charset=utf-8 ");

3.response.setcontenttype ("Text/html;charset=utf-8"); This code actually has two functions: Notify response to UTF-8 output and browser to open UTF-8. That is equivalent to Response.setheader ("Content-type", "Text/html;charset=utf-8"), and Response.setcharacterencoding ("UTF-8"), two lines of code.

4. Through reading above, readers 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 will take 1 to check the Code table, and then display the corresponding characters. To output numbers in a browser, you should turn the numbers into strings, Response.getOutputStream.write (1+ ""). GetBytes ());.

To send data using OutputStream (Byte stream):
1, Response.getoutputstream (). Write ("China". GetBytes ());//Send data in default encoding
2, Response.getoutputstream (). Write ("China". GetBytes ("UTF-8"))//Send data in UTF-8 encoding, the browser (GB2312 by default) will appear garbled

Solution:
2.1 By changing the way the browser is encoded: ie/"View"/"Encoding"/"UTF-8" (undesirable)
2.2 By setting the response header to inform the client of the encoding: Response.setheader ("Content-type", "text/html;charset=utf-8");//Tell browser data type and encoding
2.3 Through META tag simulation request header: Out.write ("<meta http-equiv= ' content-type ' content= ' text/html;") Charset=utf-8 '/> '. getBytes ());
2.4 Through the following methods: Response.setcontenttype ("Text/html;charset=utf-8");

Related Article

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.