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