tomcat 7 GET請求中文亂碼
方法一:
修改server.xml: URIEncoding="utf-8"
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8" />
方法二:
String parameter = request.getParameter("auth_id");
String string = new String(parameter.getBytes("iso-8859-1"), "utf-8");
(1)tomcat7,URL中引數的預設解碼是ISO-8859-1,而tomcat8的預設解碼為utf-8。
(2)在過濾器中使用 request.setCharacterEncoding() 只能用來處理 POST 請求,對 GET 則無效。
(3)tomcat亂碼的終極解決辦法:
tomcat7:設定URIEncoding + Post的過濾器
tomcat8:只要設定一個Post過濾器就可以了,tomcat9跟tomcat8是一樣的。
<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>