前台用的是html页面:
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
....
var url = "addBlog.do?&blogcontent=中文测试";
url = encodeURI(url);  //加不加这个后台打出来的都是4个问号
//url = encodeURI(encodeURI(url));  //这样写后台会报500错误
alert(url);
request.open("post",url,true);
request.onreadystatechange = addBlogReturn;
request.send(null);后台:
String blogcontentTemp = request.getParameter("blogcontent");
String blogcontent = new String(blogcontentTemp.getBytes("ISO-8859-1"),"UTF-8");
System.out.println(blogcontent);  //打出来的是4个问号也使用了servlet过滤器,编码是UTF-8
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding("UTF-8");
chain.doFilter(request, response);
}
....
为什么还是乱码呢??快崩溃了

解决方案 »

  1.   

    后台也加了下面两行也没用:
    request.setCharacterEncoding("UTF-8");
    response.setContentType("text/html;charset=UTF-8");
      

  2.   

    在过滤器中在加一个response.setCharacterEncoding("UTF-8");
      

  3.   

    String blogcontent=new String(request.getParameter("blogcontent").getBytes("ISO-8859-1"),"gb2312");
      

  4.   


    var url = "addBlog.do";var params = [];
    params.push("blogcontent", encodeURIComponent("中文测试"));
    params.push("param1", encodeURIComponent("字串二"));
    params.push("param2", encodeURIComponent("字串三"));
    // 可加入多个参数...request.open("POST", url, true); 
    request.onreadystatechange = addBlogReturn; 
    request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    request.send(params.join("&"));
    request.setCharacterEncoding("UTF-8");
    String blogcontent = request.getParameter("blogcontent");
    // ...
      

  5.   

    更正:
    var params = [];
    params.push("blogcontent=" + encodeURIComponent("中文测试"));
    params.push("param1=" + encodeURIComponent("字串二"));
    params.push("param2=" + encodeURIComponent("字串三"));
      

  6.   

    楼主,你的过滤器XML文件里,url-pattern这个属性里面写成/*试试看
      

  7.   

    5楼说的对,在后台打出来中文了。又到数据库里乱码了,安装时好像也是UTF-8,是不是要装GBK才行啊
      

  8.   

    utf-8可以显示中文的,可能你的数据库编码有关系,是mysql的话,改为utf-8看看,其它的具体怎么改自己研究下吧
      

  9.   

    ajax只支持UTF-8编码,其他编码都不支持。
      

  10.   

    我一般也不用啥过滤器,
    前台两遍encodeURI
    后台一遍decode就齐了
      

  11.   

    AJAX只支持UTF-8编码.
    前台发送请求之前得设置一下编码方式:request.setCharacterEncoding("UTF-8");
      

  12.   

    tomcat里加个东西URIEncoding="UTF-8"

    <Connector port='8080' protocol='HTTP/1.1'
                   connectionTimeout='20000'
                   redirectPort='8443' URIEncoding='UTF-8' />
      

  13.   

    页面终于没乱码了,谢谢大家先原来是安装mysql的问题。但是在mysql客户端里的是乱码,传到页面却不是乱码,这又是怎么回事呢