JSP网上书店系统实验报告

浙江工业大学之江学院

Java网站架构技术大型实验报告

班    级:       软件801       

姓    名         ***         

           学    号:        ****         

指导老师         ***         

20##年12月22日

网上书店系统设计

1、实验目的

1 运用以前学习的Ajax+JSP+JavaBean系统的开发方法完成网上购物系统架构设计和代码编写。

2 掌握需求分析、文档编写和数据库设计等系统开发步骤。

3 掌握系统测试方法。

2内容设计

1功能描述

分为前端和后端,前端主要实现:

(1) 能够购买,具有购物车功能/

(2) 结帐

(3) 用户注册

(4) 具有查询功能

后端主要实现:

(1) 图书的修改,删除,添加

(2) 图书分类

(3) 用户权限分配

2数据库表(数据字典),带表头

(1) 用户表user_info

(2) 图书表book_info

(3) 购物车表shop_bus

(4) 订单表list_info

3详细实现

1运行环境:MyEclipse,TomCat6.0

2数据库连接:mySQL

3主要页面,及页面功能描述

登录页面:登录页面含有用户的登录功能,如果用户未注册过,还可以通过注册功能注册为会员,然后再登录该系统进入首页,同样,为了方便用户重新输入用户名与密码,还设置了用户名与密码重置的功能。如图1:

图1 登录界面

主要的登录代码如下:

protected void doGet(HttpServletRequest req, HttpServletResponse resp)

                     throws ServletException, IOException {

              HttpSession session = req.getSession();

              UserInfo user = new 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 (bookInfoDao.allCount() % 12 == 0) {

                            page.setAllPage(bookInfoDao.allCount() / 12);

                     } else {

                            page.setAllPage(bookInfoDao.allCount() / 12 + 1);

                     }

                     List<BookInfo> list = new ArrayList<BookInfo>();

                     list = bookInfoDao.getAllBooks(page);

                     page.goTo();

                     ShopBusDao shopBusDao = new ShopBusDaoImp();

                     shopBusDao.delAll();

                     user = userInfoDao.findByUsername(user);

                     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, 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 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);

              }

       }

查询功能:查询的功能主要是通过选择查询条件书名或者作者,然后按照输入的关键字进行查询如图3和图4:

 

      图3 查询功能                       图4查询所得图书列表

主要代码如下:

protected void doGet(HttpServletRequest req, HttpServletResponse resp)

                     throws ServletException, IOException {

              BookInfoDao bookInfoDao=new BookInfoDaoImp();

              List<BookInfo>list=bookInfoDao.getBooksByKey(req.getParameter("keyWord"));

              HttpSession session=req.getSession();

              session.setAttribute("listBookByKey", list);

              ServletContext sc = getServletContext();

              RequestDispatcher rd = null;

              rd = sc.getRequestDispatcher("/lookBooks.jsp");

              rd.forward(req, resp);

       }

通过关键字查找数据库的代码如下:

public List<BookInfo> getBooksByKey(String key) {

              List<BookInfo> list = new ArrayList<BookInfo>();

              con = DBCon.GetConnectionMysql();

              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.setAuthor(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;

       }

购物车功能:购物车功能可以让会员把自己选择的书籍放入购物车,方便会员一次性下单也方便会员查看以选择的书籍,同时帮会员计算所需支付的总价,如图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));

                            shopBus.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();

              PreparedStatement 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 = 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();

              PreparedStatement 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.CloseDB(stmt, con);

              } catch (Exception e) {

                     e.printStackTrace();

              }

              return flag;

       }

分页功能及显示列表的代码如下:

public List<ListInfo> 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) / 5 + 1);

              }

              page.goTo();

              List<ListInfo>list=new ArrayList<ListInfo>();

              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.getNowPage() - 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.getInt(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 = 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);

       }

图书信息列表:方便会员查看图书信息,便于选择,和通过点击购买按钮放入购物车,同样为了节省版面具有分页功能,如图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 = rs.getInt(1);

                     }

                     CloseSql.CloseDB(rs, stmt, con);

              } catch (SQLException e) {

                     e.printStackTrace();

              }

              return num;

       }

分页功能代码如下:

public List<BookInfo> getAllBooks(Page page) {

              List<BookInfo> list = new ArrayList<BookInfo>();

              con = DBCon.GetConnectionMysql();

              String sql = "SELECT * FROM book_info limit ?,?";

              PreparedStatement stmt = null;

              ResultSet rs = null;

              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(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的应用更熟练更透彻。同样在实验中也发现了自身的许多不足与缺陷相信在接下来的学习中会努力的去解决。

相关推荐