Java Web乱码问题对应
1 GET请求乱码
在server.xml中配置URI编码
<Connector connectionTimeout="20000" port="8080" URIEncoding="UTF-8" protocol="HTTP/1.1" redirectPort="8443"/>
2 POST请求乱码
1>在web.xml中配置过滤器
Spring提供了实现类CharacterEncodingFilter
<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>
3 JSP显示乱码
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<meta http-equiv="Content-Type" content="text/html; harset=UTF-8">
4 数据库乱码
Mysql:在DB的URL中,添加编码设定,例:
String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8"
其他DB:创建DB和表时,指定与系统一致的编码