救急!又遇中文输入问题!

pz2002 2002-05-21 06:56:23
我在作一个留言班板的时候,一输入中文就乱码,请各位高手帮帮忙!以下是源程序:.
index.heml:
<!-- index.html -->
<%@ page contentType="text/html;charset=gb2312"%>
<link href="web/css/style1.css" rel="stylesheet" type="text/css">
<HTML>
<HEAD>
<TITLE> 留言板 </TITLE>
</HEAD>
<body background=images/back.jpg bgcolor="#FFFFFF">
<TABLE width="90%" align="center" border=1 cellspacing="0" cellpadding="0" bordercolordark="#88a6dd" bordercolorlight="#88a6dd">
<TR>
<TD align="center" bgcolor="#88a6dd">访客留言板</TD>
</TR>
<TR>
<TD><TABLE align="center" border=1 cellspacing="0" cellpadding="0" bordercolordark="#88a6dd" bordercolorlight="#88a6dd">
<FORM ACTION="add_message.jsp">
<TR>
<TD>姓名:</TD>
<TD bgcolor="#88a6dd"><input type="text" name="name" size=25></TD>
</TR>
<TR>
<TD bgcolor="#88a6dd">E-mail:</TD>
<TD bgcolor="#88a6dd"><input type="text" name="mail" size=25></TD>
</TR>
<TR>
<TD>主题:</TD>
<TD bgcolor="#88a6dd"><input type="text" name="title" size=25></TD>
</TR>
<TR>
<TD valign="top" bgcolor="#88a6dd">留言:</TD>
<TD><textarea name="content" rows=7 cols=25></textarea></TD>
</TR>
<TR>
<TD colspan=3><TABLE align="center" width="100%" cellspacing="0" cellpadding="0" bordercolordark="#88a6dd" bordercolorlight="#88a6dd">
<TR>
<TD align="center" bgcolor="#88a6dd"><input type="submit" value="提交留言"></TD>
<TD align="center"><a href="view_message.jsp"><font size=2>查看留言</font></a></TD>
<TD align="center" bgcolor="#88a6dd"><input type="reset" value="重新填写"></TD>
</TR>
</TABLE></TD>
</TR>
</FORM>
</TABLE></TD>
</TR>
</TABLE>
</BODY>
</HTML>
add_message:
<!--add_message.jsp-->
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%@ page contentType="text/html;charset=gb2312"%>
<link href="web/css/style1.css" rel="stylesheet" type="text/css">
<HTML>
<HEAD>
<TITLE>提交留言 </TITLE>
</HEAD>
<body background=images/back.jpg bgcolor="#FFFFFF">
<%
String name=request.getParameter("name");
byte[] tmpbyte=name.getBytes("ISO8859_1");
name=new String(tmpbyte);
String mail=request.getParameter("mail");
String title=request.getParameter("title");
tmpbyte=title.getBytes("ISO8859_1");
title=new String(tmpbyte);
String content=request.getParameter("content");
tmpbyte=content.getBytes("ISO8859_1");
content=new String(tmpbyte);
if(name==null)
name="";
if(title==null)
title="";
if(content==null)
content="";
if(mail==null)
mail="";
if(name.length()==0)
out.println("留言人姓名不能为空!");
else if(title.length()==0)
out.println("留言主题不能为空!");
else if(content.length()==0)
out.println("留言内容不能为空!");
else
{
java.util.Date date=new java.util.Date();
String datetime=new Timestamp(date.getTime()).toString();

try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:bookshop";
Connection con=DriverManager.getConnection(url,"simba","12345");
PreparedStatement stm=con.prepareStatement("insert into message values(?,?,?,?,?)");
stm.setString(1,title);
stm.setString(2,name);
if(mail.length()==0)
stm.setString(3,null);
else stm.setString(3,mail);
stm.setString(4,datetime);
stm.setString(5,content);
try
{
stm.executeQuery();
} catch(Exception e)
{}
con.close();
}
catch (Exception e)
{}
%>
<jsp:forward page="view_message.jsp" />
<%
}
%>
</body>
</html>
view_message:
<!--view_message.jsp-->
<%@ page contentType="text/html;charset=gb2312"%>
<link href="web/css/style1.css" rel="stylesheet" type="text/css">
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<HTML>
<HEAD>
<TITLE> 查看留言 </TITLE>
</HEAD>
<body background=images/back.jpg bgcolor="#FFFFFF">
<p align="center">所有访客留言</p>
<hr>
<%
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:bookshop";
Connection con=DriverManager.getConnection(url,"simba","12345");
Statement stm=con.createStatement();
ResultSet result=stm.executeQuery("select count(*) from message");
int message_count=0;
if(result.next())
{
message_count=result.getInt(1);
result.close();
}
if(message_count>0)
{
result=stm.executeQuery("select * from message order by time desc");
while(result.next())
{
String title=result.getString("title");
String name=result.getString("name");
String mail=result.getString("mail");
Timestamp time=result.getTimestamp("time");
Date date_time=new Date(time.getTime());
Time time_time=new Time(time.getTime());
String content=result.getString("content");
%>
<TABLE width="100%" align="center" border=1 cellspacing="0" cellpadding="0" bordercolordark="#000000" bordercolorlight="#88a6dd">
<tr><td bgcolor="#88a6dd"><div align="center"><font size=2>主题:</font></div></td><td colspan=3><%=title%></td></tr>
<tr><td bgcolor="#88a6dd"><div align="center"><font size=2>留言人:</font></div></td>
<td colspan=3><%=name%>
</td>
<td bgcolor="#88a6dd"><div align="center"><font size=2>E-mail:</font></div></td>
<td>
<%
out.println(""+mail+"");
%>
</td></tr>
<tr><td bgcolor="#88a6dd"><div align="center"><font size=2>留言时间:</font></div></td><td colspan=3>
<%
out.println("<font size=2>"+date_time+" "+time_time+"</font>");

%>
</td></tr>
<tr><td align="center">
<%
out.println("("+message_count+")");
%>
</td>
<td colspan=3><%=content%></td></tr>
</table>
<%
out.println("<hr>");
message_count--;
}
result.close();
con.close();
}
else
{
out.println("目前还没有任何留言!");
con.close();
}
} catch(Exception e)
{out.println(e);}
%>
<p align="center"><a href="index.html">我要留言</a></p>
</body>
</html>

