求救:jsp+javaBean的中文乱码问题
public String ShowWords(String s, HttpServletRequest httpservletrequest, int i)
throws Exception
{
String s2 = "";
int j = 0;
int k = handle.getInt(httpservletrequest, "Pid");
try
{
String s1 = "SELECT * FROM " + s + " WHERE product_id=" + k + " ORDER BY id DESC";
for(ResultSet resultset = dbconn.ExeQuery(s1); resultset.next();)
{
j++;
s2 = s2 + "<table width=100% border=0 cellspacing=1 cellpadding=3>";
s2 = s2 + "\t<tr bgcolor=#FFFFFF> ";
s2 = s2 + "\t\t<td width=4% align=center valign=middle>" + j + "</td>";
s2 = s2 + "\t\t<td width=8% align=center bgcolor=eeeeee>作者:</td>";
s2 = s2 + "\t\t<td width=29%>" + resultset.getString("author") + "</td>";
s2 = s2 + "\t\t<td width=13% align=center valign=middle bgcolor=eeeeee>发表时间:</td>";
s2 = s2 + "\t\t<td width=24% align=center valign=middle>" + resultset.getString("date_time") + "</td>";
s2 = s2 + "\t\t<td width=8% align=center valign=middle bgcolor=eeeeee>评分:</td>";
s2 = s2 + "\t\t<td width=14% align=center valign=middle><font color=#FF0000>" + Num2Star(resultset.getInt("level")) + "</font></td>";
s2 = s2 + "\t</tr>";
s2 = s2 + "\t<tr bgcolor=#FFFFFF> ";
s2 = s2 + "\t\t<td colspan=7>评论:" + resultset.getString("content") + "</td>";
s2 = s2 + "\t</tr>";
s2 = s2 + "</table>";
if(j == i)
break;
}
dbconn.CloseConn();
}
catch(SQLException sqlexception)
{
System.err.println("aq.executeQuery:" + sqlexception.getMessage());
}
return s2;
}
上面的函数返回一个字符串,奇怪的是从数据库中取回的字符串是中文正确的,但是写在函数中的中文返回的却是乱码。
怎么解决啊??
急
问题点数:100、回复次数:7Top
1 楼lovemory(墨尔)回复于 2003-12-04 07:44:34 得分 20
用getBytes("ISO-8859-1")进行字符转换Top
2 楼51ling(核心问题)回复于 2003-12-04 08:11:19 得分 50
用以下两句语句就没有问题了!!!
<%@ page contentType = "text/html;charset=gb2312" %>
<% request.setCharacterEncoding("GB2312");%>Top
3 楼VVV_lucky(*太阳*)回复于 2003-12-04 08:55:01 得分 10
不一定要用GB2312
UTF-8对于中日文混合是不错的选择。Top
4 楼dwxq(dawei)回复于 2003-12-04 09:06:31 得分 10
写如库中时不要用转换,从数据库中读出时需要用到进行getBytes("ISO-8859-1")转换Top
5 楼mayafree(我浮躁,所以要认真)回复于 2003-12-04 09:14:40 得分 10
用下面这两个方法在class或jsp中转换:
public static String getStr(String str){
try{
String temp_p=str;
byte[] temp_t=temp_p.getBytes("ISO8859-1");
String unicode=new String(temp_t,"GBK");
return unicode;
}
catch(Exception e){
System.out.println(e);
return "";
}
}
public static String getStr1(String str){
try{
String temp_p=str;
byte[] temp_t=temp_p.getBytes("GBK");
String unicode=new String(temp_t,"ISO8859-1");
return unicode;
}
catch(Exception e){
System.out.println(e);
return "";
}
}
jsp中必须加:<%@ page contentType = "text/html;charset=gb2312" %>
或<%@ page contentType = "text/html;charset=GBK" %>Top
6 楼AlexHwang(Alex)回复于 2003-12-04 09:17:03 得分 0
可是我已经试过用下面的函数转换了啊,还是不行。
哪位再看看吧,急啊
public String ISO2GBK(String s)
throws Exception
{
return new String(s.getBytes("ISO8859_1"), "GBK");
}Top
7 楼AlexHwang(Alex)回复于 2003-12-04 09:17:51 得分 0
:<%@ page contentType = "text/html;charset=gb2312" %>
或<%@ page contentType = "text/html;charset=GBK" %>
这个我也是添加的。Top




