@[TOC]

Problem: Using byte stream output, generate Chinese garbled characters?

Take a chestnut

The following form will output Chinese garble

public class ResponseDemo1 extends HttpServlet {

    /** * demonstrates the problem of byte stream output garble */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       
        String str = "The problem of Chinese characters in byte stream output";// UtF-8 character set. In this case, the browser display also needs utF-8 character set.
        //1. Get the byte stream output object
        ServletOutputStream sos = response.getOutputStream();

        //2. Convert STR into a byte array and print it to the browser
        sos.write(str.getBytes("UTF-8")); 
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException { doGet(request, response); }}Copy the code

Reason: save and access are not the same code table

  • The IDEA character set used to create the file is savedUTF-8.
  • The default character set for Chrome and Internet Explorer isGB2312(actually GBK), save and access is not the same code table, will produce garbled code.

Solution: put and take the code table unified

The first solution:

Modify the browser encoding by right-clicking – encoding – to UTF-8. (Not recommended, our app tries not to ask users to do anything)

Internet Explorer and Firefox can right-click to set the character set. Chrome, on the other hand, requires a plugin, which is cumbersome.

Second solution: (not recommended because it’s hard to remember)

Output a meta tag to the page that looks like this:

 <meta http-equiv="content-type" content="text/html; charset=UTF-8">
Copy the code

It basically tells the browser which code to display.

In Java it looks like this:

//1. Get the byte stream output object
ServletOutputStream sos = response.getOutputStream();

sos.write("
      ".getBytes());
        
Copy the code

The third solution:

Sets the response header to tell the browser the MIME type and character set of the response body

response.setHeader("Content-Type"."text/html; charset=UTF-8");
Copy the code

This is implemented in Java:

//1. Get the byte stream output object
ServletOutputStream sos = response.getOutputStream();
      
response.setHeader("Content-Type"."text/html; charset=UTF-8");
Copy the code

Fourth solution: the one we recommend

It essentially sets up a response header

response.setContentType("text/html; charset=UTF-8");
Copy the code

This is implemented in Java

//1. Get the byte stream output object
ServletOutputStream sos = response.getOutputStream();

response.setContentType("text/html; charset=UTF-8");
//2. Convert STR into a byte array and print it to the browser
sos.write(str.getBytes("UTF-8")); 

Copy the code

case

Examples of the four solutions mentioned above:

public class ResponseDemo1 extends HttpServlet {

    /** * demonstrates the problem of byte stream output garble */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        /** * error: * String STR = "error "; * When using byte stream output, will Chinese characters be garbled? * String STR = "Byte stream Chinese garbled problem "; Save using UTF-8, the character set used for IDEA creation files. * The default character set for Chrome and Ie is GB2312(actually GBK). If you use a different code table, garbled characters will be generated. * * If a garbled code, is not the same storage and access table * solution: * the storage and access table unified. * /
        String str = "The problem of Chinese characters in byte stream output";// UtF-8 character set. In this case, the browser display also needs utF-8 character set.
        //1. Get the byte stream output object
        ServletOutputStream sos = response.getOutputStream();
        /** * First workaround: * Change the encoding of the browser by right-clicking - encoding - to UTF-8. * Internet Explorer and Firefox can set the character set directly by right-clicking. Chrome, on the other hand, requires a plugin, which is cumbersome. 
       < span style = "box-sizing: border-box; color: RGB (51, 51, 51); * Response. setHeader(" content-type ","text/ HTML; charset=UTF-8"); * The fourth solution: the one we recommend * this essentially sets a response header * Response.setContentType ("text/ HTML; charset=UTF-8"); * /
        Sos. write("
      ".getBytes());
        Response. setHeader(" content-type ","text/ HTML; charset=UTF-8");
        // The fourth solution:
        response.setContentType("text/html; charset=UTF-8");
        //2. Convert STR into a byte array and print it to the browser
        sos.write(str.getBytes("UTF-8")); 
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException { doGet(request, response); }}Copy the code

Case after problem solving: Output Chinese normally

public class ResponseDemo2 extends HttpServlet {

    /** * Character stream output Chinese garble *@param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String str = "Character stream output Chinese garbled characters";
        //response.setCharacterEncoding("UTF-8");

        // Sets the MIME type and character set for the response body
        response.setContentType("text/html; charset=UTF-8");
        //1. Get the character output stream
        PrintWriter out = response.getWriter();
        //2. Use character stream to output Chinese
        /** * problem: * out.write(STR); Utf-8 = utF-8; utF-8 = utF-8; utF-8 = utF-8; * The default browser is GBK. (local system character set) * * UTF-8(save)————>PrintWriter ISo-8859-1 (save) * PrintWirter ISo-8859-1 (save)———— PrintWriter is taken from the Response object, and the response character set is set. Note: Set the character set for response before taking the stream. * response.setCharacterEncoding("UTF-8"); * * response.setContentType("text/html; charset=UTF-8"); * This method does two things: * 1. Set the character set of the response object (including the output stream of characters retrieved by the response object) * 2. Tell the browser the MIME type and character set */ of the response body

        out.write(str);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException { doGet(request, response); }}Copy the code