关于Java EE项目GET,POST中文编码问题

来源:互联网 发布:a star算法的流程图 编辑:程序博客网 时间:2024/05/24 15:39

针对POST方式获取中文的编码,需要在web.xml增加:

<span style="white-space:pre"></span><filter>        <span style="white-space:pre"></span><filter-name>CharacterEncoding</filter-name>       <span style="white-space:pre"></span><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>        <span style="white-space:pre"></span><init-param>            <span style="white-space:pre"></span><param-name>encoding</param-name>            <span style="white-space:pre"></span><param-value>UTF-8</param-value>        <span style="white-space:pre"></span></init-param>        <span style="white-space:pre"></span><init-param>            <span style="white-space:pre"></span><param-name>forceEncoding</param-name>            <span style="white-space:pre"></span><param-value>true</param-value>        <span style="white-space:pre"></span></init-param>    <span style="white-space:pre"></span></filter>    <span style="white-space:pre"></span><filter-mapping>        <span style="white-space:pre"></span><filter-name>CharacterEncoding</filter-name>        <span style="white-space:pre"></span><url-pattern>/*</url-pattern>    <span style="white-space:pre"></span></filter-mapping>
这个设置是针对POST请求的,tomacat对GET和POST请求处理方式是不同的,要处理针对GET请求的编码问题,则需要改tomcat的server.xml配置文件,如下:
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" useBodyEncodingForURI="true"/>
//增加:useBodyEncodingForURI="true"


PS:配置useBodyEncodingForURI="true"后,可以解决普通get请求的中文乱码问题,但是对于通过ajax发起的get请求中文依然会乱码,请把useBodyEncodingForURI="true"改为URIEncoding="UTF-8"即可。

今天到此。


0 0
原创粉丝点击