《最新JSP网上书店系统实验报告.doc》由会员分享,可在线阅读,更多相关《最新JSP网上书店系统实验报告.doc(47页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、Four short words sum up what has lifted most successful individuals above the crowd: a little bit more.-author-dateJSP网上书店系统实验报告浙江工业大学之江学院浙江工业大学之江学院Java网站架构技术大型实验报告班 级: 软件801 姓 名: * 学 号: * 指导老师: * 2011年12月22日网上书店系统设计1、实验目的1 运用以前学习的Ajax+JSP+JavaBean系统的开发方法完成网上购物系统架构设计和代码编写。2 掌握需求分析、文档编写和数据库设计等系统开发步骤。
2、3 掌握系统测试方法。2内容设计1功能描述分为前端和后端,前端主要实现:(1) 能够购买,具有购物车功能/(2) 结帐(3) 用户注册(4) 具有查询功能后端主要实现:(1) 图书的修改,删除,添加(2) 图书分类(3) 用户权限分配2数据库表(数据字典),带表头(1) 用户表user_info数据项数据类型数据长度备注idInt11用户编号usernameVarchar14用户名passwordvarchar22密码super_idint11是否超级用户,1为是(2) 图书表book_info数据项数据类型数据长度备注idInt11书籍编号nameVarchar80书籍名称authorvar
3、char20作者priceint4单价photovarchar40存放封面地址Type_idInt11区别图书类型describevarchar250图书描述(3) 购物车表shop_bus数据项数据类型数据长度备注idInt11物品编号book_nameVarchar80书籍名pricevarchar5单价allpriceint6总价(4) 订单表list_info数据项数据类型数据长度备注idInt11订单编号all_priceInt14总价all_booksvarchar500所有书籍名称user_idint11订单所属ID3详细实现1运行环境:MyEclipse,TomCat6.02数
4、据库连接:mySQL3主要页面,及页面功能描述登录页面:登录页面含有用户的登录功能,如果用户未注册过,还可以通过注册功能注册为会员,然后再登录该系统进入首页,同样,为了方便用户重新输入用户名与密码,还设置了用户名与密码重置的功能。如图1:图1 登录界面主要的登录代码如下:protected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException HttpSession session = req.getSession();UserInfo user = ne
5、w UserInfo();user.setUsername(req.getParameter(username);user.setPassword(req.getParameter(password);UserInfoDao userInfoDao = new UserInfoDaoImp();BookInfoDao bookInfoDao = new BookInfoDaoImp();if (userInfoDao.login(user) Page page = new Page();page.setPageSize(12);page.setNowPage(1);if (bookInfoDa
6、o.allCount() % 12 = 0) page.setAllPage(bookInfoDao.allCount() / 12); else page.setAllPage(bookInfoDao.allCount() / 12 + 1);List list = new ArrayList();list = bookInfoDao.getAllBooks(page);page.goTo();ShopBusDao shopBusDao = new ShopBusDaoImp();shopBusDao.delAll();user = userInfoDao.findByUsername(us
7、er);session.setAttribute(page, page);session.setAttribute(bookList, list);session.setAttribute(user, user);resp.sendRedirect(main.jsp); else req.setAttribute(error, 用户名或密码错误!);ServletContext sc = getServletContext();RequestDispatcher rd = null;rd = sc.getRequestDispatcher(/index.jsp);rd.forward(req,
8、 resp);注册页面:注册页面可以让需要注册的玩家填写注册信息,同意具有重置功能,如图2:图2 注册页面注册的的代码如下:protected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException UserInfo user = new UserInfo();user.setUsername(req.getParameter(username);user.setPassword(req.getParameter(password);UserInfoDao
9、userInfoDao = new UserInfoDaoImp();if (userInfoDao.reg(user) userInfoDao.addUserInfo(user);resp.sendRedirect(success.jsp); else req.setAttribute(error, 用户名已存在!);ServletContext sc=getServletContext();RequestDispatcher rd=null;rd=sc.getRequestDispatcher(/reg.jsp);rd.forward(req, resp);查询功能:查询的功能主要是通过选
10、择查询条件书名或者作者,然后按照输入的关键字进行查询如图3和图4: 图3 查询功能 图4查询所得图书列表主要代码如下:protected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException BookInfoDao bookInfoDao=new BookInfoDaoImp();Listlist=bookInfoDao.getBooksByKey(req.getParameter(keyWord);HttpSession session=req.getS
11、ession();session.setAttribute(listBookByKey, list);ServletContext sc = getServletContext();RequestDispatcher rd = null;rd = sc.getRequestDispatcher(/lookBooks.jsp);rd.forward(req, resp);通过关键字查找数据库的代码如下:public List getBooksByKey(String key) List list = new ArrayList();con = DBCon.GetConnectionMysql()
12、;String sql = SELECT * FROM book_info WHERE name LIKE %+key+%;PreparedStatement stmt = null;ResultSet rs = null;try stmt = con.prepareStatement(sql);/stmt.setString(1, key);rs = stmt.executeQuery();while (rs.next() BookInfo bk = new BookInfo();bk.setId(rs.getInt(1);bk.setName(rs.getString(2);bk.setA
13、uthor(rs.getString(3);bk.setPrice(rs.getInt(4);bk.setPhoto(rs.getString(5);bk.setTypeId(rs.getInt(6);bk.setDescribe(rs.getString(7);list.add(bk);CloseSql.CloseDB(rs, stmt, con); catch (SQLException e) e.printStackTrace();return list;购物车功能:购物车功能可以让会员把自己选择的书籍放入购物车,方便会员一次性下单也方便会员查看以选择的书籍,同时帮会员计算所需支付的总价
14、,如图54:图5 购物车显示以已放入购物车中的主要代码如下:public ShopBus getLast() con=DBCon.GetConnectionMysql();ResultSet rs=null;PreparedStatement stmt=null;String sql=select * from shop_bus;ShopBus shopBus=new ShopBus();try stmt=con.prepareStatement(sql);rs=stmt.executeQuery();while(rs.next()shopBus.setId(rs.getInt(1);shop
15、Bus.setBookName(rs.getString(2);shopBus.setPrice(rs.getInt(3);shopBus.setAllPrice(rs.getInt(4); catch (SQLException e) e.printStackTrace();return shopBus;用于存放当前所购物车中的书籍信息,对数据库表shop_bus进行插入书籍信息的代码如下:public boolean add(ShopBus shopBus) boolean flag = false;con = DBCon.GetConnectionMysql();PreparedStat
16、ement stmt = null;String sql = insert into shop_bus (book_name,price,allPrice) values (?,?,?);try stmt = con.prepareStatement(sql);stmt.setString(1, shopBus.getBookName();stmt.setInt(2, shopBus.getPrice();stmt.setInt(3, this.getLast().getAllPrice()+shopBus.getPrice();if (stmt.executeUpdate()=1) flag
17、 = true;CloseSql.CloseDB(stmt, con); catch (SQLException e) e.printStackTrace();return flag;查看订单列表:查看订单列表便于会员查看自己以往所过买的书籍信息,同时该列表具有翻页功能使列表占有平面空间不大,如图6:图6 订单列表当购物车点击生成订单按钮时就已经完成对订单列表的信息增加,主要代码如下:public boolean add(ListInfo listInfo) boolean flag = false;con=DBCon.GetConnectionMysql();PreparedStatemen
18、t stmt = null;String sql = insert into list_info (all_price,all_books,user_id) values (?,?,?);try stmt = con.prepareStatement(sql);stmt.setInt(1, listInfo.getAllPrice();stmt.setString(2, listInfo.getAllBooks();stmt.setInt(3, listInfo.getUserId();if (stmt.executeUpdate() = 1) flag = true;CloseSql.Clo
19、seDB(stmt, con); catch (Exception e) e.printStackTrace();return flag;分页功能及显示列表的代码如下:public List getAll(int nowPage,int id) Page page=new Page();page.setNowPage(nowPage);page.setPageSize(5);if (this.allCount(id) % 5 = 0) page.setAllPage(this.allCount(id) / 5); else page.setAllPage(this.allCount(id) /
20、 5 + 1);page.goTo();Listlist=new ArrayList();if(page.getNowPage()=page.getAllPage()/假如存在当前页con=DBCon.GetConnectionMysql();PreparedStatement stmt = null;ResultSet rs=null;String sql = select * from list_info where user_id = ? limit ?,?;try stmt = con.prepareStatement(sql);stmt.setInt(2, (page.getNowP
21、age() - 1) * page.getPageSize();stmt.setInt(3, page.getNowPage() * page.getPageSize();stmt.setInt(1, id);rs=stmt.executeQuery();while(rs.next()ListInfo listInfo=new ListInfo();listInfo.setId(rs.getInt(1);listInfo.setAllPrice(rs.getInt(2);listInfo.setAllBooks(rs.getString(3);listInfo.setUserId(rs.get
22、Int(4);list.add(listInfo);CloseSql.CloseDB(rs, stmt, con); catch (Exception e) e.printStackTrace();return list;注销功能:方便用户使用完毕,用于退出网页,同时回到登录页面,注销前后如图7和图8: 图7注销前 图8注销后注销的主要代码如下:protected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException HttpSession session
23、 = req.getSession();session.removeAttribute(page);session.removeAttribute(bookList);session.removeAttribute(user);req.setAttribute(error, 注销成功!);ServletContext sc = getServletContext();RequestDispatcher rd = null;rd = sc.getRequestDispatcher(/index.jsp);rd.forward(req, resp);图书信息列表:方便会员查看图书信息,便于选择,和
24、通过点击购买按钮放入购物车,同样为了节省版面具有分页功能,如图9:图9图书信息列表显示图书信息的主要代码如下:public int allCount() int num = 0;con = DBCon.GetConnectionMysql();String sql = SELECT count(*) FROM book_info;PreparedStatement stmt = null;ResultSet rs = null;try stmt = con.prepareStatement(sql);rs = stmt.executeQuery();while (rs.next() num =
25、 rs.getInt(1);CloseSql.CloseDB(rs, stmt, con); catch (SQLException e) e.printStackTrace();return num;分页功能代码如下:public List getAllBooks(Page page) List list = new ArrayList();con = DBCon.GetConnectionMysql();String sql = SELECT * FROM book_info limit ?,?;PreparedStatement stmt = null;ResultSet rs = nu
26、ll;try stmt = con.prepareStatement(sql);stmt.setInt(1, (page.getNowPage() - 1) * page.getPageSize();stmt.setInt(2, page.getNowPage() * page.getPageSize();rs = stmt.executeQuery();while (rs.next() BookInfo bk = new BookInfo();bk.setId(rs.getInt(1);bk.setName(rs.getString(2);bk.setAuthor(rs.getString(
27、3);bk.setPrice(rs.getInt(4);bk.setPhoto(rs.getString(5);bk.setTypeId(rs.getInt(6);bk.setDescribe(rs.getString(7);list.add(bk);CloseSql.CloseDB(rs, stmt, con); catch (SQLException e) e.printStackTrace();return list;4总结通过这次实验,加深了对Ajax+JSP+JavaBean的理解与应用,也把一学期所学的知识都综合应用到一起,并解决了学习中存在的很多疑惑与难点,对实验中的Ajax和JavaBean的应用更熟练更透彻。同样在实验中也发现了自身的许多不足与缺陷相信在接下来的学习中会努力的去解决。-