欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

使用session和jsp实现简单购物车

程序员文章站 2022-03-01 18:43:02
...

这里没有连接数据库,用的一个类模仿数据库了
(一)、实体类代码Book.java

public class Book implements Serializable{
	private String id;
	private String name;
	private String price;
	private String author;
	
	public Book() {
		super();
	}
	public Book(String id, String name, String price, String author) {
		this.id = id;
		this.name = name;
		this.price = price;
		this.author = author;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPrice() {
		return price;
	}
	public void setPrice(String price) {
		this.price = price;
	}
	public String getAuthor() {
		return author;
	}
	public void setAuthor(String author) {
		this.author = author;
	}
	//重写hashCode和equals方法,怎样比较是否是同一个对象
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Book other = (Book) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}
	
}

(二)、模仿数据库BookStore.java

public class BookStore {
	static List<Book> booklist;
	static {
		booklist=new ArrayList<>();
		booklist.add(new Book("1", "红楼梦", "78", "曹雪芹"));
		booklist.add(new Book("2", "水浒传", "78", "施耐庵"));
		booklist.add(new Book("3", "三国演义", "78", "罗贯中"));
		booklist.add(new Book("4", "西游记", "78", "吴晨恩"));
	}
	public static List<Book> getBooklist() {
		return booklist;
	}
	public static Book getBookById(String id) {
		Book book=new Book();
		book.setId(id);
		int index=booklist.indexOf(book);
		return booklist.get(index);
	}
}

(三)、前端页面展示book.jsp

<body>
	<table border="1px">
		<caption>书籍列表</caption>
		<%
			List<Book> books=new BookStore().getBooklist();
			for(Book book:books){
		%>
			<tr>
				<td><%=book.getName() %></td>
				<td><%=book.getPrice() %></td>
				<td><%=book.getAuthor() %></td>
				<td><a href="BookServlet?id=<%=book.getId()%>">添加商品到购物车</a></td>
			</tr>
		<%
			}
		%>
	</table>
	<hr>
	<a href="show.jsp">查看购物车商品</a>
</body>

(四)、后台实现BookServlet

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html);charset=utf-8");
		
		//1、获取传过来的商品ID
		String id=request.getParameter("id");
		//2、把对应商品放入购物车
		Map<Book, Integer> cart=(Map<Book, Integer>)request.getSession().getAttribute("cart");
		if(cart==null) {
			cart=new HashMap<Book, Integer>();
		}
		//获取指定id的书籍对象
		Book book=BookStore.getBookById(id);
		//获取图书在购物车里面对应的数量
		Integer count = cart.get(book);
		if (count==null) {
			count=1;	//如果购物车里面没有改图书,数量=1
		}else {
			count+=1;	//如果购物车里面有该图书,数量+1
		}
		//把购物车里面对应的图书数量进行更新
		cart.put(book, count);
		//更新session里面的map集合
		request.getSession().setAttribute("cart", cart);
		
		response.getWriter().write("<a href='book.jsp'>继续购物</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href='show.jsp'>查看购物车</a>");
	}

五、查看购物车前端实现show.jsp

<body>
<table border="1px">
	<caption>购物车详情</caption>
	<tr>
		<td>书名</td>
		<td>价格</td>
		<td>作者</td>
		<td>数量</td>
	</tr>
	<%
		Map<Book, Integer> cart=(Map<Book, Integer>)request.getSession().getAttribute("cart");
		if(cart!=null && cart.size()!=0){
			Set<Book> books=cart.keySet();
			for(Book book:books){
	%>
		<tr>
			<td><%=book.getName() %></td>
			<td><%=book.getPrice() %></td>
			<td><%=book.getAuthor() %></td>
			<td><%=cart.get(book) %></td>
		</tr>
	<%		
			}
		}
	%>
</table >
</body>