session: 简单购物车

来源:互联网 发布:nba2k17欧文捏脸数据 编辑:程序博客网 时间:2024/05/04 23:11
session:简单购物车


**********************************************************
// 注:  session中的对象(如:Book) 一般都要实现Serializable接口
public class Book implements Serializable{
        private String id;
        private String name;
        private String author;
        private float  price;
        private String description;        
        public Book(){}
        public Book(String id, String name, String author, float price,
            String description) {
                super();
                this.id = id;
                this.name = name;
                this.author = author;
                this.price = price;
                this.description = description;
        }
        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 getAuthor() {
                return author;
        }
        public void setAuthor(String author) {
                this.author = author;
        }
        public float getPrice() {
                return price;
        }
        public void setPrice(float price) {
                this.price = price;
        }
        public String getDescription() {
                return description;
        }
        public void setDescription(String description) {
                this.description = description;
        }
        @Override
        public String toString() {
                return "Book [author=" + author + ", description=" + description
                    + ", id=" + id + ", name=" + name + ", price=" + price + "]";
        }
}
**********************************************************
public class BookDB {
        // key: 书的id   value: id对应的书对象
        private static Map<String, Book> books = new LinkedHashMap<String, Book>();
        static{
                books.put("1", new Book("1", "葵花宝典", "葛付以", 5.00f, "欲练此功));
                books.put("2", new Book("2", "玉女心经", "朱巧玲", 8.00f, "欲练此功));
                books.put("3", new Book("3", "辟邪剑法", "邹海洋", 5.00f, "欲练此功));
                books.put("4", new Book("4", "金瓶梅", "刘建平", 15.00f, "古代爱"));
                books.put("5", new Book("5", "红楼梦", "曹雪芹", 105.00f, "古代"));
        }        
        public static Book findBookById(String bookId){
                return books.get(bookId);   // map集合的方法
        }       
        public static Map<String,Book> findAllBooks(){
                return books;
        }
}
**********************************************************
// 显示所有商品
// 提供购买链接
// 提供查看购物车的链接
public class ShowAllBooksServlet extends HttpServlet {


        public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
                response.setCharacterEncoding("UTF-8");
                response.setContentType("text/html;charset=UTF-8");
                PrintWriter out = response.getWriter();
                
                // 显示所有的商品  并提供购买链接   
                out.write("<h1>本站有以下商品</h1>");
                Map<String,Book> books = BookDB.findAllBooks();
                for(Map.Entry<String, Book> me : books.entrySet()){
                        out.write(me.getValue().getName() + "&nbsp;&nbsp;
                          <a href='/day08/servlet/BuyServlet?id="+ me.getKey() +"'>
                          购买</a><br/>");
                }
                
                // 提供查看购物车的链接
                out.write("<a href='/day08/servlet/ShowCartServlet'>查看购物车</a>");
        }


        public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
                doGet(request, response);
        }
}
********************************************************** 
// 完成购物,提供继续购物的超级链接
public class BuyServlet extends HttpServlet {


        public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
                response.setCharacterEncoding("UTF-8");
                response.setContentType("text/html;charset=UTF-8");
                PrintWriter out = response.getWriter();
                
                String id = request.getParameter("id");
                Book book = BookDB.findBookById(id);
                
                // 搞一个购物车:先查看有无购物车,若没有,则创建一个,若有,则直接使用
                HttpSession session = request.getSession();
                List<Book> cart = (List<Book>) session.getAttribute("cart");
                if(cart == null){
                        cart = new ArrayList<Book>();
                        session.setAttribute("cart", cart);
                }
                cart.add(book);
                out.write(book.getName() + "已经放入您的购物车<br/>");
                
                out.write("<a href='/day08/servlet/ShowAllBooksServlet'>继续购物</a>");
                
                // 若想关闭浏览器之后再次访问时,购物车的信息还在,
                // 则须自己写cookie,根据JSESSIONID覆盖session中的cookie
                Cookie c = new Cookie("JSESSIONID",session.getId());
                c.setPath(request.getContextPath());
                c.setMaxAge(Integer.MAX_VALUE);   
                response.addCookie(c);
        }


        public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
                doGet(request, response);
        }
}
**********************************************************
public class ShowCartServlet extends HttpServlet {


        public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
                response.setCharacterEncoding("UTF-8");
                response.setContentType("text/html;charset=UTF-8");
                PrintWriter out = response.getWriter();
                
                HttpSession session = request.getSession(false);  // 只查询       
                if(session == null){
                         // 没购物车
                        out.write("对不起,您还未曾购物!");  
                }else{
                        List<Book> cart = (List<Book>) session.getAttribute("cart");
                        if(cart == null){
                                // 有购物车,但没购物
                                out.write("对不起,您还未曾购物!");  
                        }else{
                                out.write("您购买的商品如下:<br/> ");
                                for(Book b : cart){
                                        out.write(b.getName() + "<br/>");
                                }
                        }
                }
                out.write("<a href='/day08/servlet/ShowAllBooksServlet'>继续购物</a>");
        }


        public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
                doGet(request, response);
        }
}
**********************************************************



















0 0
原创粉丝点击