servlet 返回数据乱码(或都是问号???)

解决方法

//将返回的数据编码成utf-8格式的字节流,与下面的content-type区别
response.setCharacterEncoding("utf-8");
//告诉浏览器返回的数据是json格式,且以utf-8格式解码
response.setContentType("application/json;charset=utf-8");//response.setHeader("Content-type", "text/json;charset=UTF-8");
PrintWriter writer = response.getWriter(); //必须在setCharacterEncoding后面
writer.print(JSON.toJSONString(RespBean.bad("请先登录")));
writer.flush();

你可能感兴趣的:(Spring,#,servlet)