tomcat小知识-请求乱码解决方案

xiaoxiao2021-02-27  153

与页面交互过程中的乱码问题


1.post请求方式乱码解决方案


在web项目中的web.xml文件中配置过滤器,CharacterEncodingFilter


相应jar不能少



在web项目中的web.xml文件中配置项如下

<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>

2. get请求的乱码解决方案


2.1 在tomcat服务器中,server.xml文件中找到图示中的代码片段,并添加如下代码**


URIEncoding="UTF-8"

最终效果图,保存退出即可

<Connector URIEncoding="UTF-8" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>


2.2 单个参数的get请求乱码解决方案:

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

此代码结束后: username为utf-8

转载请注明原文地址: https://www.6miu.com/read-12157.html

最新回复(0)