直接使用

1
String email = request.getParameter("email");

接收到email是乱码,需要转换

1
email = new String(request.getParameter("email").getBytes("ISO-8859-1"),"UTF-8");

这样麻烦还需要处理异常。

还可以这么解决:

web.xml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
<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>
</filter>
<filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>