JSP顯示中文有亂碼怎麼辦用request得到的用戶輸入的中文怎麼是亂碼把漢字寫到數據庫怎麼是亂碼等等一些關於漢字亂碼的問題其實這個問題很簡單管它漢字不漢字還是日文還是其他的什麼雙字節的語言我們一律把它當作UTF看待
(一)request中的雙字節文字
我們來實現在整個應用程序中使用UTF編碼工作之所以選擇UTF不僅僅之於上述原因我們知道java的就是基於在UTF之上的所以我們選擇UTF應該沒錯
首先把我們的java jsp文件都用UTF編碼來保存如果以前的沒有用UTF保存也無所謂但是建議以後寫的都用UTF來保存
並在jsp裡面寫:
以下是引用片段<%@page contentType=
text/html; charset=UTF
%>而不是
%@page contentType=text/html; charset=UTF% 然後在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
</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
import javax
servlet
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編碼的在JSP程序中就可以使用requestgetParameter(myKey)來直接得到UTF編碼的字符串了而不需要像這樣new String(requestgetParameter(myKey)getBytes(ISO) GBK)來解決那些亂碼了
(二)數據庫處理的雙字節文字
另外一個就是寫入數據庫的問題我們知道我們在使用mysql的時候可以改用這樣的url來處理漢字編碼問題jdbc:mysql://localhost:/upas?useUnicode=true& characterEncoding=gb那麼對於那些我們無法像mysql這樣解決的怎麼辦呢?難道我們每次都這樣寫嗎(wwwliancom)
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:
/test
root
);
pstmt = con
prepareStatement(
SELECT f
f
FROM tbl
WHERE f
= ? AND f
= ?
);
pstmt
setString(
new String(f
getBytes(
GBK
)
ISO
);
pstmt
setString(
new String(f
getBytes(
GBK
)
ISO
);
rs = pstmt
executeQuery();
String f
f
;
while(rs
next()) {
f
= new String(rs
getString(
)
getBytes(
ISO
)
GBK
);
f
= new String(rs
getString(
)
getBytes(
ISO
)
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:
/test
root
);
//接管數據庫連接實例
boolean coding = true;
EncodingConnection codingConnection = new EncodingConnection(con
coding
ISO
GBK
);
//獲得接管後的數據庫連接實例
以後直接使用con已經是經過EncodingConnection重新包裝過的實例
con = codingConnection
getConnection();
pstmt = con
prepareStatement(
SELECT f
f
FROM tbl
WHERE f
= ? AND f
= ?
);
pstmt
setString(
f
);
pstmt
setString(
f
);
rs = pstmt
executeQuery();
String f
f
;
while(rs
next()) {
f
= rs
getString(
);
f
= rs
getString(
);
}
}
finally {
//close resouces
}
看看怎麼樣我們只需要在獲取數據庫連接的地方稍微修改一下甚至我們可以把它當作參數保存在 properties裡面改變coding的布爾值來設定是否使用自動編碼轉換常常我們可以使用一個Database類來封裝獲取數據庫連接的那段 getConnection以便於我們可以從 javaxsqlDataSource中獲取到數據庫連接這個時候我們僅僅需要修改我們的Database類即可而不用去搜索所有使用了 rssetString() rsgetString()的地方去加入我們的編碼轉換代碼了
From:http://tw.wingwit.com/Article/program/Java/JSP/201311/19877.html