在Servlet中出现一个输出中文乱码的问题(已经解)。

在Servlet中出现一个输出中文乱码的问题,已经解。
	@Override
	public void doPost(HttpServletRequest reqeust, HttpServletResponse response)
			throws ServletException, IOException {
		
                //PrintWriter out = response.getWriter();在还没有给response指定编码格式时就获取了他的输出流,所以一直乱码

		reqeust.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter(); //在设置完编码以后在获取输出流就好了。
		jsonService = new JsonService();
		String jsonString = JsonTools.createJsonString("persons", jsonService.getPersonList());
		out.println(jsonString);
		out.flush();
		out.close();
        }

以后得在细节上多小心才行。

  

原文地址:https://www.cnblogs.com/xiangxiaodong/p/4575140.html