在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(); }
之後得在細節上多當心才行。html