增加购物车功能

编程入门 行业动态 更新时间:2024-10-26 05:18:14

增加<a href=https://www.elefans.com/category/jswz/34/1770732.html style=购物车功能"/>

增加购物车功能


这个实例没用到数据库,用的map集合

 Book

package com.itheima.entity;public class Book {private String id;private String name;private double price;private String author;
public Book(String id, String name, double price, String author) {super();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 double getPrice() {return price;
}
public void setPrice(double price) {this.price = price;
}
public String getAuthor() {return author;
}
public void setAuthor(String author) {this.author = author;
}
@Override
public String toString() {return "Book [id=" + id + ", name=" + name + ", price=" + price+ ", author=" + author + "]";
}}
DBUtil

package com.itheima.util;import java.util.HashMap;
import java.util.Map;import com.itheima.entity.Book;public class DBUtil {static Map<String,Book>books=new HashMap<String ,Book>();static {books.put("1",new Book("1", "金瓶梅", 20, "王润鑫"));books.put("2",new Book("2", "葵花宝典", 20, "杨成毅"));books.put("3",new Book("3", "九阴真经", 30, "陈光"));books.put("4",new Book("4", "玉女心经", 10, "陈志家"));}//得到所有书public static Map<String ,Book> findAllBook(){return books;}/*** * 根据id查找指定的书* */public static Book findBookById(String id){return books.get(id);  }}

AddCart

package com.itheima.cart;import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;import javax.jms.Session;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;import com.itheima.entity.Book;
import com.itheima.util.DBUtil;public class AddCart extends HttpServlet {
//把界面ShowAllBooksServlet要添加购物车的书放到这里	private Map<String, Book> findAllBook;public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html;charset=utf-8");//根据id得到书String id =request.getParameter("id");Book book = DBUtil.findBookById(id);//得到session对象HttpSession seesion=request.getSession();//从session中取到list(购物车)List<Book> list=(List<Book>)seesion.getAttribute("cart");//刚开始购物车是没有东西的,这个cart的session域也是没有创建的if (list==null) {list=new ArrayList<Book>();//数组为空,new一个数组}list.add(book);//根据id获得书放到数组里;seesion.setAttribute("cart", list);//把list放回到session域中PrintWriter out = response.getWriter();out.print("购买成功,2秒调回到商品列表界面");response.setHeader("refresh", "2;url=ShowAllBooksServlet");}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request, response);}}
ShowCart

package com.itheima.cart;import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import com.itheima.entity.Book;public class ShowCart extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html;charset=utf-8");PrintWriter out = response.getWriter(); out.print("购物车有以下商品:<br/>");//得到session对象List<Book>books=(List<Book>)request.getSession().getAttribute("cart") ;if (books==null) {out.print("你还什么都没呢");response.setHeader("refresh", "2;url=ShowAllBooksServlet");return;//这里不加return 会有错误。还会向下执行,那么book里面没东西,就会出现异常;}for (Book book : books) {out.print(book.getName()+"<br/>");}}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request, response);}}
RemoveCar

package com.itheima.cart;import java.io.IOException;
import java.io.PrintWriter;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class RemoveCar extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html;charset=utf-8");request.getSession().removeAttribute("cart"); PrintWriter out = response.getWriter();out.print("购物车清楚成功,2秒后,返回商品列表首页");response.setHeader("refresh", "2;url=ShowAllBooksServlet");}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request, response);}}








更多推荐

增加购物车功能

本文发布于:2024-02-12 03:28:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1685622.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:购物车   功能

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!