当前位置: 代码迷 >> Java Web开发 >> 传递中文产生乱码!生气呀
  详细解决方案

传递中文产生乱码!生气呀

热度:307   发布时间:2007-02-06 09:32:23.0

还是不行呀!


----------------解决方案--------------------------------------------------------
在接收页面加上request.setCharacter("gb2312")(没写错吧。。)看看行不行。。
----------------解决方案--------------------------------------------------------
以下是引用fqj793在2007-2-6 9:32:23的发言:

还是不行呀!

我看你把两个页面代码贴上来好了,你这么说永远也不知道你到底哪写的有问题
----------------解决方案--------------------------------------------------------

<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>公司管理系统</title>
<style type="text/css">
<!--
body {
background-image: url(image/bj.gif);
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
}
.style2 {color: #0033FF}
-->

</style></head>
<body>
<%
request.setCharacterEncoding("gb2312");

request.getSession(true);
String name=new String(request.getParameter("user").getBytes("ISO8859_1"),"GBK");

String password=new String(request.getParameter("pass").getBytes("ISO8859_1"),"GBK");

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:mydata";
String username="";
String pass="";
Connection con=DriverManager.getConnection(url,username,pass);
Statement stmt=con.createStatement();
String sql="select * from wglperson where username='"+name+"'and password='"+password+"'";
ResultSet rs=stmt.executeQuery(sql);
if (rs.next())
{
int web=rs.getInt("web");
String personid=rs.getString("username");
session.setAttribute("personid",personid);
if (web==1)
{
%>

<table width="800" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td>
<table width="800" border="0"align="center" cellpadding="0" cellspacing="0">
<tr>
<td colspan="6"><img src="image/top2.gif" width="800" height="58"></td>
</tr>
<tr>
<td width="165" height="22"><img src="image/nav1.gif" width="165" height="22"></td>
<td colspan="5" bgcolor="#CCCCCC"><img src="image/top2.gif" width="635" height="22"></td>
</tr>
<tr>
<td colspan="6"><img src="image/spl_1.gif" width="76" height="72"><img src="image/top2.gif" width="724" height="72"></td>
</tr>
<tr><td colspan="6"> <h3 align="center" class="style2">欢迎进入管理员界面</h3></td></tr>
<tr>
<td height="340"><img src="image/separator.gif" width="2" height="340"></td>
<td width="95">dfd</td>
<td width="127"><div align="right">dfd</div></td>
<td width="113">dfd</td>
<td width="100">dfd</td>
<td width="200"><div align="right"><img src="image/separator.gif" width="2" height="340"></div></td>
</tr>
<tr>
<td>dfd</td>
<td width="95">dfd</td>
<td width="127">dfd</td>
<td width="113">dfd</td>
<td width="100">dfd</td>
<td width="200">dfd</td>
</tr>
<tr>
<td>dfd</td>
<td width="95">dfd</td>
<td width="127">dfd</td>
<td width="113">dfd</td>
<td width="100">dfd</td>
<td width="200">dfd</td>
</tr>
<tr>
<td colspan="6"><img src="image/top2.gif" width="800" height="58"></td>
</tr>
<%}

if (web==2)
{
%>

</table>

<table width="800" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td>
<table width="800" border="0"align="center" cellpadding="0" cellspacing="0">
<tr>
<td colspan="6"><img src="image/top2.gif" width="800" height="58"></td>
</tr>
<tr>
<td width="165" height="22"><img src="image/nav1.gif" width="165" height="22"></td>
<td colspan="5" bgcolor="#CCCCCC"><img src="image/top2.gif" width="635" height="22"></td>
</tr>
<tr>
<td colspan="6"><img src="image/spl_1.gif" width="76" height="72"><img src="image/top2.gif" width="724" height="72"></td>
</tr>
<tr>
<td>

</td>
<td colspan="4"><p align="center"><font size="+3" color="#0033FF" >欢迎光临领导用户界面</font></p></td>
<td width="139">&nbsp;</td>
</tr>
<tr>
<td height="400"><img src="image/separator.gif" width="2" height="400"></td>
<td width="149">&nbsp;</td>
<td width="133"></td>
<td width="156">&nbsp;</td>
<td width="76">&nbsp;</td>
<td width="139"><div align="right"><img src="image/separator.gif" width="2" height="400"></div></td>
</tr>
<tr>
<td colspan="6"><img src="image/top2.gif" width="800" height="58">
<%}

if (web==0)
{
%>

</table>

<table width="800" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td height="599">
<table width="800" border="0"align="center" cellpadding="0" cellspacing="0">
<tr>
<td colspan="6"><img src="image/top2.gif" width="800" height="58"></td>
</tr>
<tr>
<td width="165" height="22"><img src="image/nav1.gif" width="165" height="22"></td>
<td colspan="5" bgcolor="#CCCCCC"><img src="image/top2.gif" width="635" height="22"></td>
</tr>
<tr>
<td colspan="6"><img src="image/spl_1.gif" width="76" height="72"><img src="image/top2.gif" width="724" height="72"></td>
</tr>
<tr>
<td height="26">

</td>
<td colspan="4"><p align="center"><font size="+3" color="#0033FF" >欢迎光临普通用户界面</font></p></td>
<td width="139">&nbsp;</td>
</tr>
<tr>
<td height="360"><img src="image/separator.gif" width="2" height="360"></td>
<td width="165"><a href="yuangong/rest.jsp"><img src="image/qj.jpg" width="150" height="55" border="0"></a></td>
<td width="117"></td>
<td width="156"><a href="yuangong/restuser.jsp"><img src="image/qjcx.jpg" width="150" height="55" border="0"></a></td>
<td width="76">&nbsp;</td>
<td width="139"><div align="right"><img src="image/separator.gif" width="2" height="360"></div></td>
</tr>
<tr>
<td height="61" colspan="6"><img src="image/top2.gif" width="800" height="58"><% }}

else
{
rs.close();

response.sendRedirect("error.jsp?msg=您好的输入有错误!");


}

%></td>
</tr>

</table>

</td>
</tr>
</table>



</body>
</html>
以下是error.jsp的内容
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>公司内部系统---</title>
<style type="text/css">
<!--
body {
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
background-image: url(image/bj.gif);
}
.style1 {
font-size: 16px;
font-weight: bold;
}
.style2 {
color: #0066FF;
font-weight: bold;
font-size: 18px;
}
-->
</style></head>
<SCRIPT LANGUAGE="JavaScript">
function setf()
{
document.for1.user.focus();
}
function cancle()
{ document.for1.user.value="";
document.for1.pass.value="";
return false;
}
function selec(txt)
{
txt.focus();
txt.select();
}
function chkUser(txt)
{ var user = document.for1.user;
if (user.value=="")
{
alert ("please input username");
selec(txt);
return false;
}else{
return true;}
}
function chkPw(txt)
{ var pw = document.for1.pass;
if (pw.value=="")
{
alert ("please input password");
selec(txt);
return false;
}else{
return true;}
}
function chkUserPw()
{var user = document.for1.user;
var pw = document.for1.pass;
if(chkUser(user))
{
if(chkPw(pw))
{
return true;
}
return false;
}
return false;
}
</SCRIPT>

<body>
<table width="800" border="0"align="center" cellpadding="0" cellspacing="0">
<tr>
<td colspan="6"><img src="image/top2.gif" width="800" height="58"></td>
</tr>
<tr>
<td width="165" height="22"><img src="image/nav1.gif" width="165" height="22"></td>
<td colspan="5" bgcolor="#CCCCCC"><img src="image/top2.gif" width="635" height="22"></td>
</tr>
<tr>
<td colspan="6"><img src="image/spl_1.gif" width="76" height="72"><img src="image/top2.gif" width="724" height="72"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td width="55">&nbsp;</td>
<td width="301"><div align="center" class="style2">公司内部管理系统</div></td>
<td width="54">&nbsp;</td>
<td width="89">&nbsp;</td>
<td width="136">&nbsp;</td>
</tr>
<tr>
<td height="300"><img src="image/separator.gif" width="2" height="400"></td>
<td>&nbsp;</td>
<td><form action="logo.jsp" name="for1" method="post" onSubmit="return chkUserPw()"><table width="100%" border="0" >
<tr>
<td width="29%">&nbsp;</td>
<td width="31%"><span class="style1">username</span></td>
<td width="29%">&nbsp;</td>
<td width="11%">&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="text" name="user" size="12">&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td><span class="style1">password</span></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="password" name="pass" size="16">&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td align="center"><input type="image" border="0" name="imageField" src="image/login.gif" width="60" height="25"></td>
<td>&nbsp;</td>
<td><input type="image" border="0" name="imagereset" src="image/reset.gif" width="60" height="25" onClick="return cancle();"></td>
<td>&nbsp;</td>
</tr>
<tr>
<td colspan="3">
<%
request.setCharacterEncoding("gb2312");

String msg=new String(request.getParameter("msg").getBytes("ISO8859_1"),"GBK");
if(msg!=null)
out.print("<br>"+msg);
%>
</td>
<td>&nbsp;</td>
</tr>
</table>
</form></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><img src="image/separator.gif" width="2" height="400" align="right"></td>
</tr>
<tr>
<td colspan="6"><img src="image/top1.gif" width="800" height="58"></td>
</tr>
</table>
</body>
</html>
红色部分为所述代码!


----------------解决方案--------------------------------------------------------
request.setCharaterEncoding("GBK");
或request.setCharaterEncoding("GB2312");
之后再request
就不会出现中文乱码了

----------------解决方案--------------------------------------------------------
response.sendRedirect("error.jsp?msg=<%=URLEncoder.encode('您好的输入有错误!')%>");
你加上URLEncoder!
----------------解决方案--------------------------------------------------------

我在String msg=new String(request.getParameter("msg").getBytes("ISO8859_1"),"GBK");之前有加这句呀,request.setCharaterEncoding("GB2312");


----------------解决方案--------------------------------------------------------

ISO-8859-1 是不是写错了


----------------解决方案--------------------------------------------------------

这一步我做了!


----------------解决方案--------------------------------------------------------
以下是引用fqj793在2007-2-7 10:55:40的发言:

我在String msg=new String(request.getParameter("msg").getBytes("ISO8859_1"),"GBK");之前有加这句呀,request.setCharaterEncoding("GB2312");

我那种方法你试了么?


----------------解决方案--------------------------------------------------------
  相关解决方案