实验报告一、实验题目编程实现“网上书店系统”二、题目分析1、需求分析要开发一个系统,首先要了解该系统到底想做什么,需要实现怎样的功能,这就是需求分析。网上书店从大的需求方面看,就是要实现通过互联网能够进行书籍的购买及相关的管理等功能,大致需要提供下面的一系列功能:*浏览书籍(模糊查询)*选择需要购买的书籍*可以继续选择别的书籍,并且可以购买该书籍*选择了需要购买的书籍后,进行购买书籍操作*保存选中的图书和数量UML图:2、数据分析图书信息数据库购物车本次会话有效浏览书籍购买书籍登记数量和送货地址三、实验设计1、页面转移关系2、对象模型(1)界面对象(View)*查询页Search.jsp*图书目录页Directory.jsp*购物车页Cart.jsp*定单页Order.jsp(2)控制器(Control)*CtrlServlet负责响应输入命令*包括:模糊查询key、查看购物车cart、结算order、继续选书continue、再次查询search(3)业务模型*图书信息:BookBean实体类的会话Bean*购物车信息:CartBean会话Bean查看购物车再次查询继续选书结算查询页Search.jsp购物车页Cart.jsp图书目录页Directoty.jsp模糊查询再次查询定单页Order.jsp3、数据设计(1)图书信息(数据库表、查询结果)*书号ISBNchar[24]主键*书名Titlechar[64]*作者Authorchar[32]*出版社Presschar[32]*价格Pricedouble(2)购物车信息*书号ISBNchar[24]*数量Numberinteger(3)共享数据*查询结果和选中的图书信息*作为session参数4、动态模型Search.jspDirectory.jspCart.jspBookBeanCartBeanOrder.jspCtrlServletkeycartsearchordercontinuesearchaddgetssetfindget5、EJB接口设计(1)BookBean管理图书信息(封装数据库)*find(stringkey)*根据关键字从数据库中查找出图书信息记录(数组)条件:书名、作者、出版社名中包含关键字get(stringisbn)*根据书号,找出图书信息记录(2)CartBean管理购物车信息(本次会话有效)*add(stringisbn)添加记录*gest()获得记录数组*set(stringisbn,integernum)设置购买数量5、控制器(1)CtrlServlet的输入*来自查询页page=“search”&key=关键字*来自图书目录页page=“directory”&submit=“cart”&choose=书号数组page=“directoty”&submit=“search”*来自购物车页page=“cart”&submit=“order”page=“cart”&submit=“continue”page=“cart”&submit=“search”(2)算法概要*模糊查询search按照key从BookBean查询图书,转交图书目录页*查看购物车将选定图书存入CartBean,转交购物车页*结算计算CartBean中的图书总价,从BookBean获得图书信息,连同地址,转交定单页*继续选书返回图书目录页*再次查询转向查询页*异常处理直接生成错误信息页面四、实现技术1、采用MVC模式(1)JSP实现页面视图View(2)Servlet实现控制Control(3)EJB实现业务模型Model2、交互关系(1)JSP表单动作(form)CtrlServlet(2)Servlet转发JSP页面3、数据传输(1)Servlet调用EJB对象获得数据(2)Servlet通过session参数传递给JSP页面五、实验源代码1、BooksOnline-ejb(1)Books①Books.java:importjava.io.Serializable;importjavax.persistence.Column;importjavax.persistence.Entity;importjavax.persistence.Id;javax.persistence.NamedQueries;importjavax.persistence.NamedQuery;importjavax.persistence.Table;@Entity@Table(name=BOOKS)@NamedQueries({@NamedQuery(name=Books.findByIsbn,query=SELECTbFROMBooksbWHEREb.isbn=:isbn),@NamedQuery(name=Books.findByTitle,query=SELECTbFROMBooksbWHEREb.title=:title),@NamedQuery(name=Books.findByAuthor,query=SELECTbFROMBooksbWHEREb.author=:author),@NamedQuery(name=Books.findByPress,query=SELECTbFROMBooksbWHEREb.press=:press),@NamedQuery(name=Books.findByPrice,query=SELECTbFROMBooksbWHEREb.price=:price)})publicclassBooksimplementsSerializable{privatestaticfinallongserialVersionUID=1L;@Id@Column(name=ISBN,nullable=false)privateStringisbn;@Column(name=TITLE)privateStringtitle;@Column(name=AUTHOR)privateStringauthor;@Column(name=PRESS)privateStringpress;@Column(name=PRICE)privateDoubleprice;publicBooks(){}publicBooks(Stringisbn){this.isbn=isbn;}publicStringgetIsbn(){returnisbn;}publicvoidsetIsbn(Stringisbn){this.isbn=isbn;}publicStringgetTitle(){returntitle;}publicvoidsetTitle(Stringtitle){this.title=title;}publicStringgetAuthor(){returnauthor;}publicvoidsetAuthor(Stringauthor){this.author=author;}publicStringgetPress(){returnpress;}publicvoidsetPress(Stringpress){this.press=press;}publicDoublegetPrice(){returnprice;}publicvoidsetPrice(Doubleprice){this.price=price;}@OverridepublicinthashCode(){inthash=0;hash+=(isbn!=null?isbn.hashCode():0);returnhash;}@Overridepublicbooleanequals(Objectobject){//TODO:Warning-thismethodwon'tworkinthecasetheidfieldsarenotsetif(!(objectinstanceofBooks)){returnfalse;}Booksother=(Books)object;if((this.isbn==null&&other.isbn!=null)||(this.isbn!=null&&!this.isbn.equals(other.isbn))){returnfalse;}returntrue;}@OverridepublicStringtoString(){returnBooks.Books[isbn=+isbn+];}}②BooksFacade.javaimportjava.util.List;importjava.util.ArrayList;importjavax.ejb.Stateless;importjavax.persistence.EntityManager;importjavax.persistence.PersistenceContext;@StatelesspublicclassBooksFacadeimplementsBooksFacadeRemote{@PersistenceContextprivateEntityManagerem;publicvoidcreate(Booksbooks){em.persist(books);}publicvoidedit(Booksbooks){em.merge(books);}publicvoidremove(Booksbooks){em.remove(em.merge(books));}publicBooksfind(Objectid){returnem.find(Books.class,id);}publicListBooksfindAll(){returnem.createQuery(selectobject(o)fromBooksaso).getResultList();}publicArrayListBookssearch(Stringkey){ListBooksall=this.findAll();if(all.isEmpty()){returnnull;}ArrayListBooksresult=newArrayListBooks();for(Bookscurrent:all){if(current.getTitle().toLowerCase().contains(key)||current.getAuthor().toLowerCase().contains(key)||current.getPress().toLowerCase().contains(key)){result.add(current);}}returnresult;}}③BooksFacadeRemote.javaimportjava.util.List;importjava.util.ArrayList;importjavax.ejb.Remote;@RemotepublicinterfaceBooksFacadeRemote{voidcreate(Booksbooks);(2)Cart①Cart.javaimportjava.io.Serializable;publicclassCartimplementsSerializable{privateStringisbn;privateStringtitle;privatedoubleprice;privateintnumber;publicCart(Stringisbn,Stringtitle,doubleprice,intnumber){this.isbn=isbn;this.title=title;this.price=price;this.number=number;}publicStringgetIsbn(){returnthis.isbn;}publicStringgetTitle(){returnthis.title;}publicdoublegetPrice(){retur