<!-- characterEncodingFilter字符編碼過濾器 --> <filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <!--要使用的字符集,通常咱們使用UTF-8(保險起見UTF-8最好)--> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <!--是否強制設置request的編碼爲encoding,默認false,不建議更改--> <param-name>forceRequestEncoding</param-name> <param-value>false</param-value> </init-param> <init-param> <!--是否強制設置response的編碼爲encoding,建議設置爲true,下面有關於這個參數的解釋--> <param-name>forceResponseEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <!--這裏不能留空或者直接寫 ' / ' ,否者不起做用--> <url-pattern>/*</url-pattern> </filter-mapping>
打開該類源碼,能夠看到該類有三個類屬性html
private String encoding; //要使用的字符集,通常咱們使用UTF-8(保險起見UTF-8最好) private boolean forceRequestEncoding = false; //是否強制設置request的編碼爲encoding private boolean forceResponseEncoding = false; //是否強制設置response的編碼爲encoding
主要方法只有一個,也就是下面這個,代碼邏輯很簡單,入註釋所解釋java
@Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { String encoding = getEncoding(); if (encoding != null) { //若是設置了encoding的值,則根據狀況設置request和response的編碼 //若設置request強制編碼或request自己就沒有設置編碼 //則設置編碼爲encoding表示的值 if (isForceRequestEncoding() || request.getCharacterEncoding() == null) { request.setCharacterEncoding(encoding); } //若設置response強制編碼,則設置編碼爲encoding表示的值 if (isForceResponseEncoding()) { //請注意這行代碼,下面有額外提醒 response.setCharacterEncoding(encoding); } } filterChain.doFilter(request, response); }
if (isForceResponseEncoding()) { response.setCharacterEncoding(encoding); }
是在web
filterChain.doFilter(request, response);
以前執行的,這也就是說這段代碼的做用是設置response的默認編碼方式,在以後的代碼裏是能夠根據需求設置爲其餘編碼的,即這裏設置的編碼可能不是最終的編碼
對於get請求中文參數出現亂碼解決方法有兩個:spring
修改tomcat配置文件添加編碼與工程編碼一致,以下:tomcat
<Connector URIEncoding="utf-8" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>app
另一種方法對參數進行從新編碼:ide
String userName = new String(request.getParamter("userName").getBytes("ISO8859-1"),"utf-8")this
ISO8859-1是tomcat默認編碼,須要將tomcat編碼後的內容按utf-8編碼編碼
3、能夠自定義攔截器(在web.xml中配置)
<filter> <filter-name>SetCharacterEncodingFilter</filter-name> <filter-class>com.itwang.filter.SetCharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>SetCharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
public class SetCharacterEncodingFilter implements Filter { private FilterConfig filterConfig; public void init(FilterConfig filterConfig) throws ServletException { this.filterConfig = filterConfig; } public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletRequest request; HttpServletResponse response; try { request = (HttpServletRequest) req; response = (HttpServletResponse) res; } catch (ClassCastException e) { throw new ServletException("non-HTTP request or response"); } String encoding = filterConfig.getInitParameter("encoding"); if(encoding==null){ encoding = "UTF-8"; } //POST: request.setCharacterEncoding(encoding); response.setCharacterEncoding(encoding); response.setContentType("text/html;charset="+encoding); chain.doFilter(request, response); } public void destroy() { } }
參考:https://blog.csdn.net/lianjunzongsiling/article/details/77926370