jsp中文显示的小问题,请指教。

newcomer2004 2004-06-24 03:04:15
使用jsp,中文显示一致令人不舒服,常常问题出在字符集上,特别是有关数据库查询的。
自己做了个实验,也没总结出什末经验。希望高手能多多赐教。
从test1.jsp 中传数据给test2.jsp显示。
//-------------- test1.jsp
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head>
<%
String name = "姓名";
int age = 23;
String GBKname = new String( name.getBytes("ISO8859_1"),"GBK");
String ISOname = new String( name.getBytes("GBK"),"ISO8859_1");
%>

<body>
name = <%=name%><br>
GBKname = <%=GBKname%><br>
ISOname = <%=ISOname%><br>
age = <%=age%><br>
<a href="test2.jsp?name=<%=name%>&GBKname=<%=GBKname%>&ISOname=<%=ISOname%>&age=<%=age%>">test2.jsp</a>
<form action="test2.jsp" method="post">
<input type="hidden" value="<%=name%>" name="name">
<input type="hidden" value="<%=GBKname%>" name="GBKname">
<input type="hidden" value="<%=ISOname%>" name="ISOname">
<input type="hidden" value="<%=age%>" name="age">
<input name="sum" type="submit" value="xx">
</form>

</body>
</html>

// -------------------test2.jsp
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head>
<%

String name = request.getParameter("name");
String name_ISO_GBK = new String( name.getBytes("ISO8859_1"),"GBK");
String GBKname = request.getParameter("GBKname");
String GBKname_ISO_GBK = new String( GBKname.getBytes("ISO8859_1"),"GBK");
String GBKname_GBK_ISO = new String( GBKname.getBytes("GBK"),"ISO8859_1");
String ISOname = request.getParameter("ISOname");
String ISOname_ISO_GBK = new String( ISOname.getBytes("ISO8859_1"),"GBK");
String ISOname_GBK_ISO = new String( ISOname.getBytes("GBK"),"ISO8859_1");
int age = Integer.parseInt(request.getParameter("age"));
%>
<body>

name = <%=name%><br>
name_ISO_GBK = <%=name_ISO_GBK%><br>
GBKname = <%=GBKname%><br>
GBKname_ISO_GBK = <%=GBKname_ISO_GBK%><br>
GBKname_GBK_ISO = <%=GBKname_GBK_ISO%><br>
ISOname = <%=ISOname%><br>
ISOname_ISO_GBK = <%=ISOname_ISO_GBK%><br>
ISOname_GBK_ISO = <%=ISOname_GBK_ISO%><br>
age = <%=age%>
</body>
</html>

// ---------------------test2.jsp的结果显示如下:
name = ????
name_ISO_GBK = 姓名
GBKname = ??
GBKname_ISO_GBK = ??
GBKname_GBK_ISO = ??
ISOname = ????
ISOname_ISO_GBK = ????
ISOname_GBK_ISO = ????
age = 23

显示五花八门,而且结果也并非我想象。
在实际工作中,每次要进行入库查询时,都要把sql语句显示出来,看看中文是否有问题。实在是受不了了,难道没有规律吗?
...全文
129 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Jzealot 2004-06-28
  • 打赏
  • 举报
回复
在顶部加一句
<%request.setCharacterEncoding("gb2312");%>
twelove 2004-06-28
  • 打赏
  • 举报
回复
String uu=new String(name.getBytes("ISO-8859-1"));

老是会有同样的问题出现
newcomer2004 2004-06-28
  • 打赏
  • 举报
回复
谢谢 freelarry。可是如你所说,不就是test2.jsp中的 ISOname_ISO_GBK 吗?可是显示不行呀。同我用的weblogic显示一样。期待你的回复。
newcomer2004 2004-06-28
  • 打赏
  • 举报
回复
to:Jzealot
加 <%request.setCharacterEncoding("gb2312");%>的用处是什末?
我加了,还是只有 name_ISO_GBK 显示正确.
jianeng310 2004-06-25
  • 打赏
  • 举报
回复
UP
freelarry 2004-06-25
  • 打赏
  • 举报
回复
不是没有规律。实际上是这样的:要看的是wenserver使用的编码方式。

比如说tomcat 用的是8859编码。
在这样的情况下,不做任何转换,在第二个页面上收到的是 ((gb2312)8859)
所以只要用8859的编码取出来后转成gb2312就可以了

所以我们一般处理是,在传送前,把gb2312转成8859
在接收页面 把8859转成gb2312。

这样的说明够了吗?
newcomer2004 2004-06-25
  • 打赏
  • 举报
回复
好可怜,贴了几天了,没人发表一下高见。 看来这种问题高手们都不屑一顾了。
就是有人和我讨论一下也好呀?

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