[size=xx-large][color=orange][b]乱码解决[/b][/color][/size]
[size=medium][color=red][b]1.超链接发送参数:[/b][/color][/size]
[size=medium][color=red][b]2.服务器配置编码[/b][/color][/size]
[size=medium][color=red][b]3.DB编码[/b][/color][/size]
[size=medium][color=red][b]4.IDE编码[/b][/color][/size]
[size=medium][color=red][b]5.页面编码[/b][/color][/size]
[size=medium][color=red][b]6.配置文件:过滤器编码[/b][/color][/size]
1.spring
2.Servlet编码:
[size=medium][color=red][b]1.超链接发送参数:[/b][/color][/size]
发送的时候使用URLEncoder.encode编码,接收的时候使用URLDecoder.decode解码,都按指定的编码格式进行编码、解码,可以保证不会出现乱码
URLEncoder.encode(String s, String enc)
使用指定的编码机制将字符串转换为 application/x-www-form-urlencoded 格式
URLDecoder.decode(String s, String enc)
使用指定的编码机制对 application/x-www-form-urlencoded 字符串解码。
[size=medium][color=red][b]2.服务器配置编码[/b][/color][/size]
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" useBodyEncodingForURI="true" URIEncoding="UTF-8" />
[size=medium][color=red][b]3.DB编码[/b][/color][/size]
Mysql:
字符集:utf8 -- UTF-8 Unicode
校对:utf8_general_ci
[size=medium][color=red][b]4.IDE编码[/b][/color][/size]
1.Eclipse工作环境:windows--Preferences--workspaces--text file encoding
2.项目编码:右击--Properties--Resource--text file encoding
[size=medium][color=red][b]5.页面编码[/b][/color][/size]
<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
<link href="/css/main.css" type="text/css" rel="stylesheet"/>
</head>
[size=medium][color=red][b]6.配置文件:过滤器编码[/b][/color][/size]
1.spring
<filter>
<filter-name>CharacterEncodingFilter</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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2.Servlet编码:
1)web.xml:
<!--中文编码过滤器-->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>com.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GB2312</param-value>
</init-param>
<init-param>
<param-name>ignore</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<servlet-name>/*</servlet-name>
</filter-mapping>
2)CharacterEncodingFilter.java:
/** 编码过滤器 */
public class CharacterEncodingFilter implements Filter {
String encoding = null;
FilterConfig filterConfig = null;
/** 初始化方法 */
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
}
/** 过滤处理方法 */
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
if (encoding!=null){
//对请求进行编码设置
request.setCharacterEncoding(encoding);
response.setCharacterEncoding(encoding);
}
//将处理权转交给下一个处理器
chain.doFilter(request,response);
}
/** 销毁方法 */
public void destroy() {
this.encoding = null;
this.filterConfig = null;
}
}