...全文
13 点赞 收藏 8
写回复
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
pz2002 2002-05-22
index.html:
<!-- index.html -->
<%@ page contentType="text/html;charset=gb2312"%>
<link href="web/css/style1.css" rel="stylesheet" type="text/css">
<HTML>
<HEAD>
<TITLE> 留言板 </TITLE>
</HEAD>
<body background=images/back.jpg bgcolor="#FFFFFF">
<TABLE width="90%" align="center" border=1 cellspacing="0" cellpadding="0" bordercolordark="#88a6dd" bordercolorlight="#88a6dd">
<TR>
<TD align="center" bgcolor="#88a6dd">访客留言板</TD>
</TR>
<TR>
<TD><TABLE align="center" border=1 cellspacing="0" cellpadding="0" bordercolordark="#88a6dd" bordercolorlight="#88a6dd">
<FORM ACTION="add_message.jsp">
<TR>
<TD>姓名:</TD>
<TD bgcolor="#88a6dd"><input type="text" name="name" size=25></TD>
</TR>
<TR>
<TD bgcolor="#88a6dd">E-mail:</TD>
<TD bgcolor="#88a6dd"><input type="text" name="mail" size=25></TD>
</TR>
<TR>
<TD>主题:</TD>
<TD bgcolor="#88a6dd"><input type="text" name="title" size=25></TD>
</TR>
<TR>
<TD valign="top" bgcolor="#88a6dd">留言:</TD>
<TD><textarea name="content" rows=7 cols=25></textarea></TD>
</TR>
<TR>
<TD colspan=3><TABLE align="center" width="100%" cellspacing="0" cellpadding="0" bordercolordark="#88a6dd" bordercolorlight="#88a6dd">
<TR>
<TD align="center" bgcolor="#88a6dd"><input type="submit" value="提交留言"></TD>
<TD align="center"><a href="view_message.jsp"><font size=2>查看留言</font></a></TD>
<TD align="center" bgcolor="#88a6dd"><input type="reset" value="重新填写"></TD>
</TR>
</TABLE></TD>
</TR>
</FORM>
</TABLE></TD>
</TR>
</TABLE>
</BODY>
</HTML>
add_message.jsp:
<!--add_message.jsp-->
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%@ page contentType="text/html;charset=gb2312"%>
<link href="web/css/style1.css" rel="stylesheet" type="text/css">
<HTML>
<HEAD>
<TITLE>提交留言 </TITLE>
</HEAD>
<body background=images/back.jpg bgcolor="#FFFFFF">
<%
String name=request.getParameter("name");
out.print("name=="+name) ;
name = new String(name.getBytes("8859_1"),"gb2312");
out.print("转换后name=="+name) ;
String mail=request.getParameter("mail");
String title=request.getParameter("title");
out.print("title=="+title) ;
title= new String(title.getBytes("8859_1"),"gb2312");
out.print("转换后title=="+title) ;
String content=request.getParameter("content");
out.print("content=="+content) ;
content= new String(content.getBytes("8859_1"),"gb2312");
out.print("转换后content=="+content) ;

