tomcat上json数据乱码问题的解决

来源:互联网 发布:体育网络电视 编辑:程序博客网 时间:2024/06/06 04:18

在jQuery提交到servlet时,在jQuery中确保:

contentType:"application/x-www-form-urlencode; charset=UTF-8"

在Servlet中:

request.setCharacterEncoding("UTF-8");

接收到的字符如果仍为乱码,则

String strRe = new String(request.getParameter("xxx").getBytes("ISO-8859-1"),"UTF-8");

这样在Servlet中就不会是乱码了。

同时在Servlet 中写入:

response.setContentType("application/json; charset=UTF-8");  

我使用的是 JSON的,如果不是,后面的可用:

application/x-www-form-urlencode; charset=UTF-8

可防止在JSP页面上显示乱码

0 0
原创粉丝点击