1 处理乱码方式:
1 连接数据库的时候
jdbc.properties:jdbc:mysql://localhost:3306/myproject?useUnicode=true&characterEncoding=utf8
2 修改mysql配置文件my.ini/my.cnf default-character-set=utf8
3 修改tomcat中的配置文件server.xml改为
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8"/>
4 在web.xml配置spring过滤器
<filter>
<filter-name>char</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>char</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
5 设置mysql字符集
show variables like‘character_set_%‘;
把显示的character_set_**全都设置成utf8,即是:set character_set_serve=‘utf8‘
show variables like ‘%collation%‘;
把显示的都设置成为如下:set collation_server=‘utf8_general_ci‘
6.取值时HTtp(post)请求中的中文乱码
在Servlet页面中 添加
request.setCharacterEncoding("utf-8");
user_name = request.getParameter("user_name") == null? "" : java.net.URLDecoder.decode(request.getParameter("user_name"), "UTF-8");
7。前台返回????
后台赋值:errorMessage = URLEncoder.encode("redis中没有该定期数据信息", "UTF-8").replace("+", "%20");
前台取值:decodeURIComponent(errorMessage)