资源描述
、实验目的1)理解并掌握编码与测试相关理论与方法;2)进一步熟悉利用编程语言实现系统;3)要求学生理解测试在软件开发中的重要地位;4)学生了解测试用例与测试报告的相关书写规范、实验内容与要求1)根据系统的特点,选取javaWeb MVC开发模型实现该系统;2)系统的编码结构:边界类:1个html (项目首页),13个jsp (中间跳转的页面),一个css (界面美化功能),一个javascript (控制输入合法性)。控制类:一个servlet类(连接边界类和实体类)实体类:一个datebase类(连接数据库),一个book info 类(书本的 增删检查操作)服务器采用tomcat 。如下图展示:.C5S2015/1/1 20:27I .戶2015/1/1 20:27WEB-IWF2015/1/1 20:23,_ add.bcekj&p201S/1/1 21:212KE0 buyj年2015/1/1 21:212Bi gditjsp2015/1/1 21.212KBfailure_iTi.j5p2014/12/29 12:591KB.failure_r.jsp2014/12/2 12:591KBindesnhtrnl2015/1/120:342KBmsnager.jp2015/1/ 14:;S2ni3a1 Oa需ITvFl8m i/iOFM M1ES w聲KSKSlll|HP-4H管sK*密X第s.13耳UdXA.tx 京乂归丼匪粧棒K*s订单信息已审檢亮卑2012011 90血日制32U2KISi正在打印订单请稍候查询页面图书宿息査询*笛输入则需芫整)水目认关濯寧即可)咒输入矣键宇即可)查询结果躺号书色出匸牡32012011307卩唔it学阿51华柠20111665貼2nL2DL2La亦y梅b5K5畤4卒斗蔚;|20K-1-旳1D2ni2DL124旳卫手冃中05科斗社嗣t3300-12-1655B血管理员页面编耳书若ttft出am出丹曰期怕略1itrrry#iitW-1!-*科0ttikHA20329I11CK1jiWIKE3DH-34ieM.初】MILW1匚啊tlR亂蘿丸黄倆t30II4-KIQ1昌gfljamnoiiZi#土丈利!KS牡35逊3tl2kll3U囚迴tISE番k攀如t如WK越amoiimJfl12-160ffit201201西丹4如扯Ft20OD-2-19tttt添加图书页面添加图书信息不完整页面图书佶总nc narr?L三、完整开发代码(按照上面截图部署即可实现)Table.css:bodybackgro und:margi n: 15;body,table,th,td,i nput,select,textareafon t-family: T ahoma,Verda na,Arial,Helvetica,sa ns-serif;fon t-size: 12px;table.defaultborder: 1px solid #3366CC;border-collapse: collapse;width: 80%;table.default tdpaddi ng: 2 5 2 5;height: 26px;text-alig n: cen ter;border: 1px solid #ffffff; backgrou nd-color: #fOfOfO;table.default tr.title tdfon t-weight: bold;text-alig n: cen ter;backgrou nd: #99CCFF;color: red;border: 1px solid #ffffff; white-space: no wrap;height: 26px;table.default th.itemfon t-weight: bold;color: #333333;text-alig n: cen ter;vertical-alig n: top;paddi ng: 10px; fon t-size: 14px;xyh.css:body fon t-family: Arial, Helvetica, san s-serif;fon t-size:12px;color:#666666;backgrou nd:#fff;text-alig n:cen ter;* margi n:0;paddi ng:0;a color:#1E7ACE;text-decorati on:non e; a:hover color:#000;text-decoratio n:un derl in e;h3 fon t-size:14px;fo nt-weight:bold;pre,p color:#1E7ACE;margi n:4px;in put, select,textarea paddi ng:1px;margi n:2p x;fon t-size:11p x;.buttompadd in g:1px 10px;fo nt-size:12px;border:1px #1E7ACE solid;backgro un d:#D0F0FF; #formwrapper width:450p x;m argi n:15px auto;paddi ng:20px;text-alig n:left;border:1px solid #A4CDF2; fieldset paddi ng:10px;marg in-top:5px;border:1px solid #A4CDF2;backgrou nd:#fff;fieldset lege nd color:#1E7ACE;fo nt-weight:bold;paddi ng:3px 20px 3px 20px;border:1px solid #A4CDF2;backgro un d:#fff;fieldset label float:left;width:120px;text-alig n:right;paddi ng:4px;marg in :1px;fieldset div clear:left;marg in-bottom:2px;.in put width:120px;.en ter text-alig n:ce nter;.clear clear:both;Common.js:fun ctio n isNum( nu m) for(i=0;i9|c0) return false; return true;function check() if(form1.id.value=”) alert ( no id!);form1.id.focus();return false; if(form1. name.value=) alert(” no n ame!);form1. name.focus(); return false; if(form1.author.value=) alert(no author!);form1.author.focus(); return false;if(form1.press.value=”) alert( no press!);form1.press.focus();return false;if(form1.date.value=) alert(no date!) form1.date.focus(); return false;if(!isNum(form1.price) alert(price is wron g!) form1.price.focus(); return false;if(!isNum(form1.acco un t) alert(acco unt is wring!) form1.acco un t.focus(); return false;Bookinfo.java:package bea n;import java .Ian g.Stri ng;import java .Ian g.System;import java.sql.ResultSet;import java.sql.SQLExcepti on;import java.util.ArrayList;public class book infoprivate String id;private String name;private String author;private String press;private String date;private int price;private int acco unt;public void setName(Stri ng n ame) this. name = n ame;public String getName() return n ame;public void setAuthor(Stri ng author) this.author = author;public Stri ng getAuthor() return author;public void setDate(Stri ng date) this.date = date; public Stri ng getDate() return date;public void setPress(Stri ng press) this.press = press;public String getPress() retur n press;public void setld(Stri ng id) this.id = id; public Stri ng getId() return id;public void setAcco un t(i nt aco unt) this.acco unt = aco unt;public int getAcco un t() retur n acco unt; public void setPrice(int price) this.price = price;public int getPrice() retur n price;public static ArrayList getbooklist()ArrayList list=new ArrayList();String sql=select * from book.bookshop;dbbea n jdbc=new dbbea n();ResultSet rs=jdbc.executeQuery(sql);trywhile(rs. next()book info bi=new book info();bi.setld(rs.getStri ng(id);bi.setName(rs.getStri ng( name);bi.setAuthor(rs.getStri ng(author);bi.setPress(rs.getStri ng(press);bi.setDate(rs.getStri ng(date);bi.setPrice(rs.get In t(price);bi.setAcco un t(rs.get In t(acco un t); list.add(bi);rs.close();catch(SQLExceptio n e)e.pri ntStackTrace();jdbc.close();return list;/通过编号书名作者来搜索n ame,Stri ngpublic static ArrayListgetbookbyway(Stringid,Stringauthor)ArrayList list=new ArrayList();String sql=n ull;int way=0;if (!id.equals()way=way+1;if (!n ame.equals()way=way+10;if (!author.equals()way=way+100;if (way=1|way=11|way=101)sql=select * from book.bookshop where id=+id+;if (way=10)sql=select * from book.bookshop where n ame like %+name+%:if (way=100)sql=select * from bookshop where author like %+author+%;andif (way=110)sql=select * from bookshop where name like %+name+% author like %+author+%;dbbea n jdbc=new dbbea n();ResultSet rs=jdbc.executeQuery(sql);trywhile(rs. next()book info bi=new book info();bi.setDate(rs.getStri ng(date);bi.setPress(rs.getStri ng(press);bi.setAuthor(rs.getStri ng(author);bi.setName(rs.getStri ng( name);bi.setld(rs.getStri ng(id);bi.setPrice(rs.get In t(price);bi.setAcco un t(rs.get In t(acco un t);list.add(bi);rs.close();catch(SQLExceptio n e)System.out.pri ntl n(” no fin d);jdbc.close();return list;public static book info getbookbyid(Stri ng id)String sql=select * from book.bookshop where id=+id+;dbbea n jdbc=new dbbea n();ResultSet rs=jdbc.executeQuery(sql);book info bi = new book in fo();tryif(rs. next()bi.setDate(rs.getStri ng(date);bi.setPress(rs.getStri ng(press);bi.setAuthor(rs.getStri ng(author);bi.setName(rs.getStri ng( name);bi.setld(rs.getStri ng(id);bi.setPrice(rs.get In t(price);bi.setAcco un t(rs.get In t(acco un t);rs.close();catch(SQLExceptio n e)System.out.println(no find);jdbc.close();return bi;author,Stri ngsetpublic static intupdatebook(Stri ngid,Stri ngn ame,Stri ngpress,String date,int price,int account)int result=0;Stringsql=updatebook.bookshopn ame=+name+,author=+author+,press=+press+,date=+date+,price= +price+,acco un t=+acco un t+ where id=+id+;dbbea n jdbc=new dbbea n();result=jdbc.executeUpdate(sql);return result;public static int deletebook(String id)int result=O;String sql=delete from book.bookshop where id=+id+;dbbea n jdbc=new dbbea n();result=jdbc.executeUpdate(sql);return result;public static int addbook(String id,String name,String author,String press,String date,i nt price,i nt acco un t)intoint result=0;Stringsql=i nsertbook.bookshop(id ,n ame,author,press,date,price,acco unt)values(”+id+,”+name+,”+author+,”+press+,”+date+,+price+,+accoun t+);dbbea n jdbc=new dbbea n();result=jdbc.executeUpdate(sql);return result;Dbbean.java:package bea n;import java .Ian g.*;import java.sql.*;public class dbbea nprivate String driverStr=com.mysql.jdbc.Driver;Stri ngprivateconn Str=jdbc:mysql:/localhost:3306/book?characterE ncodi ng=utf-8;private String name=root;private Stri ng password=xyh201268;private Connection conn=n ull;private Stateme nt stmt=n ull;public dbbea n()tryClass.forName(driverStr);conn=DriverMa nager.getC onn ecti on(conn Str, name,password);stmt=c onn .createStateme nt();catch(Excepti on ex)System.out.pri ntln (conn ect to database is worry!);public int executeUpdate(Stri ng s)int result=0;tryresult=stmt.executeUpdate(s);catch(Excepti on ex)System.out.pri ntl n(update wrong!);return result;public ResultSet executeQuery(Stri ng s)ResultSet rs=n ull;tryrs=stmt.executeQuery(s);catch(Excepti on ex)System.out.pri ntl n(search wrong!);return rs;public void close()trystmt.close();c onn .close();catch(Excepti on ex)System.out.println(can not close the database!);Servlet.java:package servlets;import javax.servlet.ServletExceptio n;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResp on se;import java.i o.I OExcepti on;import java.util.ArrayList;import java .Ian g.*;/* Created by Admi nistrator on 14-5-31.*/public class Servlet exte nds HttpServlet protected void doGet(HttpServletRequestrequest, HttpServletResp onseresp on se) throws ServletExcepti on, I OExcepti on doPost(request, resp on se);protected void doPost(HttpServletRequestrequest, HttpServletResp onseresp on se) throws ServletExcepti on, I OExcepti on request.setCharacterE ncodi ng(utf-8);String action=request.getServletPath();if(act ion. equals(/servlets/depe nd.do)String value=request.getParameter(password);if(value.equals(123)ArrayList list= bean.bookinfo.getbooklist(); request.setAttribute(list, list);request.getRequestDispatcher(/ma nager.jsp).forward(request,resp on se);elseArrayList list= bea n.book in fo.getbooklist();request.setAttribute(list, list);request.getRequestDispatcher(/reader.jsp).forward(request,resp on se);else if (act ion. equals(/servlets/list_m.do)ArrayList list= bean.bookinfo.getbooklist(); request.setAttribute(list, list);request.getRequestDispatcher(/ma nager.jsp).forward(request,resp on se);else if (act ion. equals(/servlets/list_r.do)ArrayList list= bean.bookinfo.getbooklist(); request.setAttribute(list, list);request.getRequestDispatcher(/reader.jsp).forward(request,resp on se);else if(act ion. equals(/servlets/edit.do)String id =request.getParameter(id);bea n. book info bi= bea n.book in fo.getbookbyid(id);request.setAttribute(bi,bi);request.getRequestDispatcher(/edit.jsp).forward(request,resp on se);else if(act ion. equals(/servlets/update.do)String id=request.getParameter(id);String n ame=request.getParameter( name);String author=request.getParameter(author);String press=request.getParameter(press);String date=request.getParameter(date);String s_price=request.getParameter(price);String s_acco un t=request.getParameter(acco un t);int price=ln teger.parse In t(s_price);int acco unt=ln teger.parse In t(s_acco un t);r=intbea n. book in fo.updatebook(id, name,author,press,date,price,acco un t);if (r=1)request.getRequestDispatcher(/success_m.jsp).forward(request,resp on se);elserequest.getRequestDispatcher(/failure_m.jsp).forward(request,resp on se);else if (actio n.equals(/servlets/delete.do)String id=request.getParameter(id);int r= bea n. book in fo.deletebook(id);if (r=1)request.getRequestDispatcher(/success_m.jsp).forward(request,resp on se);elserequest.getRequestDispatcher(/failure_m.jsp).forward(request,resp on se);else if (act ion. equals(/servlets/add.do)String id=request.getParameter(id);String n ame=request.getParameter( name);String author=request.getParameter(author);String press=request.getParameter(press);String date=request.getParameter(date);String s_price=request.getParameter(price);String s_acco un t=request.getParameter(acco un t);int price=ln teger.parse In t(s_price);int acco unt=ln teger.parse In t(s_acco un t);press,int r=bea n. book in fo.addbook(id,n ame,author,date,price,acco un t);if (r=1)request.getRequestDispatcher(/success_m.jsp).forward(request,resp on se);elserequest.getRequestDispatcher(/failure_m.jsp).forward(request,resp on se);else if (act ion. equals(/servlets/buy.do)String buy=request.getParameter(buy);bea n. book info bi=bea n. book in fo.getbookbyid(buy);request.setAttribute(bi,bi); request.getRequestDispatcher(/buy.jsp).forward(request,resp on se);else if (act ion. equals(/servlets/search_r.do)String id=request.getParameter(id);String n ame=request.getParameter( name);String author=request.getParameter(author);String user=request.getParameter(user);list=ArrayListbea n. book in fo.getbookbyway(id, name,author);request.setAttribute(list, list);if (user.equals(reader)request.getRequestDispatcher(/sear_result_r.jsp).forward(request,resp on se);if (user.equals(ma nager)request.getRequestDispatcher(/sear_result_m.jsp).forward(request,resp on se);else if (act ion. equals(/servlets/check_buy.do)String id=request.getParameter(id);String s_nu m_buy=request.getParameter( nu m_buy);int nu m_buy =ln teger.parse In t(s_ nu m_buy);bea n. book info bi= bea n.book in fo.getbookbyid(id);if(bi.getAcco un t() =nu m_buy)request.setAttribute(bi,bi);request.setAttribute(s_ nu m_buy,s_ nu m_buy); request.getRequestDispatcher(/success_buy.jsp).forward(request,resp on se);elserequest.getRequestDispatcher(/failure_r.jsp).forward(request,resp on se);else if (act ion. equals(/servlets/pri nt_order.do)String id=request.getParameter(id);String s_nu m_buy=request.getParameter(s_ nu m_buy);bea n. book info bi= bea n.book in fo.getbookbyid(id);int nu m_buy =ln teger.parse In t(s_ nu m_buy);bea n. book in fo.updatebook(id,bi.getName(),bi.getAuthor(),bi.getPress(),bi.getDate( ),bi.getPrice(),(bi.getAcco un t()-nu m_buy);request.getRequestDispatcher(/pri nt_order.jsp).forward(request,resp on se);Add_book.jsp: vformn ame=form1on submit=retur ncheck()action=/bookshop/servlets/add.do method=post图书信息 编 号*(不能为空)书 名*(不能为空)作 者 *( 不能为空)name=press*(不能为空)出版日期 *( 不能为空)价 格 *(只能数字)库 存 *(只能数字)buy.jsp: 订单信息 编 号 in putvalue= n ame=n ame 书名 in putvalue= n ame=n ame 作者 in putvalue= name=author 出版社 i nputvalue= n ame=press 出版日期 inputtype=texttype=texttype=texttype=texttype=textvalue= name=date value= n ame=price 库 存 in putvalue= n ame=acco un t 购买数量 edit.jsp:type=texttype=textIan guage=java 修改信息 编 号 in puttype=textvalue= name=id 书名 input type=text value= n ame=n ame 作者 input type=text value= n ame=author 出版社 inputtype=textvalue= n ame=press 出版日期 inputtype=textvalue= name=date value= n ame=price type=text 库 存 in
展开阅读全文