当前位置:文档之家› 购物车

购物车

实验四基于MVC模式的网上教材订购(第18章实验指导4)需求分析:基于MVC模式,制作一个购物程序,让学生可以在网页上订购教材。

该系统由三个界面组成。

购物车

购物车

购物车

1、准备数据

此处使用MySQL 5.5 厂商驱动,

数据库名:SCHOOL

用户名为:sa

密码:limei6875

数据表:

包含图书编号、图书名称和图书价格

创建表的脚本如下:

CREATE TABLE t_book(

bookno varchar(40) NOT NULL,

bookname varchar(40) default NULL,

bookprice float default NULL

)

插入一些数据.

2、编写DBOPer数据库操作类(基础类)

略(同前面的项目)

3、编写过滤器,解决中文处理问题。

(同上一个实验)

4、在DBOPer的基础上编写DAO和VO

本项目中,应该在DAO中查询所有的图书,图书的信息用VO封装

(1)编写BookDao.java

package dao;

public class BookDao {

private Connection conn=null;

//返回所有的图书

public HashMap getAllBook() throws Exception{

HashMap hm=new HashMap();

String sql="select BOOKNO,BOOKNAME,BOOKPRICE FROM T_BOOK";

ResultSet rs=this.executeQuery(sql,null);

while(rs.next()){

// 创建VO对象

Book book=new Book();

// 将查询到的结果保存到VO对象中

book.setBookno(rs.getString("bookno"));

// String name=new String(rs.getString("bookname").getBytes("ISO-8859-1"),"gb2312"); book.setBookname(rs.getString("bookname"));

book.setBookprice(rs.getFloat("bookprice"));

// 将VO对象(键值对)放入HashMap中

hm.put(book.getBookno(),book);

}

return hm; }

}

----------------------------------------------------------------------------- (2)编写VO:Book.java

----------------------------------------------------------------------------- package vo;

public class Book implements Serializable {

private String bookno;

private String bookname;

private double bookprice;

private int booknumber;

//略去setter/getter方法

}

----------------------------------------------------------------------------- (3)编写一个测试类,测试Dao中的方法(用来测试该方法的正确性)

TestDao.java

----------------------------------------------------

package dao;

public class TestDao {

public static void main(String[] args) throws Exception {

HashMap books=new HashMap();

BookDao dao=new BookDao();

books=dao.getAllBook();

Set set=books.keySet();

Iterator ite=set.iterator();

System.out.println("书号书名价格");

while(ite.hasNext()){

String bookno=(String)ite.next();

Book book=(Book)books.get(bookno);

System.out.println( book.getBookno()+book.getBookname()+" "+book.getBookprice());

}

}

}

-------------------------------------------------------------------------------

运行Java类:检验是否能从数据库中读出所有图书(不用启动服务器,直接运行Java代码即可)

3 、编写SessionListener.java

SessionListener.java是一个监听器,负责对session的内容进行初始化。

SessionListener.java

-----------------------------------------------------------------------------

package listener;

public class SessionListener implements HttpSessionListener {

public void sessionCreated(HttpSessionEvent se) {

HttpSession session=se.getSession();

// 初始化购物车

HashMap books=new HashMap();

session.setAttribute("books", books);

// 初始化总钱数

session.setAttribute("money", 0.00);

System.out.println("创建了session");

}

public void sessionDestroyed(HttpSessionEvent se) {

}

}

-----------------------------------------------------------------------------

配置监听器:

web.xml

-----------------------------------------------------------------

listener.SessionListener

-----------------------------------------------------------------

4 、编写InitServlet.java和showAllBook.jsp

用户首先访问的是InitServlet,负责查询所有的图书,然后跳转到showAllBook.jsp

InitServlet.java

-----------------------------------------------------------------------------

package servlet;

public class InitServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

doPost(request,response);

}

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

BookDao bookDao=new BookDao();

HashMap allbook=null;

try {

allbook=bookDao.getAllBook();

} catch (Exception e) {

e.printStackTrace();

}

// 将所有图书放入session中

HttpSession session=request.getSession();

session.setAttribute("allbook", allbook);

// 跳转到显示所有图书界面

response.sendRedirect("/ProjWeb18/showAllBook.jsp");

}

}

------------------------------------------------------------------------------------ showAllBook.jsp

