五、servlet乱码解决

1、提交数据:

Post提交 设置:req.setCharacterEncoding(“utf-8”);

get提交 需要转换:

String name = req.getParameter(“name”);
Name = new String(name.getBytes(“iso-8859-1”),”utf-8”);

还可以设置Tomcat:

<Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" URIEncoding=”utf-8” />

2、响应数据:

//设置的在网络传递编码,默认为iso-8859-1
resp.setCharacterEncoding("utf-8");
//设置浏览器 
resp.setContentType("text/html;charset=utf-8");

解决不同浏览器编码不一致的问题:

<Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" URIEncoding=”utf-8” useBodyEncodingForURI=”ture” />

猜你喜欢

转载自blog.csdn.net/qq_40638598/article/details/85924468