Dark Horse day03 Character stream garbled solution method and principle analysis

Source: Internet
Author: User

1. Garbled Characters of character stream

Response.getwriter (). Write ("China"); This line of code will first write China to the buffer and then encode it using a foreigner's favorite iso-8859-1.

The browser then decodes using the local encoding table. Therefore, garbled characters are generated.

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;public class Demo7servlet extends HttpServlet {public void doget ( HttpServletRequest request, HttpServletResponse response) throws Servletexception, IOException {response.getwriter (). Write ("China");} public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { Doget (request, Response);}}

Operation Result:


Then the solution: is to tell the server to use GBK encoding: response.setcharacterencoding ("GBK");

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;public class Demo7servlet extends HttpServlet {public void doget ( HttpServletRequest request, HttpServletResponse response) throws Servletexception, IOException {// Tell the server to encode response.setcharacterencoding ("GBK") using GBK, Response.getwriter (). Write ("China"); public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { Doget (request, Response);}}

Operation Result:


2. If you tell the server to use Utf-8 for encoding, but the browser will use the default encoding GBK, so in order to prevent garbled, so you need to tell the browser to use Utf-8 to decode

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;public class Demo7servlet extends HttpServlet {public void doget ( HttpServletRequest request, HttpServletResponse response) throws Servletexception, IOException {// Inform the server to encode response.setcharacterencoding ("Utf-8") using GBK,//Tell the browser to decode Response.setheader using Utf-8 ("Content-type", "text /html;charset=utf-8 "); Response.getwriter (). Write (" China "); public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { Doget (request, Response);}}
Operation Result:

3.sun Company uses Response.setcontenttype ("Text/html;charset=utf-8") for ease of use, tells the browser to decode using Utf-8, but the underlying can know in advance what encoding the server uses to encode.

So this is the equivalent of telling the server to use ut8-8 encoding and also telling the browser to decode it using Utf-8. This sentence replaces the effect of these two words:

Tell the server to encode using GBK
Response.setcharacterencoding ("Utf-8");
Tell the browser to decode using Utf-8
Response.setheader ("Content-type", "text/html;charset=utf-8");
Response.getwriter (). Write ("China");


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;public class Demo7servlet extends HttpServlet {public void doget ( HttpServletRequest request, HttpServletResponse response) throws Servletexception, IOException {//that tells the server to use ut8-8 encoding, It also tells the browser to decode using Utf-8. Response.setcontenttype ("Text/html;charset=utf-8"); Response.getwriter (). Write ("China"); public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { Doget (request, Response);}}

Operation Result:



Dark Horse day03 Character stream garbled solution method and principle analysis

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.