jsp中乱码解决方案(转载)

来源:互联网 发布:百度网盘mac旧版本 编辑:程序博客网 时间:2024/05/20 19:18

1、JSP输出中文的乱码
 在JSP页面头部加上语句
 在Servlet中使用httpServletResponse.setContentType("text/html;charset=utf-8")
 最好同时在JSP页面的head部分加上


2、在获取表单提交数据时的中文乱码
 String str=request.getParameter("chStr");String str=new String(str.getBytes("ISO-8859-1"),"utf-8")
或者:将tomcat/webapps/servlets-examples/Web-INF/classes/filters/目录下的文件SetCharacterEncodingFilter.class拷

贝到
自己应用的/Web-INF/classes/ch2/util/filter目录下,然后再在web.xml文件的后面加上如下配置代码:

 Set Character Encoding
 ch2.util.filter.SetCharacterEncodingFilter
 
  encoding
  UTF-8
 


 Set Character Encoding
 /*

 

3、URL中的中文问题
打开tomcat安装目录下的/conf/server.xml文件,找到Connector块,往其中添加URIEncoding="utf-8"/>

 

4、数据库访问时的乱码
在使用JDBC链接MySQL数据库时,连接字符串写成如下形式可以避免一些中文问题
jdbc://mysql://hostname:port/DBname?user=username&password=pwd&useUnicode=true&character Encoding=utf-8