JSP第四章课后题答案和实验报告.docx
第四章实验报告4.7.1 实现日历(1)日历bean,CalcendarBean.java 实现年、月、日等相关属性的设置和获取,代码如下:package beans;import java.util.*;public class CalcendarBeanCalendar calendar = null;int year,dayOfMonth,dayOfYear,weekOfYear,weekOfMonth,dayOfWeek,hour,minute,second;private String day;String date;String time;public CalcendarBean() calendar=Calendar.getInstance();Date time = new Date();calendar.setTime(time);public int getYear()return calendar.get(Calendar.YEAR);public String getMonth()int m=1+calendar.get(Calendar.MONTH);String months="1","2","3","4","5","6","7","8","9","10","11","12"if (m>12)return "0"return monthsm+1;public String getDay()int n = getDayOfWeek();String days="日","一","二","三","四","五","六"if (n>7)return "星期?"return daysn;public String getDate()return getYear() + "/" + getMonth() + "/" + getDayOfMonth();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)-1;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);(2)日历的显示由calendar.jsp 通过调用javabean来实现显示,代码如下:<% page contentType="text/html;charset=GB2312" %><% page import="beans.CalcendarBean" %><jsp:useBean id="cal" class="beans.CalcendarBean" scope="session" ></jsp:useBean><td align="center"><font color="red"><jsp:getProperty name="cal" property="year"/></font>年<font color="red"><jsp:getProperty name="cal" property="month"/></font>月<font color="red"><jsp:getProperty name="cal" property="dayOfMonth"/></font>日星期<font color="blue"><jsp:getProperty name="cal" property="day"/></font></td><BR>当前时间为<jsp:getProperty name="cal" property="time"/><BR>今天是今年的第<jsp:getProperty name="cal" property="dayOfYear"/>天<BR>本周是今年的第<jsp:getProperty name="cal" property="weekOfYear"/>周<BR>本周是本月的第<jsp:getProperty name="cal" property="weekOfMonth"/>周(3)实现日历显示:显示结果 4.7.1-14.7.2实现留言板(1)留言板javabean,NoteBean.java主要实现作者、标题、和他们的相关属性的设置和获取,代码如下:package beans;public class NoteBeanpublic String title;public String author;public String content;public String getTitle() return title;public void setTitle(String title) this.title=title;public String getAuthor() return author;public void setAuthor(String author) this.author=author;public String getContent() return content;public void setContent(String content) this.content=content;(2)留言板的添加,通过insert.jsp来实现,包括标题、作者、内容,代码如下:<% page contentType="text/html; charset=gb2312" language="java" %><center><h1>留言管理</h1><hr><br><form action ="insert_do.jsp" method="post"><table><tr><td colspan="3">添加新留言</td></tr><tr><td>标题:</td><td><input type="text" name="title"></td></tr><tr><td>作者:</td><td><input type="text" name="author"></td></tr><tr><td>内容:</td><td><textarea name="content" cols="30" rows="6"></textarea></td></tr><tr><td colspan="2"><input type="submit" value="添加"><input type="reset" value="重置"></td></tr></table></form></center>显示结果 4.7.2-1(3)留言板通过insert-do.jsp来实现,代码如下:<% page contentType="text/html; charset=gb2312" %><% page import="beans.NoteBean"%><center><h1>添加的内容如下所示</h1><hr><br><jsp:useBean id="sb" class="beans.NoteBean" scope="session" ></jsp:useBean><jsp:setProperty name="sb" property="*"/><h1>标题:<jsp:getProperty name="sb" property="title" /></h1><h1>作者:<jsp:getProperty name="sb" property="author" /></h1><h1>内容:<jsp:getProperty name="sb" property="content" /></h1></center>(4)执行结果如图:显示结果 4.7.2-24.7.3实现购物车(1)购物车通过JavaBean,ShopBean.java实现,用一个哈希表对象list存放购物的商品,包括名称、数量、单位属性的设置和获取,代码如下:package beans;import java.util.*;public class ShopBean Hashtable list=new Hashtable();String item=""int mount=0;String unit=""public void Carl() public void setItem(String newItem) item=newItem;public void setUnit(String newUnit) unit=newUnit;public void setMount(int m) mount=m;public Hashtable show() String str="productname:"+item+" count:n"+mount+" unit:n"+unit; list.put(mount,str); return list; (2)添加商品通过car.jsp来实现,代码如下:<% page contentType="text/html;charset=GB2312"%><% page import="java.util.*"%><% page import="beans.ShopBean"%><jsp:useBean id="shop" class="beans.ShopBean" scope="session" ></jsp:useBean><p>这里是百货广场,选择您要购买的商品添加到购物车:<FORM action="show.jsp" method=post name=form> <Select name="item" value="没选择"> <Option value="电视机">电视机 <Option value="苹果">苹果 <Option value="可口可乐">可口可乐 <Option value="牛奶">牛奶 <Option value="茶叶">茶叶 </Select><p>输入购买的数量:<Input type=text name="mount"><p>选择计量单位:<INPUT type="radio" name="unit" value="个">个 <INPUT type="radio" name="unit" value="公斤">公斤 <INPUT type="radio" name="unit" value="台">台 <INPUT type="radio" name="unit" value="瓶">瓶<Input type=submit value="提交添加"></FORM>显示如图 4.7.3-1(3)显示添加商品的信息通过show.jsp来实现,代码如下:<% page contentType="text/html;charset=GB2312"%><% page import="java.util.*"%><% page import="beans.ShopBean"%><jsp:useBean id="shop" class="beans.ShopBean" scope="session" ></jsp:useBean><jsp:setProperty name="shop" property="*" /><% shop.show(); %><P>您的购物车有如下商品:<% Hashtable list=shop.show(); Enumeration enuml=list.elements(); out.print("123"); while(enuml.hasMoreElements() out.print("456"); String goods=(String)enuml.nextElement(); byte b=goods.getBytes("ISO-8859-1"); goods=new String(b); out.print("<BR>"+goods);%>显示如图 4.7.3-2