java web 乱码终结
配置 tomcat
打开 tomcat 安装路径下的 conf/server.xml 文件,将 port 为 8080 的 connector 做如下更改:
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
URIEncoding="utf-8"
useBodyEncodingForURI="true"
redirectPort="8443" />
我们新加了URIEncoding="utf-8"
和useBodyEncodingForURI="true"
配置 .jsp
在每个 .jsp 文件中,都需要添加一条规则:<%@ page contentType="text/html;charset=UTF-8" pageEncoding="utf-8"%>
配置 springMVC(如果你使用了 springMVC)
打开 springMVC 工程下的 web.xml,增加一条 filter 规则:
<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>