- 1 数据库乱码问题
我用的是mysql数据库,通过阿里的jedis连接池获取配置文件进行动态加载
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql:///数据库名?characterEncoding=utf-8
username=用户名
password==密码
# 初始化连接数量
initialSize=5
# 最大连接数
maxActive=10
# 最大等待时间
maxWait=3000
在数据库名后面记得加characterEncoding=utf-8
- 2 tomcat get请求乱码问题
- Tomcat配置:
更改两个位置的配置文件
1.server.xml
<Connector port='8080' protocol="HTTP/1.1" connectionTimeout="20000" redirectProt="8443" URIEncoding="utf-8"/>
是为了解决get请求处理中文数据乱码问题!
- post请求乱码可以自定义一个过滤器
public void doFilter(ServletRequest req, ServletResponse rep, FilterChain filterChain) throws IOException, ServletException {
//将父接口转为子接口
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) rep;
//获取请求方法
String method = request.getMethod();
//解决post请求中文数据乱码问题
if(method.equalsIgnoreCase("post")){
request.setCharacterEncoding("utf-8");
}
//处理响应乱码
response.setContentType("text/html;charset=utf-8");
filterChain.doFilter(request,response);
}