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

针对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"即可。

今天到此。


猜你喜欢

转载自blog.csdn.net/yufan_xiaowu/article/details/51248774