Servlet输出中文乱码

设置response编码,需要注意的是,要先设置编码,然后再获取输出流

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    resp.setCharacterEncoding("UTF-8");
    resp.setHeader("Content-Type","text/html;charset=UTF-8");
    resp.setContentType("application/json;charset=UTF-8");
    PrintWriter out = resp.getWriter();
}

  目录