多读多写多记录,多学多练多思考。-----------Banana • Banuit Gang(香柚帮)
中文乱码问题
首先看jsp页面的字符集是否设置为了UTF-8
1.form表单以get方式传入后台后,接收参数出现乱码,把method改为post方式
2.前台参数提交到后台,接收之后出现乱码,在接收的第一个参数前面添加
response.setCharacterEncoding("UTF-8");
3.在struts.xml文件中配置i18n常量
<constant name="struts.i18n.encoding" value="UTF-8" />
4.创建Java类进行自定义转字符集,然后将此类配置到web.xml的filter中
5.在tomcat服务器下的server.xml文件中的
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
添加URIEncoding="UTF-8"
6.解决MySQL数据库中文问题主要在JDBC驱动的URL上,例如:
jdbc:mysql://localhost/xxx?user=xxx&password=xxx&useUnicode=true&characterEncoding=UTF-8
7.js传值后台中文乱码解决
在“.jsp”页面中,需要通过js将值传递到后台,后台根据传递的值进行数据查询时,通过.jsp的js进行编码,后台java解码,可以解决
【.jsp】
var name = encodeURI(document.getElementById("name").value);
name = encodeURI(name); //需要通过两次编码
【java代码】
name = URLDecoder.decode(name , "utf-8");
8.两个HTML页面之间传值时中文出现乱码,怎样解决???
可以在页面a用encodeURI 编码url 在b页面用decodeURI解码url
页面a
function test() {
var parm1=document.getElementById("test1").value;
var parm2=document.getElementById("test2").value;
var parm3=document.getElementById("test3").value;
var myurl="b.html"+"?"+"parm1="+parm1+"&parm2="+parm2+"&parm3="+parm3;
// encodeURI 编码
window.location.assign(encodeURI(myurl));
}
页面b
function getvalue(){
//b.html?parm1=myimg1&parm2=myimg2
//获得第一个属性
//decodeURI 解码
var url=decodeURI(location.href);
var tmp1=url.split("?")[1];
var tmp2=tmp1.split("&")[0];
var tmp3=tmp2.split("=")[1];
var parm1=tmp3;
...
}
9. IDEA启动Tomcat时,Server、Tomcat Localhost Log、Tomcat Catalina Log乱码问题
将tomcat安装目录下conf文件夹里的logging.properties文件的编码格式从UTF-8改为GBK虽然解决了localhost和catalina log的乱码问题,但是却造成了server的乱码,所以不要修改这里,否则server不管怎么改都是乱码,简单的解决三种乱码问题的话,只需要在idea的tomcat VM options里配置-Dfile.encoding=UTF-8