怎么设置tomcat在get请求的中文也不乱码?两种情况下配置

我们都知道,get请求和post请求的一个比较显著区别就是,在使用post请求的时候,中文不会乱码,但是在使用get请求的时候,如果url后面带有中文就会乱码了 。那么这个怎么解决呢?

怎么设置tomcat在get请求的中文也不乱码?两种情况下配置

文章插图
 
前提:配置项目的encoding编码位utf-8的前提下 。如,使用spring项目的时候,可以在spring的配置文件中添加如下的:
<filter>
<filter-name>encoding</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>encoding</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
 
两个方案:
一:使用Tomcat,将项目直接部署到tomcat中的
这种情况下,如果想要修改get请求中文参数不乱码的话,需要修改:
在tomcat的配置文件中添加如下配置:
<!-- 添加属性URIEncoding="utf-8"可以解决get乱码问题,然后重启服务器-->
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="utf-8" />
 
二:如果使用的是maven 插件运行tomcat的话,就需要在pom.xml的maven插件中配置编码方式:
<plugins>
<!-- maven项目的热部署使用的插件 使用tomcat7的插件的话,运行时设置部署命令是需要使用tomcat7:deploy -->
<plugin>
<groupId>org.Apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<uriEncoding>UTF-8</uriEncoding> <!--配置编码方式为UTF-8-->
<url>http://localhost:8080/manager/text</url>
<path>/</path>
<server>tomcat</server>
<username>tomcat</username>
<password>123456</password>
</configuration>
</plugin>

【怎么设置tomcat在get请求的中文也不乱码?两种情况下配置】


    推荐阅读