1、頁面編碼html
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
2、URL中的亂碼java
改tomcat中server.xml中Connector的port=「8080」,加上一個 URIEncoding=」utf-8」mysql
3、配置過濾器,指定全部請求的編碼web
(1)配置spring的編碼過濾器,爲了防止spring中post方式提交的時候中文亂碼,方法:修改web.xml文件,添加spring的編碼過濾器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> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
(2)配置編碼過濾器,方法:先建立filter類,再修改web.xml文件,注意的是放在spring的編碼過濾器以後sql
filter類:數據庫
package com.qiyuan.filter; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class EncoidingFilter implements Filter { private String encoding=""; @Override public void destroy() { // TODO Auto-generated method stub } //過濾方法 是否往下執行 @Override public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain chain) throws IOException, ServletException { HttpServletRequest request=(HttpServletRequest)arg0; HttpServletResponse response=(HttpServletResponse)arg1; request.setCharacterEncoding(encoding); response.setCharacterEncoding(encoding); //過濾通行證 chain.doFilter(request, response); } //根據web.xml文件的配置進行初始化 @Override public void init(FilterConfig arg0) throws ServletException { this.encoding = arg0.getInitParameter("Encoding"); } }
修改web.xml,添加以下配置:tomcat
<!-- 配置編碼過濾 --> <filter> <filter-name>EncoidingFilter</filter-name> <filter-class>com.qiyuan.filter.EncoidingFilter</filter-class> <init-param> <param-name>Encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>EncoidingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
4、文件編碼mvc
將文件另存爲utf-8格式app
5、數據庫編碼
鏈接字符串指定編碼格式
public static String URL="jdbc:mysql://127.0.0.1:3306/mvcdb?useUnicode=true&characterEncoding=UTF-8"
建立數據庫的時候指定utf-8編碼格式
6、其餘參考資料
http://www.cnblogs.com/javahr/p/8268983.html