Printwriter output information garbled

Source: Internet
Author: User

In asynchronous mode, when JSON is returned to the foreground, printwriter is used to output information to the foreground, but garbled characters occur during the output process.

So I remembered response. setcharacterencoding ("UTF-8"); sets the page encoding and response. setcontenttype ("text/html; charset = UTF-8"); sets the content type encoding. However, the encoding fails after the experiment and garbled characters remain unchanged.

    PrintWriter out = response.getWriter();    response.setCharacterEncoding("utf-8");    response.setContentType("text/html; charset=utf-8");    out.print(json);    out.flush();    out.close();

The returned JSON is as follows:

{"Seriesdata": [22619,22671, 21908,5415, 0], "caption": "Organization Code-Annual Statistics", "xaxisdata": ["2010 ", "January 2011", "January 2012", "January 2013", "January 2014"]}

After checking, it is found that printwriter will first get the project encoding and set characterencoding according to the encoding. Therefore, you must set the encoding before getting this printwriter object. Note the sequence as follows.

  response.setCharacterEncoding("utf-8");  response.setContentType("text/html; charset=utf-8");  PrintWriter out = response.getWriter();    out.print(json);    out.flush();    out.close();

 

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.