post乱码、get乱码问题如何解决?

迷南。 2022-09-24 12:25 357阅读 0赞

post乱码:

在web.xml文件中加入字符编码过滤器

  1. <!-- post乱码过滤器 -->
  2. <filter>
  3. <filter-name>CharacterEncodingFilter</filter-name>
  4. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  5. <init-param>
  6. <param-name>encoding</param-name>
  7. <param-value>UTF-8</param-value>
  8. </init-param>
  9. </filter>
  10. <filter-mapping>
  11. <filter-name>CharacterEncodingFilter</filter-name>
  12. <url-pattern>/*</url-pattern>
  13. </filter-mapping>

get乱码:

方法1.
修改tomcat配置文件添加编码与工程编码一致,如下:
<Connector URIEncoding="utf-8" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>
方法2.
对参数进行重新编码:

  1. String userName =new String(request.getParamter("userName").getBytes("ISO8859-1"),"utf-8");

ISO8859-1 是tomcat默认编码,需要将tomcat编码后的内容按utf-8编码

发表评论

表情:
评论列表 (有 0 条评论,357人围观)

还没有评论,来说两句吧...

相关阅读