实验报告一、实验题目编程实现“网上书店系统”二、题目分析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(1)图书信息(数据库表、查询结果)*书号ISBN char[24] 主键*书名Title char[64]*作者Author char[32]*出版社Press char[32]*价格Price double(2)购物车信息*书号ISBN char[24]*数量Number integer(3)共享数据*查询结果和选中的图书信息*作为session参数4、动态模型5、EJB接口设计(1)BookBean 管理图书信息(封装数据库)*find( string key )*根据关键字从数据库中查找出图书信息记录(数组)条件:书名、作者、出版社名中包含关键字get( string isbn )*根据书号,找出图书信息记录(2)CartBean 管理购物车信息(本次会话有效)*add( string isbn ) 添加记录*gest( ) 获得记录数组*set( string isbn, integer num ) 设置购买数量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:import java.io.Serializable;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.Id;dQueries;import dQuery;import javax.persistence.Table;@Entity@Table(name = "BOOKS")@NamedQueries({@NamedQuery(name = "Books.findByIsbn", query = "SELECT b FROM Booksb WHERE b.isbn = :isbn"), @NamedQuery(name = "Books.findByTitle", query = "SELECTb FROM Books b WHERE b.title = :title"), @NamedQuery(name = "Books.findByAuthor",query = "SELECT b FROM Books b WHERE b.author = :author"), @NamedQuery(name = "Books.findByPress", query = "SELECT b FROM Books b WHERE b.press = :press"), @NamedQuery(name = "Books.findByPrice", query = "SELECT b FROM Books b WHERE b.price = :price")})public class Books implements Serializable {private static final long serialVersionUID = 1L;@Id@Column(name = "ISBN", nullable = false)private String isbn;@Column(name = "TITLE")private String title;@Column(name = "AUTHOR")private String author;@Column(name = "PRESS")private String press;@Column(name = "PRICE")private Double price;public Books() {}public Books(String isbn) {this.isbn = isbn;}public String getIsbn() {return isbn;}public void setIsbn(String isbn) {this.isbn = isbn;}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 getPress() {return press;}public void setPress(String press) {this.press = press;}public Double getPrice() {return price;}public void setPrice(Double price) {this.price = price;}@Overridepublic int hashCode() {int hash = 0;hash += (isbn != null ? isbn.hashCode() : 0);return hash;}@Overridepublic boolean equals(Object object) {// TODO: Warning - this method won't work in the case the id fields are not setif (!(object instanceof Books)) {return false;}Books other = (Books) object;if ((this.isbn == null && other.isbn != null) || (this.isbn != null && !this.isbn.equals(other.isbn))) {return false;}return true;}@Overridepublic String toString() {return "Books.Books[isbn=" + isbn + "]";}}②BooksFacade.javaimport java.util.List;import java.util.ArrayList;import javax.ejb.Stateless;import javax.persistence.EntityManager;import javax.persistence.PersistenceContext;@Statelesspublic class BooksFacade implements BooksFacadeRemote {@PersistenceContextprivate EntityManager em;public void create(Books books) {em.persist(books);}public void edit(Books books) {em.merge(books);}public void remove(Books books) {em.remove(em.merge(books));}public Books find(Object id) {return em.find(Books.class, id);}public List<Books> findAll() {return em.createQuery("select object(o) from Books as o").getResultList(); }public ArrayList<Books> search(String key) {List<Books> all = this.findAll();if(all.isEmpty()) {return null;}ArrayList<Books> result = new ArrayList<Books>();for(Books current : all) {if(current.getTitle().toLowerCase().contains(key)||current.getAuthor().toLowerC ase().contains(key)||current.getPress().toLowerCase().contains(key)) {result.add(current);}}return result;}}③BooksFacadeRemote.javaimport java.util.List;import java.util.ArrayList;import javax.ejb.Remote;@Remotepublic interface BooksFacadeRemote {void create(Books books);(2)Cart①Cart.javaimport java.io.Serializable;public class Cart implements Serializable {private String isbn;private String title;private double price;private int number;public Cart(String isbn,String title,double price,int number) { this.isbn = isbn;this.title = title;this.price = price;this.number = number;}public String getIsbn() {return this.isbn;}public String getTitle() {return this.title;}public double getPrice() {return this.price;}public int getNumber() {return this.number;}public void setIsbn(String isbn) {this.isbn = isbn;}public void setTitle(String title) {this.title = title;}public void setPrice(double price) {this.price = price;}public void setNumber(int number) {this.number = number;}}②CartBean.javaimport javax.ejb.Stateful;import java.util.ArrayList;@Statefulpublic class CartBean implements CartRemote {private ArrayList<Cart> record;private boolean initialed = false;public void add(String isbn,String title,double price){ record.add(new Cart(isbn,title,price,1));}public void set(String isbn,int number){for(Cart current : record) {if(current.getIsbn().equals(isbn)) {current.setNumber(number);}}}public boolean exist(String isbn) {for(Cart current : record) {if(current.getIsbn().equals(isbn)) {return true;}}return false;}public ArrayList<Cart> get(){ArrayList<Cart> result = new ArrayList<Cart>();for(Cart current : record){ result.add(newCart(current.getIsbn(),current.getTitle(),current.getPrice() ,current.getNumber()));}return result;}public boolean initialed() {return this.initialed;}public void create(){record = new ArrayList<Cart>();initialed = true;}}void edit(Books books);void remove(Books books);Books find(Object id);List<Books> findAll();ArrayList<Books> search(String key);}③CartRemote.javaimport javax.ejb.Remote;import java.util.ArrayList;@Remotepublic interface CartRemote {public void add(String isbn,String name,double price);public void set(String isbn,int number);public boolean exist(String isbn);public ArrayList<Cart> get();public boolean initialed();public void create();}2、BooksOnline-warControlServlet.javaimport Books.BooksFacadeRemote;import Cart.CartRemote;import java.util.Enumeration;import java.io.IOException;import java.io.PrintWriter;import javax.ejb.EJB;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class ControlServlet extends HttpServlet {@EJBprivate CartRemote cartBean;@EJBprivate BooksFacadeRemote booksFacade;/*** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.* @param request servlet request* @param response servlet response*/protected void processRequest(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html;charset=UTF-8");request.setCharacterEncoding("UTF-8");PrintWriter out = response.getWriter();try {if(request.getParameter("page").equals("search")) {request.getSession().setAttribute("directory",booksFacade.search(request.getParameter("key").toLowerCase()));response.sendRedirect("Directory.jsp");}if(request.getParameter("page").equals("directory")&&request.getParameter("su bmit").equals("cart")) {if(!cartBean.initialed()) {cartBean.create();}Enumeration names = request.getParameterNames();while(names.hasMoreElements()) {String name = (String)names.nextElement();if(!name.equals("page")&&!name.equals("submit")) {if(!cartBean.exist(request.getParameter(name))) {cartBean.add(request.getParameter(name),booksFacade.find(request.getParameter (name)).getTitle(),booksFacade.find(request.getParameter(name)).getPrice()); }}}request.getSession().setAttribute("cart",cartBean.get());response.sendRedirect("Cart.jsp");}if(request.getParameter("page").equals("directory")&&request.getParameter("su bmit").equals("search")) {response.sendRedirect("Search.jsp");}if(request.getParameter("page").equals("cart")&&request.getParameter("submit" ).equals("order")) {double sum = 0;Enumeration names = request.getParameterNames();while(names.hasMoreElements()) {String name = (String)names.nextElement();if(!name.equals("page")&&!name.equals("submit")&&!name.equals("address")) {cartBean.set(name,Integer.parseInt(request.getParameter(name)));sum +=booksFacade.find(name).getPrice()*Integer.parseInt(request.getParameter(name) );}}request.getSession().setAttribute("cart",cartBean.get());request.getSession().setAttribute("fee",sum);request.getSession().setAttribute("address",request.getParameter("address")); response.sendRedirect("Order.jsp");}if(request.getParameter("page").equals("cart")&&request.getParameter("submit" ).equals("continue")) {response.sendRedirect("Directory.jsp");}if(request.getParameter("page").equals("cart")&&request.getParameter("submit" ).equals("search")) {response.sendRedirect("Search.jsp");}} finally {out.close();}}// <editor-fold defaultstate="collapsed" desc="HttpServlet 方法。