| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1394 人关注过本帖
标题:JSP里调用了JavaBean的getxxx方法,此方法返回汉字,页面显示的时候,汉字乱 ...
只看楼主 加入收藏
Grace_TT
Rank: 1
等 级:新手上路
威 望:1
帖 子:324
专家分:0
注 册:2005-12-21
收藏
 问题点数:0 回复次数:3 
JSP里调用了JavaBean的getxxx方法,此方法返回汉字,页面显示的时候,汉字乱码。

JavaBean:
package hh;
import java.text.DateFormat;
import java.util.*;
public class JspCalendar
{
Calendar calendar = null;
public JspCalendar() {
calendar = Calendar.getInstance();
Date trialTime = new Date();
calendar.setTime(trialTime);
}
public int getYear() {
return calendar.get(Calendar.YEAR);
}
public String getMonth() {
int m = getMonthInt();
String[] months = new String [] { "1", "2", "3","4", "5", "6", "7", "8", "9", "10", "11", "12" };
if (m > 12)
return "Unknown to Man";
return months[m - 1];
}
public String getDay() {
int x = getDayOfWeek();
String[] days = new String[] {"日","一", "二", "三", 四", "五", "六"};// 这些汉字显示乱码"
if (x > 7)
return "Unknown to Man";
return days[x - 1];
}
public int getMonthInt() {
return 1 + calendar.get(Calendar.MONTH);
}
public String getDate() {
return getMonthInt() + "/" + getDayOfMonth() + "/" + getYear();
}
public String getTime() {
return getHour() + ":" + getMinute() + ":" + getSecond();
}
public int getDayOfMonth() {
return calendar.get(Calendar.DAY_OF_MONTH);
}
public int getDayOfYear() {
return calendar.get(Calendar.DAY_OF_YEAR);
}
public int getWeekOfYear() {
return calendar.get(Calendar.WEEK_OF_YEAR);
}
public int getWeekOfMonth() {
return calendar.get(Calendar.WEEK_OF_MONTH);
}
public int getDayOfWeek() {
return calendar.get(Calendar.DAY_OF_WEEK);
}
public int getHour() {
return calendar.get(Calendar.HOUR_OF_DAY);
}
public int getMinute() {
return calendar.get(Calendar.MINUTE);
}
public int getSecond() {
return calendar.get(Calendar.SECOND);
}
public static void main(String args[]) {
JspCalendar db = new JspCalendar();
p("date: " + db.getDayOfMonth());
p("year: " + db.getYear());
p("month: " + db.getMonth());
p("time: " + db.getTime());
p("date: " + db.getDate());
p("Day: " + db.getDay());
p("DayOfYear: " + db.getDayOfYear());
p("WeekOfYear: " + db.getWeekOfYear());
p("era: " + db.getEra());
p("ampm: " + db.getAMPM());
p("DST: " + db.getDSTOffset());
p("ZONE Offset: " + db.getZoneOffset());
p("TIMEZONE: " + db.getUSTimeZone());
}
private static void p(String x) {
System.out.println(x);
}
public int getEra() {
return calendar.get(Calendar.ERA);
}
public String getUSTimeZone() {
String[] zones = new String[] {"Hawaii", "Alaskan", "Pacific","Mountain", "Central", "Eastern"};
return zones[10 - getZoneOffset()];
}
public int getZoneOffset() {
return calendar.get(Calendar.ZONE_OFFSET)/(60*60*1000);
}
public int getDSTOffset() {
return calendar.get(Calendar.DST_OFFSET)/(60*60*1000);
}
public int getAMPM() {
return calendar.get(Calendar.AM_PM);
}
调用它的JSP(其实是date.inc调用的):
<%@ include file="header.inc"%> [font color=RED]//header.inc文件内容见下. [/font]
<jsp:useBean id='clock' scope='page' class='hh.JspCalendar' type="hh.JspCalendar" />
<%@ page language="java" import="java.sql.*" %>
<jsp:useBean id="workM" scope="page" class="hh.faq" />
<TABLE border=0 cellPadding=0 cellSpacing=0 width=760 align="center">
<TBODY>
<tr><td align="left" height=25><%if(session.getAttribute("username")!=null){out.println(session.getAttribute("username"));}%> 当前位置:<a href="../index.jsp">首页</a> -&gt; 成员服务 </td>
<td align="right"> <%@ include file="date.inc"%> </td>//[font color=RED]在此处调用,显示乱码。date.inc文件内容见下。[/font]
</tr>
<TR bgColor=#3399ff>
<TD height=1 colspan="2"><IMG height=1 src="images/spacer.gif"
width=16></TD></TR>
<tr><td height=10 colspan="2"><IMG height=1 src="images/spacer.gif"
width=16></td></tr>
</TBODY></TABLE>
<table align="center" border="0" width="760" cellspacing="0" cellpadding="0" height="355">
<tr><td width="150" height="355" valign="top">
<%@ include file="table.inc"%></td><td width="10" height="100%"></td>
<td width="1" height="100%" bgcolor="#3399ff"></td>
<td width="10" height="100%"></td>
<td width="589" height="331" valign="top" background="images/bg1.gif">
<table border="0" width="100%" cellspacing="0" cellpadding="0" height="307">
<tr align=center>
<td width="100%" colspan="4" height="20" bgcolor="#3399ff">&nbsp;<font color="#ffffff">新成员注册新成员注册新成员注册新成员注册</font> </td></tr>
<tr> <td width="100%" align="center" colspan="4" height="36"> 粗体内容必须填写 </td></tr>
<form name="form1" method="post" action="reg_ok.jsp">
<tr><td align="right" ><b>登录名称:</b></td><td >&nbsp;</td>
<td colspan="2" ><input type="text" name="logname" value="wangxining"></td></tr>
<tr> <td align="right"><b>真实姓名:</b></td><td>&nbsp;</td><td colspan="2"> <input type="text" name="realname" value="wangwang"></td></tr><tr> <td align="right"><b>您的密码:</b></td><td>&nbsp;</td><td colspan="2">
<input type="password" name="passwd1" value="blueriver"></td></tr><tr>
<td align="right"><b>密码确认:</b></td><td>&nbsp;</td><td colspan="2"> <input type="password" name="passwd2" value="blueriver"></td></tr>
<tr> <td align="right"><b>电子邮件:</b></td><td>&nbsp;</td><td colspan="2"> <input type="text" name="email" value="wang@263.net"></td></tr>
<tr> <td align="right"><b>性&nbsp;&nbsp;&nbsp;&nbsp;别:</b></td><td>&nbsp;</td>
<td colspan="2"> <input type="radio" name="Gender" value="0" checked>
男 <input type="radio" name="Gender" value="1">女 </td></tr><tr> <td align="right">联系电话:</td><td>&nbsp;</td><td colspan="2"><input type="text" name="phone" value=""></td></tr>
<tr> <td align="right">密码提示问题:</td><td>&nbsp;</td><td colspan="2"> <input type="text" name="problem" value=""></td></tr><tr>
<td align="right">提示答案:</td><td>&nbsp;</td><td colspan="2">
<input type="text" name="answer" value=""></td></tr><tr> <td align="right">省市:</td>
<td>&nbsp;</td><td colspan="2">
<select name="Province" size="1">
<option value="null" >请选择</option>
<option value="Beijing">北京</option>
<option value="Tianjin" >天津</option>
<option value="Shanghai" >上海</option>
<option value="Chongqing" >重庆</option>
<option value="Hebei" >河北</option>
<option value="Henan" >河南</option>
<option value="Heilongjiang">黑龙江</option>
<option value="Jinlin" >吉林</option>
<option value="Shenyang" >辽宁</option>
<option value="Neimenggu" >内蒙古</option>
<option value="Hainan" >海南</option>
<option value="Shanxi" >山西</option>
<option value="Shanxi3" >陕西</option>
<option value="Shandong" >山东</option>
<option value="Jiangsu" >江苏</option>
<option value="Zhejiang" >浙江</option>
<option value="Anhui" >安徽</option>
<option value="Jiangxi" >江西</option>
<option value="Fujian" >福建</option>
<option value="Gansu" >甘肃</option>
<option value="Ningxia" >宁夏</option>
<option value="Qinghai" >青海</option>
<option value="Xinjiang" >新疆</option>
<option value="Hubei" >湖北</option>
<option value="Hunan" >湖南</option>
<option value="Guangxi" >广西</option>
<option value="Sichuan" >四川</option>
<option value="Guizhou" >贵州</option>
<option value="Yunnan" >云南</option>
<option value="Xizang" >西藏</option>
<option value="Hongkong" >香港</option>
<option value="Aomen" >澳门</option>
<option value="Taiwan" >台湾</option>
</select></td></tr>
<tr> <td align="right">教育程度:</td><td>&nbsp;</td><td colspan="2">
<select name=education size=1>
<option value="null" selected>请选 ...</option>
<option value="Below high school">高中以下程度</option>
<option value="high school/5-year college">高中/中专</option>
<option value="College/University">大学/专科</option>
<option value="Graduate and above">研究生以上</option></select></td></tr>
<tr> <td align="right">兴趣爱好:</td><td>&nbsp;</td>
<td> <input type=checkbox name=hobbies value="1@">计算机行业/软硬件
&nbsp;&nbsp;&nbsp;&nbsp;
<input type=checkbox name=hobbies value="2@">
旅游、度假 </td></tr>
<tr> <td align="right">&nbsp;</td><td>&nbsp;</td>
<td> <input type=checkbox name=hobbies value="3@">
上网聊天、游戏
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type=checkbox name=hobbies value="4@">
阅读、图书音像</td> </tr>
<tr> <td align="right">&nbsp;</td><td>&nbsp;</td>
<td> <input type=checkbox name=hobbies value="5,">
文化/教育/育儿
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type=checkbox name=hobbies value="6,">
情感生活 </td>
</tr><tr> <td align="right">&nbsp;</td><td>&nbsp;</td>
<td> <input type=checkbox name=hobbies value="7,">
影音娱乐
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type=checkbox name=hobbies value="8,">
生活资讯 </td>
</tr><tr> <td align="right">&nbsp;</td><td>&nbsp;</td><td>
<input type=checkbox name=hobbies value="9,">
医疗健康
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type=checkbox name=hobbies value="10,">
房产家居 </td>
</tr><tr> <td align="right" valign="top">自我简介:</td><td>&nbsp;</td><td colspan="2">
<textarea name="selfintro" cols="40" rows="5">selddddddddd</textarea>
</td></tr><tr> <td align="right">&nbsp;</td><td>&nbsp;</td>
<td align="left" colspan="2">
<input type="button" value="填好了" onclick="sub()">
<input type="reset" value="重填"><script language="javascript">
function sub()
{
if(document.form1.logname.value=="")
{
window.alert("请填写你的登录名!");
window.form1.logname.focus();
return false;
}
if(document.form1.realname.value=="")
{
window.alert("请填写你的真名!");
window.form1.realname.focus();
return false;
}
if(document.form1.passwd1.value=="")
{
window.alert("请填写密码!");
window.form1.passwd1.focus();
return false;
}
if(document.form1.passwd1.value!=document.form1.passwd2.value)
{
window.alert("请验证密码!");
window.form1.passwd2.focus();
return false;
}
if(document.form1.email.value=="")
{
window.alert("请填写你的E_mail!");
window.form1.email.focus();
return false;
}
document.form1.submit();
}
</script> </td></tr></form></table> </td></tr></table>
<%@ include file="footer.inc"%>

date.inc:
<td align="right">
<font color="Red"><jsp:getProperty name="clock" property="year"/></font>年
<font color="Red"><jsp:getProperty name="clock" property="month"/></font>月
<font color="Red"><jsp:getProperty name="clock" property="dayOfMonth"/></font>日
星期<font color="Blue"><b><jsp:getProperty name="clock" property="day"/></b></font>&nbsp;&nbsp;
</td>
<td>当前时间为 <jsp:getProperty name="clock" property="time"/>
</td>

header.inc:

<HTML><HEAD><TITLE></TITLE>
<META content="text/html; charset=gb2312" http-equiv=Content-Type>
<LINK href="../css/site.css" rel=stylesheet>
<META content="MSHTML 5.00.2014.210" name=GENERATOR></HEAD>
<script language="javascript" src="../js/all.js">
</script>
<BODY bgColor=#ffffff leftMargin=0 topMargin=5 marginheight="5" marginwidth="5">
<table align="center" border="0" width="760" height="18" bgcolor="#3399FF" cellspacing="0">
<tr><td width="100%">
<p><a class="x" href="list_all_user.jsp">用户列表</a> | <a class="x" href="../book_store/index.jsp">商品列表</a> | <a class="x" href="../search/index.jsp">查询商品</a> | <a class="x" href="../book_store/all_category.jsp">商品分类</a> | <a class="x" href="../book_store/new_goods.jsp">新货上架</a> | <a class="x" href="../book_store/good_price.jsp">特价市场</a> | <a class="x" href="../book_store/short_goods.jsp">缺货登记</a> | <a class="x" href="../book_store/shopcart.jsp" target="_blank">购物车</a> | <a class="x" href="../book_store/order.jsp" target="_blank">订单</a> | <a class="x" href="forum/index.jsp" target="_blank">论坛</a> | <a class="x" href="chat/index.jsp" target="_blank">聊天室</a></td> </tr> </table>

此种情况要怎么解决呢?谢谢!!!

[此贴子已经被作者于2006-7-2 8:15:41编辑过]

搜索更多相关主题的帖子: JavaBean getxxx 汉字 JSP 
2006-07-02 08:07
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 
你要调置你的网页的编码格式
为GBK或者为gb2312就可以了

可惜不是你,陪我到最后
2006-07-02 10:10
球球
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:1146
专家分:265
注 册:2005-11-28
收藏
得分:0 
头文件的网页编码格式

好累
2006-07-12 00:17
独孤无痕
Rank: 1
等 级:新手上路
威 望:2
帖 子:159
专家分:0
注 册:2006-7-12
收藏
得分:0 

顶 ,斑竹正确!


2006-07-12 12:55
快速回复:JSP里调用了JavaBean的getxxx方法,此方法返回汉字,页面显示的时候, ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.030333 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved