一、Window->Preferences->General ->Content Type->Text->JSP 最下面設置爲UTF-8web
二、Window->Preferences->General->Workspace 面板Text file encoding 選擇UTF-8spring
三、Window->Preferences->General->Workspace 面板Text file encoding 選擇UTF-8tomcat
在web.xml中加入:app
<filter>post
<filter-name>CharacterEncodingFilter</filter-name>編碼
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>url
<init-param>spa
<param-name>encoding</param-name>xml
<param-value>utf-8</param-value>utf-8
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
以上能夠解決post請求亂碼問題。
對於get請求中文參數出現亂碼解決方法有兩個:
修改tomcat配置文件添加編碼與工程編碼一致,以下:
<Connector URIEncoding="utf-8" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>
另一種方法對參數進行從新編碼:
String userName new
String(request.getParamter("userName").getBytes("ISO8859-1"),"utf-8")
ISO8859-1是tomcat默認編碼,須要將tomcat編碼後的內容按utf-8編碼