注册 登录
编程论坛 J2EE论坛

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

fqj793 发布于 2007-02-05 11:00, 2198 次点击
我使用如下语句:response.sendRedirect("error.jsp?msg=您的输入有误!");error.jsp页面中的接收语句为:String msg=new String(request.getParameter("msg").getBytes("ISO8859_1"),"GBK");可是当显示变量msg的时候就是乱码,各位,请教了!
32 回复
#2
想你的天空2007-02-05 11:02
传递之前转化1下就好了
#3
fqj7932007-02-05 11:04
能说具体点吗?怎样转呢?
#4
想你的天空2007-02-05 11:07
String 传之前=
URLUnicode("error.jsp?msg=您的输入有误!"); 这里处理1下, 我也记不太清楚了
response.sendRedirect(传之前);

#5
fqj7932007-02-05 11:09
先谢过了,兄弟,我试一下!
#6
しΟν∈→鱈2007-02-05 11:33
在你的整个JSP文件中加个
<meta http-equiv="Content-Type" content = "text/html; charset = gb2312">试试
#7
fqj7932007-02-05 14:38
这个是有的.已经加了,但还是不行.
#8
shenba2007-02-05 22:08
request.setCharaterEncoding("GBK")
#9
fqj7932007-02-06 08:54
8楼的xd,能明确告诉我该如何写吗?String msg=new String(request.getParameter("msg").getBytes("ISO8859_1"),"GBK");语句该怎么写呢?
#10
angeloc2007-02-06 09:05
以下是引用fqj793在2007-2-5 11:00:53的发言:
我使用如下语句:response.sendRedirect("error.jsp?msg=您的输入有误!");error.jsp页面中的接收语句为:String msg=new String(request.getParameter("msg").getBytes("ISO8859_1"),"GBK");可是当显示变量msg的时候就是乱码,各位,请教了!
你在传递前可以转码先
response.sendRedirect("error.jsp?msg=URLEncoder.encode('您的输入有误!')");
#11
fqj7932007-02-06 09:32

还是不行呀!

#12
every2007-02-06 12:51
在接收页面加上request.setCharacter("gb2312")(没写错吧。。)看看行不行。。
#13
angeloc2007-02-06 17:36
以下是引用fqj793在2007-2-6 9:32:23的发言:

还是不行呀!

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

#14
fqj7932007-02-06 17:55

<%@ 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>
红色部分为所述代码!

#15
bagger2007-02-06 22:40
request.setCharaterEncoding("GBK");
或request.setCharaterEncoding("GB2312");
之后再request
就不会出现中文乱码了
#16
angeloc2007-02-07 08:57
response.sendRedirect("error.jsp?msg=<%=URLEncoder.encode('您好的输入有错误!')%>");
你加上URLEncoder!
#17
fqj7932007-02-07 10:55

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

#18
风月_无边2007-02-07 13:36

ISO-8859-1 是不是写错了

#19
fqj7932007-02-07 15:32

这一步我做了!

#20
angeloc2007-02-07 15:45
以下是引用fqj793在2007-2-7 10:55:40的发言:

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

我那种方法你试了么?

#21
fqj7932007-02-07 16:02

也试了,产生错误信息
type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 170 in the jsp file: /www/logo.jsp

Generated servlet error:
[javac] Compiling 1 source file

D:\Program Files\Apache Group\Tomcat 4.1\work\Standalone\localhost\_\www\logo_jsp.java:299: unclosed string literal
response.sendRedirect("error.jsp?msg=<%=URLEncoder.encode('?ㄥソ????ユ????!')
^

An error occurred at line: 173 in the jsp file: /www/logo.jsp

Generated servlet error:
D:\Program Files\Apache Group\Tomcat 4.1\work\Standalone\localhost\_\www\logo_jsp.java:300: ')' expected
out.write("\");\r\n\t }\r\n\t \r\n\t %>");
^

An error occurred at line: -1 in the jsp file: null

Generated servlet error:
D:\Program Files\Apache Group\Tomcat 4.1\work\Standalone\localhost\_\www\logo_jsp.java:309: 'catch' without 'try'
} catch (Throwable t) {
^
D:\Program Files\Apache Group\Tomcat 4.1\work\Standalone\localhost\_\www\logo_jsp.java:31: 'try' without 'catch' or 'finally'
try {
^
D:\Program Files\Apache Group\Tomcat 4.1\work\Standalone\localhost\_\www\logo_jsp.java:319: '}' expected
^
5 errors
我是直接搬上去的.

#22
angeloc2007-02-07 16:27
response.sendRedirect("error.jsp?msg=<%=URLEncoder.encode('?ㄥソ????ユ????!')
你这个写的问题吧!
URLEncoder需要引入java.net.URLEncoder
#23
fqj7932007-02-08 15:59

问题还是没有解决!

#24
lgdcky2007-02-19 22:33
这个问题我也遇到过!其实只要把你要传递的信息先放到一个String里,
String urlr = "您的输入有误";
<p align='center'><a href='text.jsp?msg="+urlr+"'>" + name + "</a></p>"
然后在传过去之后进行如下转换
String url = request.getParameter("msg");
String urle = new String(url.getBytes("ISO-8859-1"),"GBK");
#25
zhupeng2007-02-23 21:30
一般都用GBK吧你怎么又用GBK又用GB2312啊
#26
独孤无痕2007-02-27 20:32
加个filtter试试!!
#27
wsn2007-03-03 17:26
据说utf8就不会了
#28
ILoveMK2007-03-03 23:36

在代码中加入这句话response.setContentType("text/html;charset=UTF8");

然后找到 server.xml 中的
<Connector port="8080" maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true" URIEncoding='UTF8' />

加入红色部分代码,这样应该就行了。
如果还不行就把上面的UTF8全部替换成GBK.


#29
球球2007-03-08 22:24
String msg=new String(request.getParameter("msg").getBytes("iso-8859-1"));

"iso-8859-1"
#30
fqj7932007-03-08 23:25

我不是这样传回中文,我是要从这里传回:response.sendRedirect("error.jsp?msg=您的输入有误!");
error.jsp页面中的接收语句为:String msg=new String(request.getParameter("msg").getBytes("ISO8859_1"),"GBK");

#31
球球2007-03-09 11:16
"ISO8859_1"改成"iso-8859-1"试试
#32
心动音符2007-03-11 22:31
以下是引用ILoveMK在2007-3-3 23:36:39的发言:

在代码中加入这句话response.setContentType("text/html;charset=UTF8");

然后找到 server.xml 中的
<Connector port="8080" maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true" URIEncoding='UTF8' />

加入红色部分代码,这样应该就行了。
如果还不行就把上面的UTF8全部替换成GBK.


你这里用的是get方式所用要配置server,不过好像在程序里也可以加段代码,也行但我把那个方法给忘了

#33
心动音符2007-03-11 22:37
老弟我也有个关于乱码的问题:
我想把内容(有中文)通过hibernter持久到数据库(MySql)中,代码可以成功执行但我到数据库中通过selest语句查询发现都是“问号”?
--我在数据库建表的时候用了mysqld --character_set_server=gb2312;
在XML的URL配置信息里也加了jdbc:mysql://localhost/dbname?useUnicode=true;characterEncoding=gb2312
但还是不行不知道为啥?我程序中所保存的中文是gb2312的

[此贴子已经被作者于2007-3-11 22:38:18编辑过]

1