解决浏览器中文乱码
在html文件中的head标签中添加<meta charset="utf-8">
就可以解决
servlet中以get方式提交的数据
在tomcat7会出现乱码,在tomcat8中不会出现乱码
解决办法:
在tomcat7的安装路径中,找到conf文件夹下的server.xml配置文件,进行修改
在connector节点标签中添加以下内容:
useBodyEncodingForURL="true"
servlet中以post方式提交的数据
在tomcat7和tomcat8中都会出现乱码
解决办法:
调用HttpServletRequest对象的setCharacterEncoding方法,参数设置为utf-8
request.setCharacterEncoding="utf-8";
解决服务器返回给客户端的数据乱码问题
在执行发出响应代码之前,调用HttpServletResponse对象中的setContentType方法,并设置参数为utf-8
response.setContentType="utf-8";
html页面有乱码
在head标签中增加
<meta charset="utf-8">
jsp页面有乱码
<%@ page pageEncoding="utf-8"%>
解决post方式提交的数据有乱码
request.setCharacterEncoding("utf-8");
解决get方式提交的数据有乱码
①在server.xml中的Connector中添加属性:useBodyEncodingForURI="true"
②request.setCharacterEncoding("utf-8");
解决响应回客户端的数据有乱码
response.setContentType("text/html; charset=utf-8");