时常在
探讨区看到有人问我的JSP显示中文有乱码怎么办,我用request得到的消费者输入的中文怎么是乱码,我把汉字写到数据库怎么是乱码,等等一些关于汉字乱码的问题 。
其实这个问题很
方便,管它汉字不汉字,还是日文,还是
其余的什么双字节的语言,我们一概把它当作UTF-8
对待 。
(一)request中的双字节文字
好下面我们就来实现在整个
利用程序中
使用UTF-8编码工作,之所以
取舍UTF-8不只仅之于上述缘由,我们晓得java的便是基于在UTF-8之上的,所以我们
取舍UTF-8应该没错^_^
我们首先把我们的.java, .jsp文件都用UTF-8编码来
保留,假如以往的没有用UTF-8
保留也无所谓,然而
提议以后写的都用UTF-8来
保留 。
并在.jsp里面写:<%@page contentType="text/html; charset=UTF-8"%>而不是<%@page contentType="text/html; charset=UTF-8"%>
而后在web.xml增加下面一段:
...
Set Character Encoding
com.redv.projects.eduadmin.util.filters.SetCharacterEncodingFilter
encoding
UTF-8
Set Character Encoding
/*
...
其中com.redv.projects.eduadmin.util.filters.SetCharacterEncodingFilter的代码如下:
package com.redv.projects.eduadmin.util.filters;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.UnavailableException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SetCharacterEncodingFilter
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 {
// Conditionally select and set the character encoding to be used
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null) {
request.setCharacterEncoding(encoding); //便是这句话在工作的啦,哈哈,它:Overrides the name of the character encoding used in the body of this request. This method must be called prior to reading request parameters or reading input using getReader().
}
}
// Pass control on to the next filter
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);
}
}
这样,我们的request
申请便是以UTT-8编码的,在JSP程序中就
可以
使用:request.getParameter("myKey")来直接得到UTF-8编码的字符串了,而不需求像这样:new String(request.getParameter("myKey").getBytes("ISO-8859-1"), "GBK")来解决那些乱码了 。
(二)数据库
解决的双字节文字
另外一个,便是写入数据库的问题,我们晓得我们在
使用mysql的时候
可以改用这样的url来
解决汉字编码问题:jdbc:mysql://localhost:3306/upas?useUnicode=true&characterEncoding=gb2312,
那么关于那些我们
无奈像mysql这样解决的怎么办呢?难道我们每次都这样写吗:
import java.sql.*;
Class.forName("org.gjt.mm.mysql.Driver");
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "");
pstmt = con.prepareStatement("SELECT f3, f4 FROM tbl1 WHERE f1 = ? AND f2 = ?");
pstmt.setString(1, new String(f1.getBytes("GBK"), "ISO-8859-1");
pstmt.setString(2, new String(f2.getBytes("GBK"), "ISO-8859-1");
rs = pstmt.executeQuery();
String f3, f4;
while(rs.next()) {
f3 = new String(rs.getString(1).getBytes("ISO-8859-1"), "GBK");
f4 = new String(rs.getString(2).getBytes("ISO-8859-1"), "GBK");
}
}
finally {
//close resouces
...
}
其实我们
彻底
可以这样写:
import java.sql.*;
import com.redv.sql.encoding.*;
Class.forName("org.gjt.mm.mysql.Driver");
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "");
//接管数据库衔接实例
boolean coding = true;
EncodingConnection codingConnection = new EncodingConnection(con, coding, "ISO-8859-1", "GBK");
//
获得接管后的数据库衔接实例,以后直接
使用con已经是
通过EncodingConnection再一次包装过的实例
con = codingConnection.getConnection();
pstmt = con.prepareStatement("SELECT f3, f4 FROM tbl1 WHERE f1 = ? AND f2 = ?");
pstmt.setString(1, f1);
pstmt.setString(2, f2);
rs = pstmt.executeQuery();
String f3, f4;
while(rs.next()) {
f3 = rs.getString(1);
f4 = rs.getString(2);
}
}
finally {
//close resouces
...
}
看看,怎么样,我们
惟独求在猎取数据库衔接的地方略微
批改一下,甚至我们
可以把它当作参数
保留在 properties里面,转变coding的布尔值来设定是不是
使用自动编码转换 。
一般我们
可以
使用一个Database类来封装猎取数据库衔接的那段getConnection,以便于我们
可以从 javax.sql.DataSource中猎取到数据库衔接 。这个时候我们仅仅需求
批改我们的Database类即可,而不用去查找全部
使用了rs.setString(), rs.getString()的地方去加入我们的编码转换代码了 。甚至我们在
使用con.createStatment()语句时,
即便我们sql语句含有汉字或者其它的双字节字符时一样没有问题:
SELECT 姓名, 性别 FROM 学生表 WHERE 班级 LIKE '%计算机%'