网络编程基础课程设计文档网上书店系统姓名李璐祎学号06043123班级060706指导教师廖湖声目录网络编程基础课程设计文档...........................................................................................................1网上书店系统...................................................................................................................................1一、功能需求...........................................................................................................................3二、整体架构设计...................................................................................................................3三、具体实现与算法设计.......................................................................................................5四、网上书店系统全部实现代码.........................................................................................10五、测试及结果.....................................................................................................................26一、功能需求1、提供基于关键字的查询功能。2、提供购物车,保存选中的图书和数量。3、提供图书定单。4、支持反复查询、图书选择和定单浏览。5、提供浏览器界面。6、图书信息从JDBC数据库中读取。7、购物车本次会话有效。二、整体架构设计1、架构思想:基于J2EE体系结构(1)表示逻辑层:JSP(2)控制逻辑层:Servlet+JavaBean+会话Bean(3)数据表示层:实体Bean2、页面转移关系设计:查询页search.jsp购物车页cart.jsp图书目录页directoty.jsp模糊查询再次查询查看购物车再次查询继续选书定单页order.jsp结算3、实现技术(1)采用MVC模式JSP实现页面视图ViewServlet实现控制ControlEJB实现业务模型Model(2)交互关系JSP表单动作(form)提交给CtrlServlet控制Servlet转发调用JSP页面(3)数据传输Servlet调用EJB对象获得数据Servlet通过session参数传递给JSP页面4、对象模型设计(1)界面对象:查询页search.jsp,图书目录页directory.jsp,购物车页cart.jsp,定单页order.jsp(2)控制器:CtrlServlet负责响应输入命令包括:模糊查询key、查看购物车cart、结算order、继续选书continue、再次查询search(3)业务模型图书信息:BookBean实体类的会话Bean购物车信息:CartBean会话Bean5、动态转移关系图search.jspdirectory.jspcart.jspBookStoreBeanCartBeanorder.jspCtrlServletkeycartsearchordercontinuesearchaddgetssetfindget三、具体实现与算法设计1、数据设计:(1)图书信息(数据库表、查询结果)书号ISBNchar[24]主键书名Titlechar[64]作者Authorchar[32]出版社Presschar[32]价格Pricedouble(2)购物车信息书号ISBNchar[24]数量Numberinteger(3)共享数据a.查询结果和选中的图书信息b.作为session参数在NetBeans6.5版本中建立内置的JDBC数据库BookStore,建立表BookStore,输入的测试数据如下:2、EJB接口设计(1)BookstoreFacade管理图书信息(封装数据库)search(Stringkey):根据关键字从数据库中查找出图书信息记录(数组);条件:书名、作者、出版社名中包含关键字;根据书号,找出图书信息记录具体算法如下:publicArrayListBookstoresearch(Stringkey){ListBookstoreall=this.findAll();if(all.isEmpty()){returnnull;}ArrayListBookstoreresult=newArrayListBookstore();for(Bookstorecurrent:all){if(current.getTitle().toLowerCase().contains(key)||current.getAuthor().toLowerCase().contains(key)||current.getPress().toLowerCase().contains(key)){result.add(current);}}returnresult;}(2)CartBeanBean管理购物车信息(本次会话有效)add(Stringisbn,Stringtitle,doubleprice)添加记录代码如下:privateArrayListCartrecord;publicvoidadd(Stringisbn,Stringtitle,doubleprice){record.add(newCart(isbn,title,price,1));}publicArrayListCartget()获得记录数组实现代码如下:publicArrayListCartget(){ArrayListCartresult=newArrayListCart();for(Cartcurrent:record){result.add(newCart(current.getIsbn(),current.getTitle(),current.getPrice(),current.getNumber()));}returnresult;}set(Stringisbn,intnumber)设置购买数量publicvoidset(Stringisbn,intnumber){for(Cartcurrent:record){if(current.getIsbn().equals(isbn)){current.setNumber(number);}}}3、控制器(CtrlServlet)的输入信息(1)来自查询页search.jsppage=“search”&key=关键字(2)来自图书目录页directory.jsppage=“directory”(3)来自购物车页cart.jsppage=“cart”(4)控制器(CtrlServlet)的实现代码packagebookstore;importBookStore.BookstoreFacadeRemote;importBookStore.CartBeanRemote;importjava.io.IOException;importjava.io.PrintWriter;importjava.util.Enumeration;importjavax.ejb.EJB;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;/****@authorliluyi*/publicclassCtrlServletextendsHttpServlet{@EJBprivateCartBeanRemotecartBeanBean;@EJBprivateBookstoreFacadeRemotebookstoreFacade;protectedvoidprocessRequest(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{response.setContentType(text/html;charset=UTF-8);PrintWriterout=response.getWriter();request.setCharacterEncoding(UTF-8);try{if(request.getParameter(page).equals(search)){//从search.jsp定向到directory.jspStringkeyword=request.getParameter(key);request.getSession().setAttribute(query,keyword);request.getSession().setAttribute(directory,bookstoreFacade.search(keyword.toLowerCase()));response.sendRedirect(directory.jsp);}if(request.getParameter(page).equals(directory)){//从directory.jsp定向到cart.jspif(!cartBeanBean.initialed()){cartBeanBean.create();}Enumerationnames=request.getParameterNames();while(names.hasMoreElements()){Stringname=(String)names.nextElement();if(!name.equals(page)&&!name.equals(submit)){if(!cartBeanBean.exist(request.getParameter(name))){cartBeanBean.add(request.getParameter(name),bookstoreFacade.find(request.getParameter(name)).getTitle(),bookstoreFacade.find(request.getParameter(name)).getPrice());}}}request.getSession().setAttribute(cart,cartBeanBean.get());response.sendRedirect(cart.jsp);}if(request.getParameter(page).equals(cart)){//从cart.jsp定向到order.jspdoublesum=0;Enumerationnames=request.getParameterNames();while(names.hasMoreElements()){Stringname=(String)names.nextElement();if