-
乱码问题分类
-
请求乱码
GET请求
修改tomcat中server.xml配置文件,在8080端口处增加URIEncoding="UTF-8"
POST请求:
在第一次获取请求参数之前添加:
request.setCharacterEncoding("UTF-8");
如何获取请求参数之前:
设置编码filter,拦截所有请求,添加 request.setCharacterEncoding("UTF-8");
<!-- 配置字符集 --> <filter> <filter-name>encodingFilter</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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> |
-
响应乱码
response.setCharacterEncoding("text/html;charset=utf-8");