`

Java Web开发中产生乱码的三种情况以及解决办法

阅读更多
1、Jsp页面上显示乱码:
    解决:在Jsp页面加上头信息<%@ pageEncoding="utf-8" %>

2、调用servlet时候的乱码:       
    解决:response.ContentType("text/html");

3、在Jsp页面通过action向Servlet提交参数servlet接受的参数为乱码:   
    解决:request.setCharacterEncoding("utf-8"); (更好的解决办法:使用过滤器)

4、在Jsp的页面中通过超链接传参数,例如:<a href="ss.jsp?userName=zhangsan"></a> 接收到的参数为乱码。   
    解决:在tomcat的配置文件servlet.xml中找到 :

    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" URIEncoding="UTF-8"/>   URIEncoding="UTF-8"是我们加入的。

5、手动转码:
String temp = new String(key.getBytes("iso-8859-1"));
例如:从页面传过来的参数key是乱码,不妨用上面的方式试一下。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics