Question about the Chinese Character Set in Servlet response, servlet Chinese Character Set

Source: Internet
Author: User

Question about the Chinese Character Set in Servlet response, servlet Chinese Character Set

When using response in Servlet to output Chinese characters to the client browser, garbled characters may occur. The following is a summary:

There are two types of response output streams: one is byte stream output and the other is bytes stream output.


1. Output in byte streams:
1. garbled wood output by default
2. Use the setHeader method of response to set UTF-8 encoding without garbled characters.
3. Use the setContentType method of response to set UTF-8 encoding without garbled characters.
4. It is recommended to output numbers as strings.


2. output with streams:
1. Check the iso-8859-1 code table by default (SUN's Servlet specification requirements), client display garbled
2. Use the setHeader method of response to set UTF-8 encoding without garbled characters.
3. Use the setContentType method of response to set UTF-8 encoding without garbled characters.

 

Byte streams are output in the default encoding format:

1 public void doGet (HttpServletRequest request, HttpServletResponse response) 2 throws ServletException, IOException {3 // output Chinese data to the client using the default encoding in the byte stream, with 4 response garbled. setContentType ("text/html"); 5 6 String str = "Oh, huh, huh"; 7 OutputStream out = response. getOutputStream (); 8 out. write ("</br> <div align = \" center \ "style = \" font-size: 25px; color: red \ "> ". getBytes (); 9 10 out. write (str. getBytes (); 11 12 out. write ("</div> ". getBytes (); 13 out. close (); 14}

 

The byte stream is encoded as UTF-8 output:

1 public void doGet (HttpServletRequest request, HttpServletResponse response) 2 throws ServletException, IOException {3 4 // notify the client to check the UTF-8 code table 5 response. setContentType ("text/html; charset = UTF-8"); 6 7 // or: 8 // response. setHeader ("Content-Type", "text/html; charset = UTF-8"); 9 10 String str = "Oh Hahahaha"; 11 OutputStream out = response. getOutputStream (); 12 out. write ("</br> <div align = \" center \ "style = \" font-size: 25px; color: red \ "> ". getBytes (); 13 14 out. write (str. getBytes ("UTF-8"); 15 16 out. write ("</div> ". getBytes (); 17 out. close (); 18}

 

Byte stream output number:

1 public void doGet (HttpServletRequest request, HttpServletResponse response) 2 throws ServletException, IOException {3 response. setHeader ("Content-Type", "text/html; charset = UTF-8"); 4 5 int I = 98; 6 OutputStream out = response. getOutputStream (); 7 8 out. write ("</br> <div align = \" center \ "style = \" font-size: 25px; color: red \ ">" 9. getBytes (); 10 11 // out. write (I); the output is 12 13 // The output is 9814 out. write (I + ""). getBytes (); 15 16 out. write ("</div> ". getBytes (); 17 out. close (); 18}

 

The callback stream is encoded as UTF-8 and output:

1 public void doGet (HttpServletRequest request, HttpServletResponse response) 2 throws ServletException, IOException {3 // notify the client to check the UTF-8 code table 4 response. setContentType ("text/html; charset = UTF-8"); 5 // or: 6 // response. setHeader ("Content-Type", "text/html; charset = UTF-8"); 7 8 String str = ""; 9 PrintWriter out = response. getWriter (); 10 out. write ("</br> <div align = \" center \ "style = \" font-size: 25px; color: red \ "> "); 11 12 out. write (str); 13 14 out. write ("</div>"); 15 out. flush (); 16 out. close (); 17}

 

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.