1.响应乱码的处理:
1.1:第一种解决响应乱码:设置响应编码,设置浏览器的编码也是utf-8
response.setCharacterEncoding("utf-8");
1.2:第二种解决响应乱码:设置响应的内容类型及浏览器展示信息的编码方式
response.setContentType("text/html;charset=utf-8");
- 请求乱码的处理:
2.1:post请求乱码:
//第一种:设置post提交请求中数据编码
request.setCharacterEncoding(“utf-8”);
2.2:get请求乱码:在tomcat的conf文件夹中server.xml中端口号的地方设置编码
<Connector URIEncoding="UTF-8" connectionTimeout="20000" port="8888" protocol="HTTP/1.1" redirectPort="8443"/>
2.3:get和post请求乱码都可以处理
//先接收请求的数据
String sname=request.getParameter(“uname”);
//将接收数据重新编码
sname=new String(sname.getBytes(“ISO-8859-1”), “utf-8”);