- Get请求
在Tomcat下找到service.xml配置文件加入URIEncoding=”UTF-8” URLEncoding=”UTF-8”
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"
URIEncoding="UTF-8" URLEncoding="UTF-8"/>
也可以在后台进行转换,不过这样在中文传递较多的情况下不建议,由于get方式提交的参数编码,只支持iso8859-1编码。所以,如果参数中带有中文。
在后台就需要转换编码,如下
String zhongwen = request.getParameter("zhongwen");
zhongwen = new String(zhongwen.getBytes("iso8859-1"),"UTF-8");
- Post请求
在web.xml文件中配置过滤器
<filter>
<filter-name>characterEncoding</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>characterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>