------------------------------------------------------------------------

<%@ page language="java" import="java.util.*,vo.Book" pageEncoding="gb2312"%>

欢迎选购图书

<%

HashMap allbook=(HashMap)session.getAttribute("allbook");

%>

<%

//将HashMap中的键取出翻入Set集中(Set集中的元素不可重复)

Set set=allbook.keySet();

//创建Iterator对象

Iterator ite=set.iterator();

while(ite.hasNext()){

String bookno=(String)ite.next();//取出书号

Book book=(Book)allbook.get(bookno);//取出该书号对应的图书对象%>

<%

}

%>

书本名称书本价格购买
<%=book.getBookname() %><%=book.getBookprice() %>购买

查看购物车

--------------------------------------------------------------------------

5、编写buyForm.jsp和AddServlet.java

---------------------------------------------------------------

buyform.jsp负责显示买书表单

<%@ page language="java" import="java.util.*,vo.Book" pageEncoding="UTF-8"%>

购买图书

<%

String bookno=request.getParameter("bookno");

HashMap allbook=(HashMap)session.getAttribute("allbook");//取出所有的图书键值对Book book=(Book)allbook.get(bookno);//取出该书号对应的图书对象

%>

欢迎购买:<%=book.getBookname() %>

书本价格:<%=book.getBookprice() %>

数量:

----------------------------------------------------------------------------------

表单提交到AddServlet,代码中用到了隐藏表单:

AddServlet.java

---------------------------------------------------------

package servlet;

public class AddServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

doPost(request,response);

}

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

request.setCharacterEncoding("UTF-8");

HttpSession session=request.getSession();

HashMap books=(HashMap)session.getAttribute("books");

// 获取提交的内容

String bookno=request.getParameter("bookno");

String bookname=request.getParameter("bookname");

String strbookprice=request.getParameter("bookprice");

String strbooknumber=request.getParameter("booknumber");

// 存入购物车

Book book=new Book();

book.setBookno(bookno);

book.setBookname(bookname);

//将读取的价格转为双精度实数

double bookprice=Double.parseDouble(strbookprice);

book.setBookprice(bookprice);

//将读取的图书数量转为整型数据

int booknumber=Integer.parseInt(strbooknumber);

book.setBooknumber(booknumber);

books.put(bookno, book);

// 总钱数增加

double money=(Double)session.getAttribute("money");

money=money+bookprice*booknumber;

session.setAttribute("money", money);

response.sendRedirect("/ProjWeb18/showCart.jsp");

}

}

--------------------------------------------------------------------

6、编写showCart.jsp和RemoveServlet .java

showCart.jsp负责显示购物车的内容。

showCart.jsp

----------------------------------------------------------------------------------

<%@ page language="java" import="java.util.*,vo.Book" pageEncoding="UTF-8"%>

显示购物车

<%

HashMap books=(HashMap)session.getAttribute("books");//从session中取出所有的键值对Set set=books.keySet();//取出HashMap中的键的集合,存入Set集中(该集中的元素不能重复)Iterator ite=set.iterator();//创建迭代器,用来遍历Set集中的所有元素

while(ite.hasNext()){

String bookno=(String)ite.next();//取出书号

Book book=(Book)books.get(bookno);//取出对应于该书号的图书对象

%>

<%

}

%>

书本名称书本价格数量删除
<%=book.getBookname() %><%=book.getBookprice() %><%=book.getBooknumber() %>删除

现金总额:<%=session.getAttribute("money") %>

继续购买

---------------------------------------------------------------------------------- RemoveServlet.java

----------------------------------------------------------------------------------

package servlet;

public class RemoveServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

doPost(request,response);

}

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

String bookno=request.getParameter("bookno");

HttpSession session=request.getSession();

HashMap books=(HashMap)session.getAttribute("books");

Book book=(Book)books.get(bookno);

float money=(Float)session.getAttribute("money");

money=money-book.getBooknumber()*book.getBookprice();

session.setAttribute("money", money);

books.remove(bookno);

response.sendRedirect("/ProjWeb18/showCart.jsp");

}

}

完成以上实验,并提交实验截图

下载文档原格式(Word原格式,共13页)
相关文档
  • 购物车系统

  • 自助购物车

  • 购物车功能

  • 购物车的实现

  • 网上购物车

相关文档推荐: