实验4 JavaBean组件开发.docx

上传人:b****8 文档编号:8957803 上传时间:2023-05-16 格式:DOCX 页数:17 大小:17.90KB
下载 相关 举报
实验4 JavaBean组件开发.docx_第1页
第1页 / 共17页
实验4 JavaBean组件开发.docx_第2页
第2页 / 共17页
实验4 JavaBean组件开发.docx_第3页
第3页 / 共17页
实验4 JavaBean组件开发.docx_第4页
第4页 / 共17页
实验4 JavaBean组件开发.docx_第5页
第5页 / 共17页
实验4 JavaBean组件开发.docx_第6页
第6页 / 共17页
实验4 JavaBean组件开发.docx_第7页
第7页 / 共17页
实验4 JavaBean组件开发.docx_第8页
第8页 / 共17页
实验4 JavaBean组件开发.docx_第9页
第9页 / 共17页
实验4 JavaBean组件开发.docx_第10页
第10页 / 共17页
实验4 JavaBean组件开发.docx_第11页
第11页 / 共17页
实验4 JavaBean组件开发.docx_第12页
第12页 / 共17页
实验4 JavaBean组件开发.docx_第13页
第13页 / 共17页
实验4 JavaBean组件开发.docx_第14页
第14页 / 共17页
实验4 JavaBean组件开发.docx_第15页
第15页 / 共17页
实验4 JavaBean组件开发.docx_第16页
第16页 / 共17页
实验4 JavaBean组件开发.docx_第17页
第17页 / 共17页
亲,该文档总共17页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

实验4 JavaBean组件开发.docx

《实验4 JavaBean组件开发.docx》由会员分享,可在线阅读,更多相关《实验4 JavaBean组件开发.docx(17页珍藏版)》请在冰点文库上搜索。

实验4 JavaBean组件开发.docx

实验4JavaBean组件开发

实验四JavaBean组件开发

一、实验目的

1)应用Eclipse创建JavaBean;

2)了解JavaBean的基本结构,基础知识;

3)应用JavaBean开发应用程序。

二、实验内容

2.1.应用Eclipse新建一个名为Book的值JavaBean。

要求该JavaBean含有属性:

bookId(String),bookName(String),bookPrice(double),并编写getter,setter函数。

2.2利用JavaBean开发一个“购物车”,要求实现以下功能:

(1)添加商品到购物车;

(2)列出购物车中的商品;(3)删除货物。

2.3验证附录实例程序,写出执行效果与主要功能代码:

附录

例1:

网站计数器

Counter.java:

publicclassCounter

{longcount=0;

publicsynchronizedlonggetCount()

{count++;

returncount;

}

}

count.jsp:

<%@pagecontentType="text/html;charset=GB2312"%>

<%@pageimport="Counter"%>

useBeanid="people"class="Counter"scope="application">

useBean>

<%if(session.isNew())

{%>

您是第

getPropertyname="people"property="count"/>

位访问本站的人

<%

}

else

{out.print("禁止通过刷新增加计数!

");

}

%>

例2:

随机分配一个1到100之间的数,请猜测这个数是多少?

GuessNumber.java:

packageguess.bean

publicclassGuessNumber

{intanswer=0,//实际答案。

guessNumber=0,//客户猜测的数。

guessCount=0;//客户猜到正确答案之前所用的次数。

Stringresult=null;

publicvoidsetAnswer(intn)

{answer=n;

guessCount=0;

}

publicintgetAnswer()

{returnanswer;

}

publicvoidsetGuessNumber(intn)

{guessNumber=n;

guessCount++;

if(guessNumber==answer)

result="恭喜,猜对了";

elseif(guessNumber>answer)

result="猜大了";

elseif(guessNumber

result="猜小了";

}

publicintgetGuessNumber()

{returnguessNumber;

}

publicintgetGuessCount()

{returnguessCount;

}

publicStringgetResult()

{returnresult;

}

}

获取一个随机数页面

getNumber.jsp:

<%@pagecontentType="text/html;charset=GB2312"%>

<%@pageimport="guess.bean.GuessNumber"%>

<%intn=(int)(Math.random()*100)+1;%>

useBeanid="guess"class="guess.bean.GuessNumber"scope="session">

useBean>

setPropertyname="guess"property="answer"value="<%=n%>"/>

随机给你一个1到100之间的数,请猜测这个数是多少?

<%Stringstr=response.encodeRedirectURL("guess.jsp");

%>

"method=post>


输入你的猜测

猜数页面

guess.jsp:

<%@pagecontentType="text/html;charset=GB2312"%>

<%@pageimport="guess.bean.GuessNumber"%>

useBeanid="guess"class="guess.bean.GuessNumber"scope="session">

useBean>

setPropertyname="guess"property="guessNumber"param="guessNumber"/>


getPropertyname="guess"property="result"/>


这是第

getPropertyname="guess"property="guessCount"/>


你给出的数是

getPropertyname="guess"property="guessNumber"/>

<%Stringstr=response.encodeRedirectURL("guess.jsp");

%>

"method=post>


再输入你的猜测

<%Stringstr1=response.encodeRedirectURL("getNumber.jsp");

%>


"method="post"name="f">

例3:

显示日历时间:

JSPCalendar.java:

packagecalendar.bean

importjava.util.*;

publicclassJSPCalendar

{Calendarcalendar=null;

intyear,dayOfMonth,dayOfYear,weekOfYear,

weekOfMonth,dayOfWeek,hour,minute,second;

Stringday,date,time;

publicJSPCalendar()

{calendar=Calendar.getInstance();

Datetime=newDate();

calendar.setTime(time);

}

//获取年份:

publicintgetYear()

{returncalendar.get(Calendar.YEAR);

}

//获取月,进行格式处理:

publicStringgetMonth(){

intm=1+calendar.get(Calendar.MONTH);

Stringmonths[]={"1","2","3","4","5","6","7","8","9","10","11","12"};

if(m>12)

return"0";

returnmonths[m-1];

}

//获取星期几,进行格式处理:

publicStringgetDay()

{intn=getDayOfWeek();

Stringdays[]={"日","一","二","三","四","五","六"};

if(n>7)

return"星期?

";

returndays[n];

}

//获取:

年、月、日

publicStringgetDate()

{returngetYear()+"/"+getMonth()+"/"+getDayOfMonth();

}

publicStringgetTime()//获取:

时:

分:

{returngetHour()+":

"+getMinute()+":

"+getSecond();

}

publicintgetDayOfMonth()//获取当前时间是一月中的哪一天

{returncalendar.get(Calendar.DAY_OF_MONTH);

}

publicintgetDayOfYear()//获取当前时间是一年中的哪一天

{returncalendar.get(Calendar.DAY_OF_YEAR);

}

publicintgetWeekOfYear()//获取当前时间是一年中的哪个星期

{returncalendar.get(Calendar.WEEK_OF_YEAR);

}

publicintgetWeekOfMonth()//获取当前时间是一年中的哪个星期

{returncalendar.get(Calendar.WEEK_OF_MONTH);

}

publicintgetDayOfWeek()//获取当前时间是一周中的哪一天

{returncalendar.get(Calendar.DAY_OF_WEEK)-1;

}

publicintgetHour()//获取小时

{returncalendar.get(Calendar.HOUR_OF_DAY);

}

publicintgetMinute()//获取分钟

{returncalendar.get(Calendar.MINUTE);

}

publicintgetSecond()//获取秒

{returncalendar.get(Calendar.SECOND);

}

}

显示日历时间的页面(效果如图6.33所示)

calendar.jsp:

<%@pagecontentType="text/html;charset=GB2312"%>

<%@pageimport="calendar.bean.JSPCalendar"%>

useBeanid="clock"class="calendar.bean.JSPCalendar"scope="page">

useBean>

getPropertyname="clock"property="year"/>年

getPropertyname="clock"property="month"/>月

getPropertyname="clock"property="dayOfMonth"/>日

星期

getPropertyname="clock"property="day"/>


当前时间为

getPropertyname="clock"property="time"/>


今天是今年的第

getPropertyname="clock"property="dayOfYear"/>天


本周是今年的第

getPropertyname="clock"property="weekOfYear"/>周


本周是本月的第

getPropertyname="clock"property="weekOfMonth"/>周

例4:

购物车

Car1.java:

importjava.util.*;

importjava.io.*;

publicclassCar1implementsSerializable

{Hashtablelist=newHashtable();

Stringitem="Welcome!

";

intmount=0;

Stringunit=null;

publicvoidCar1()

{}

publicvoidsetItem(StringnewItem)

{item=newItem;

}

publicvoidsetUnit(StringnewUnit)

{unit=newUnit;

}

publicvoidsetMount(intm)

{mount=m;

}

publicvoid添加商品到购物车()

{Stringstr="Name:

"+item+"Mount:

"+mount+"Unit:

"+unit;

list.put(item,str);

}

publicHashtable列出购物车中的商品()

{returnlist;

}

publicvoid删除货物(Strings)

{list.remove(s);

}

}

选择物品页面(效果如图6.19所示)

car.jsp:

<%@pagecontentType="text/html;charset=GB2312"%>

<%@pageimport="java.util.*"%>

<%@pageimport="Car1"%>

useBeanid="car1"class="Car1"scope="session">

useBean>

这里是第一百货商场,选择您要购买的商品添加到购物车:

<%Stringstr=response.encodeRedirectURL("add.jsp");

%>

"method=postname=form>

电视机

苹果

可口可乐

牛奶

茶叶

输入购买的数量:

选择计量单位:

公斤

您的购物车有如下商品:

<%Hashtablelist=car1.列出购物车中的商品();

EnumerationENUM=list.elements();

while(ENUM.hasMoreElements())

{Stringgoods=(String)ENUM.nextElement();

byteb[]=goods.getBytes("ISO-8859-1");

goods=newString(b);

out.print("
"+goods);

}

%>

<%Stringstr1=response.encodeRedirectURL("selectRemovedGoods.jsp");

%>

"method=postname=form>

添加货物到购物车页面(效果如图6.20所示)

add.jsp:

<%@pagecontentType="text/html;charset=GB2312"%>

<%@pageimport="java.util.*"%>

<%@pageimport="Car1"%>

useBeanid="car1"class="Car1"scope="session">

useBean>

setPropertyname="car1"property="*"/>

<%car1.添加商品到购物车();

%>

您的购物车有如下商品:

<%Hashtablelist=car1.列出购物车中的商品();

EnumerationENUM=list.elements();

while(ENUM.hasMoreElements())

{Stringgoods=(String)ENUM.nextElement();

byteb[]=goods.getBytes("ISO-8859-1");

goods=newString(b);

out.print("
"+goods);

}

%>

<%Stringstr=response.encodeRedirectURL("car.jsp");

%>


"method=postname=form>

<%Stringstr1=response.encodeRedirectURL("selectRemovedGoods.jsp");

%>


"method=postname=form>

选择删除货物的页面(效果如图6.21所示)

selectRemovedGoods.jsp:

<%@pagecontentType="text/html;charset=GB2312"%>

<%@pageimport="java.util.*"%>

<%@pageimport="Car1"%>

useBeanid="car1"class="Car1"scope="session">

useBean>

选择从购物车删除的商品:

<%Stringstr=response.encodeRedirectURL("removeWork.jsp");

%>

"method=postname=form2>

电视机

苹果

可口可乐

牛奶

茶叶

您的购物车有如下商品:

<%Hashtablelist=car1.列出购物车中的商品();

EnumerationENUM=list.elements();

while(ENUM.hasMoreElements())

{Stringgoods=(String)ENUM.nextElement();

byteb[]=goods.getBytes("ISO-8859-1");

goods=newString(b);

out.print("
"+goods);

}

%>

<%Stringstr1=response.encodeRedirectURL("car.jsp");

%>

"method=postname=form>

删除货物页面(效果如图6.22所示)

removeWork.jsp:

<%@pagecontentType="text/html;charset=GB2312"%>

<%@pageimport="java.util.*"%>

<%@pageimport="Car1"%>

useBeanid="car1"class="Car1"scope="session">

useBean>

<%Stringname=request.getParameter("deleteitem");

if(name==null)

{name="";

}

bytec[]=name.getBytes("ISO-8859-1");

name=newString(c);

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 工作范文 > 制度规范

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2