if(name==null)
name="";
if(title==null)
title="";
if(content==null)
content="";
if(mail==null)
mail="";
if(name.length()==0)
out.println("留言人姓名不能为空!");
else if(title.length()==0)
out.println("留言主题不能为空!");
else if(content.length()==0)
out.println("留言内容不能为空!");
else
{
java.util.Date date=new java.util.Date();
String datetime=new Timestamp(date.getTime()).toString();

try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:bookshop";
Connection con=DriverManager.getConnection(url,"simba","12345");
PreparedStatement stm=con.prepareStatement("insert into message values(?,?,?,?,?)");
stm.setString(1,title);
stm.setString(2,name);
if(mail.length()==0)
stm.setString(3,null);
else stm.setString(3,mail);
stm.setString(4,datetime);
stm.setString(5,content);
try
{
stm.executeQuery();
} catch(Exception e)
{}
con.close();
}
catch (Exception e)
{}
%>
<jsp:forward page="view_message.jsp" />
<%
}
%>
</body>
</html>
view_message.jsp:
<!--view_message.jsp-->
<%@ page contentType="text/html;charset=gb2312"%>
<link href="web/css/style1.css" rel="stylesheet" type="text/css">
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<HTML>
<HEAD>
<TITLE> 查看留言 </TITLE>
</HEAD>
<body background=images/back.jpg bgcolor="#FFFFFF">
<p align="center">所有访客留言</p>
<hr>
<%
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:bookshop";
Connection con=DriverManager.getConnection(url,"simba","12345");
Statement stm=con.createStatement();
ResultSet result=stm.executeQuery("select count(*) from message");
int message_count=0;
if(result.next())
{
message_count=result.getInt(1);
result.close();
}
if(message_count>0)
{
result=stm.executeQuery("select * from message order by time desc");
while(result.next())
{
String title=result.getString("title");
String name=result.getString("name");
String mail=result.getString("mail");
Timestamp time=result.getTimestamp("time");
Date date_time=new Date(time.getTime());
Time time_time=new Time(time.getTime());
String content=result.getString("content");
%>
<TABLE width="100%" align="center" border=1 cellspacing="0" cellpadding="0" bordercolordark="#000000" bordercolorlight="#88a6dd">
<tr><td bgcolor="#88a6dd"><div align="center"><font size=2>主题:</font></div></td><td colspan=3><%=title%></td></tr>
<tr><td bgcolor="#88a6dd"><div align="center"><font size=2>留言人:</font></div></td>
<td colspan=3><%=name%>
</td>
<td bgcolor="#88a6dd"><div align="center"><font size=2>E-mail:</font></div></td>
<td>
<%
out.println(""+mail+"");
%>
</td></tr>
<tr><td bgcolor="#88a6dd"><div align="center"><font size=2>留言时间:</font></div></td><td colspan=3>
<%
out.println("<font size=2>"+date_time+" "+time_time+"</font>");

%>
</td></tr>
<tr><td align="center">
<%
out.println("("+message_count+")");
%>
</td>
<td colspan=3><%=content%></td></tr>
</table>
<%
out.println("<hr>");
message_count--;
}
result.close();
con.close();
}
else
{
out.println("目前还没有任何留言!");
con.close();
}
} catch(Exception e)
{out.println(e);}
%>
<p align="center"><a href="index.html">我要留言</a></p>
</body>
</html>

