Analysis of garbled principle of black horse day03 byte output stream

Source: Internet
Author: User

Encoding and decoding consistent will not cause garbled, the browser default is the current system of the default character encoding, so when decoding will look GBK

One. Byte output stream garbled solution

1. Byte output stream Getoutputstream (). write ();

Package Com.itheima;import Java.io.ioexception;import Javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import JAVAX.SERVLET.HTTP.HTTPSERVLETRESPONSE;//encoding and decoding consistent will not cause garbled, the browser default is the default character encoding of the current system, so when decoding will check Gbkpublic class Demo6servlet Extends HttpServlet {public void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {response.getoutputstream (). Write ("China". GetBytes ());// Viewing the GetBytes method of the string class can be seen using the current system default encoding to decode the current gbk}public void DoPost (HttpServletRequest request, HttpServletResponse response) throws Servletexception, IOException {doget (request, Response);}}
Is it garbled? Answer:

Will not garbled, because GetBytes code will check GBK, browser decoding will use GBK. So it won't be garbled!

2. If you specify the encoding

Response.getoutputstream (). Write ("China". GetBytes ("Utf-8"))

Must be garbled, because the encoding uses UTF-8, the browser decodes the use of the default GBK and therefore garbled


How to resolve:

Tell the browser to decode using UTF-8 encoding:

Use//Notify browser to use Utf-8 decoding

Response.setheader ("Content-type", "text/html;charset=utf-8");




Analysis of garbled principle of black horse day03 byte output stream

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.