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添加下面一段:
<web-app>
...
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>com.redv.projects.eduadmin.util.filters.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
...
</web-app>
其中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 '%計算機%'