回复
Andrawu 2002-05-21
String name=request.getParameter("name");
out.print("name=="+name) ;
name = new String(name.getBytes("8859_1"),"gb2312");
out.print("转换后name=="+name) ;
String mail=request.getParameter("mail");
String title=request.getParameter("title");
out.print("title=="+title) ;
title= new String(title.getBytes("8859_1"),"gb2312");
out.print("转换后title=="+title) ;
String content=request.getParameter("content");
out.print("content=="+content) ;
content= new String(content.getBytes("8859_1"),"gb2312");
out.print("转换后content=="+content) ;

查查看有没有转换?
如果转换了,则不这里的问题。
回复
pz2002 2002-05-21
可是当我把add_message.jsp改为:
<!--add_message.jsp-->
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%@ page contentType="text/html;charset=gb2312"%>
<link href="web/css/style1.css" rel="stylesheet" type="text/css">
<HTML>
<HEAD>
<TITLE>提交留言 </TITLE>
</HEAD>
<body background=images/back.jpg bgcolor="#FFFFFF">
<%
String name=request.getParameter("name");
name = new String(name.getBytes("8859_1"),"gb2312");
String mail=request.getParameter("mail");
String title=request.getParameter("title");
title= new String(title.getBytes("8859_1"),"gb2312");
String content=request.getParameter("content");
content= new String(content.getBytes("8859_1"),"gb2312");
if(name==null)
name="";
if(title==null)
title="";
if(content==null)
content="";
if(mail==null)
mail="";
if(name.length()==0)
out.println("留言人姓名不能为空!");
else if(title.length()==0)
out.println("留言主题不能为空!");
else if(content.length()==0)
out.println("留言内容不能为空!");
else
{
java.util.Date date=new java.util.Date();
String datetime=new Timestamp(date.getTime()).toString();

try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:bookshop";
Connection con=DriverManager.getConnection(url,"simba","12345");
PreparedStatement stm=con.prepareStatement("insert into message values(?,?,?,?,?)");
stm.setString(1,title);
stm.setString(2,name);
if(mail.length()==0)
stm.setString(3,null);
else stm.setString(3,mail);
stm.setString(4,datetime);
stm.setString(5,content);
try
{
stm.executeQuery();
} catch(Exception e)
{}
con.close();
}
catch (Exception e)
{}
%>
<jsp:forward page="view_message.jsp" />
<%
}
%>
</body>
</html>
还是一堆问号等着我
回复
Andrawu 2002-05-21
String name=request.getParameter("name");
name = new String(name.getBytes("8859_1"),"gb2312");
// byte[] tmpbyte=name.getBytes("ISO8859_1");
// name=new String(tmpbyte);
String title=request.getParameter("title");
title = new String(title.getBytes("8859_1"),"gb2312");
// tmpbyte=title.getBytes("ISO8859_1");
// title=new String(tmpbyte);
String content=request.getParameter("content");
content = new String(content.getBytes("8859_1"),"gb2312");
// tmpbyte=content.getBytes("ISO8859_1");
// content=new String(tmpbyte);
回复
pz2002 2002-05-21
我去掉了之后,还是老毛病,可以从库里调入已经输入的中文记录,还是不能通过表单往库里输入。
回复
weidegong 2002-05-21
在request.getParameter()之后,进行toGb编码转化再存入数据库就可以了
回复
weidegong 2002-05-21
代码转换方法
//中文编码的转换
public String toGb(String uniStr){
String gbStr = "";
if(uniStr == null){
uniStr = "";
}
try{
byte[] tempByte = uniStr.getBytes("ISO8859_1");
gbStr = new String(tempByte,"GB2312");
}catch(UnsupportedEncodingException uef){
}
return gbStr;
}
public String toUni(String gbStr){
String uniStr = "";
if(gbStr == null){
gbStr = "";
}
try{
byte[] tempByte = gbStr.getBytes("GB2312");
uniStr = new String(tempByte,"ISO8859_1");
}catch(UnsupportedEncodingException uef){
}
return uniStr;
}
回复
star123qiu 2002-05-21
我用 你的代码诗了试,发现你有好多变量重复定义了。如:name,email,content等,你再好好检查一下你的程序吧。
回复
发动态
发帖子
Web 开发
创建于2007-09-28

7.9w+

社区成员

Java Web 开发
申请成为版主
社区公告
暂无公告