JSP的中文乱码问题一直是我的问题,曾经有段时间解决过,但是输入中文的生僻字的时候显示依然不正常。前几天又把生僻字的问题解决了,我想中文乱码的问题应该暂时告一段落了。中文乱码问题的核心是因为编码的问题,Tomcat中的编码是ISO-8859-1,而我们中文的编码一般是GBK跟GB2312,Linux的编码是UTF-8,XML的编码是UTF-8,网络中传输的编码是UTF-8,那我们的项目编码应该是什么?毫无疑问UTF-8才是正确的选择。
UTF-8的编码方式要从开始建项目的时候就确立,因此建立的项目编码格式首先就是UTF-8的。然后页面的编码方式自然也要是UTF-8的,但是页面中要加入以下的2行代码:
<%@ page contentType="text/html; charset=UTF-8" %>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
在上面打中文等是不受影响的。最后就是要加个过滤器了,过滤所有的网页,所有的网页经过过滤器的时候自动转成了UTF-8的编码。下面是过滤器的代码:
package
XX.XX;
import java.io. * ;
import java.util. * ;
import javax.servlet. * ;
import javax.servlet.http. * ;
public class filter
extends HttpServlet implements Filter {
// 编码的字符串
protected String encoding = null;
// 过滤器的配置
protected FilterConfig filterConfig = null;
// 是否忽略客户端的编码
protected boolean ignore = true;
// 销毁过滤器
public void destroy() {
this.encoding = null;
this.filterConfig = null;
}
// 过滤方法
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// 如果使用过滤器,忽略客户端的编码,那么使用通过过滤器设定编码
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null){
request.setCharacterEncoding(encoding);
}else{
request.setCharacterEncoding("UTF-8");
}
}
// 传送给下一个过滤器
chain.doFilter(request, response);
}
// 初始化过滤器
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
String value = filterConfig.getInitParameter("ignore");
if (value == null)
this.ignore = true;
else if (value.equalsIgnoreCase("true"))
this.ignore = true;
else if (value.equalsIgnoreCase("yes"))
this.ignore = true;
else
this.ignore = false;
}
// 返回过滤器设定的编码
protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}
}
import java.io. * ;
import java.util. * ;
import javax.servlet. * ;
import javax.servlet.http. * ;
public class filter
extends HttpServlet implements Filter {
// 编码的字符串
protected String encoding = null;
// 过滤器的配置
protected FilterConfig filterConfig = null;
// 是否忽略客户端的编码
protected boolean ignore = true;
// 销毁过滤器
public void destroy() {
this.encoding = null;
this.filterConfig = null;
}
// 过滤方法
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// 如果使用过滤器,忽略客户端的编码,那么使用通过过滤器设定编码
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null){
request.setCharacterEncoding(encoding);
}else{
request.setCharacterEncoding("UTF-8");
}
}
// 传送给下一个过滤器
chain.doFilter(request, response);
}
// 初始化过滤器
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
String value = filterConfig.getInitParameter("ignore");
if (value == null)
this.ignore = true;
else if (value.equalsIgnoreCase("true"))
this.ignore = true;
else if (value.equalsIgnoreCase("yes"))
this.ignore = true;
else
this.ignore = false;
}
// 返回过滤器设定的编码
protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}
}
当然相应的,在web.xml中也要相应过滤器配置
<
filter
>
< filter - name > filter </ filter - name >
< filter - class > net.dswhb.servlet.filter </ filter - class >
< init - param >
< param - name > encoding </ param - name >
< param - value > UTF - 8 </ param - value >
</ init - param >
< init - param >
< param - name > ignore </ param - name >
< param - value > true </ param - value >
</ init - param >
</ filter >
< filter - mapping >
< filter - name > filter </ filter - name >
// 这里写的是过滤哪些页面,所有页面就是/*
< url - pattern > /*</url-pattern>
</filter-mapping>
至此,我的乱码问题全部解决,那些比较少间的字如'姮'也能正常显示了
< filter - name > filter </ filter - name >
< filter - class > net.dswhb.servlet.filter </ filter - class >
< init - param >
< param - name > encoding </ param - name >
< param - value > UTF - 8 </ param - value >
</ init - param >
< init - param >
< param - name > ignore </ param - name >
< param - value > true </ param - value >
</ init - param >
</ filter >
< filter - mapping >
< filter - name > filter </ filter - name >
// 这里写的是过滤哪些页面,所有页面就是/*
< url - pattern > /*</url-pattern>
</filter-mapping>
至此,我的乱码问题全部解决,那些比较少间的字如'姮'也能正常显示了