Perfect solution to a problem in Servlet where the output of Chinese characters is garbled

  • 2020-05-27 05:44:12
  • OfStack

As follows:


 in Servlet Appear in the 1 The problem of output Chinese garbled code has been solved.  
 @Override
 public void doPost(HttpServletRequest reqeust, HttpServletResponse response) 
   throws ServletException, IOException { 
   
    //PrintWriter out = response.getWriter(); Not yet response Gets its output stream when you specify the encoding format, so 1 Straight garbled  
 
  reqeust.setCharacterEncoding("utf-8"); 
  response.setContentType("text/html;charset=utf-8"); 
  response.setCharacterEncoding("utf-8"); 
  PrintWriter out = response.getWriter(); // Just get the output stream after you've set up the encoding.  
  jsonService = new JsonService(); 
  String jsonString = JsonTools.createJsonString("persons", jsonService.getPersonList()); 
  out.println(jsonString); 
  out.flush(); 
  out.close(); 
  } 

Related articles: