servlet/jquery/json 防止乱码

来源:互联网 发布:简谱制谱mac 编辑:程序博客网 时间:2024/06/05 20:06

在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页面上显示乱码