-
字符集及其编码
(1) ASCII
7bit,包括英文和控制字符
(2) ISO8859-1
8bit, 添加欧洲语言字符,兼容ASCII
(3) GB2312
16bit, 添加常用汉字、日文、数字等, 兼容ASCII
(4) GBK
16bit, 添加不常用汉字、图形等,兼容GB2312
(5) GB18030
添加少数民资汉字,兼容GBK
(5) Unicode
统一全球编码集
UTF-8,可变长编码,每8个bit作为一组,每个字符可能是1/2/3个byte组成,第一个byte的前端代表了这个字符有几个字节;
UTF-16, 可变长编码,每16个bit作为一组
-
JSP页面乱码
添加
<%@page pageEncoding="UTF-8" contentType="text/html;charset=utf-8" %>
-
Servlet页面显示乱码
添加
servletResponse.setCharacterEncoding("UTF-8"); servletResponse.setContentType("text/html;charset=utf-8");
更好的方式是添加filter
web.xml(放在最前面)
<filter> <filter-name>EncodingFilter</filter-name> <filter-class>filter.EncodingFilter</filter-class> <init-param> <param-name>characterEncoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>contentType</param-name> <param-value>text/html;charset=utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>EncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
EncodingFilter.java
public class EncodingFilter implements Filter { private String characterEncoding; private String contentType; public void init(FilterConfig filterConfig) throws ServletException { this.characterEncoding = filterConfig.getInitParameter("characterEncoding"); this.contentType = filterConfig.getInitParameter("contentType"); } public void destroy() { } public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { servletRequest.setCharacterEncoding(this.characterEncoding); servletResponse.setCharacterEncoding(this.characterEncoding); servletResponse.setContentType(this.contentType); filterChain.doFilter(servletRequest, servletResponse); } }
-
post表单传递参数乱码(Servlet/JSP)
Servlet:
request.setCharacterEncoding("UTF-8");
JSP:
<%request.setCharacterEncoding("UTF-8");%>
-
URL传递参数乱码
例如: http://localhost:8080/project?name=张三
解决办法
修改 conf/server.xml 的
网友评论