1.对于post请求的处理方式,在web.xml添加拦截器
<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.对于get请求有两种处理方式
(1)在server.xml 修改配置,添加URIEncoding="utf-8"
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="utf-8"/>
(2)在代码中手动进行处理,重新以UTF-8形式编码。但是这种方式比较麻烦,每次都要这么处理。
new String(parameter.getBytes("ISO-8859-1"),"UTF-8")