码迷,mamicode.com
首页 > 其他好文 > 详细

用session实现简单的购物

时间:2014-07-10 17:08:45      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   java   color   

bubuko.com,布布扣
 1 package cn.itcast.shopping;
 2 
 3 import java.io.IOException;
 4 import java.io.PrintWriter;
 5 import java.util.Map;
 6 
 7 import javax.servlet.ServletException;
 8 import javax.servlet.http.HttpServlet;
 9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11 
12 import cn.itcast.Book;
13 import cn.itcast.Db;
14 
15 public class ListBookServlet extends HttpServlet {
16 
17     public void doGet(HttpServletRequest request, HttpServletResponse response)
18             throws ServletException, IOException {
19 
20         response.setCharacterEncoding("UTF-8");
21         response.setContentType("text/html;charset=UTF-8");
22         PrintWriter out = response.getWriter();
23         
24         out.write("本网站有如下商品:<br/>");
25         Map<String,Book> map = Db.getAll();
26         for (Map.Entry<String, Book> entry : map.entrySet()) {
27             Book book = entry.getValue();
28             out.print(book.getName()+"<a href=‘/ServletDemo/servlet/BuyServlet?id="+book.getId()+"‘ target=‘_blank‘>购买</a><br/>");
29         }
30         
31     }
32 
33     public void doPost(HttpServletRequest request, HttpServletResponse response)
34             throws ServletException, IOException {
35 
36         
37     }
38 
39 }
View Code
bubuko.com,布布扣
 1 package cn.itcast.shopping;
 2 
 3 import java.io.IOException;
 4 import java.io.PrintWriter;
 5 import java.util.ArrayList;
 6 import java.util.List;
 7 
 8 import javax.servlet.ServletException;
 9 import javax.servlet.http.HttpServlet;
10 import javax.servlet.http.HttpServletRequest;
11 import javax.servlet.http.HttpServletResponse;
12 import javax.servlet.http.HttpSession;
13 
14 import cn.itcast.Book;
15 import cn.itcast.Db;
16 
17 //完成购买
18 public class BuyServlet extends HttpServlet {
19 
20     
21     public void doGet(HttpServletRequest request, HttpServletResponse response)
22             throws ServletException, IOException {
23 
24          String id = request.getParameter("id");
25          Book book = (Book) Db.getAll().get(id);
26         
27          HttpSession session =request.getSession();
28          
29          //从Session得到用户名用于保存所有书的集合(购物车)
30          List list = (List) session.getAttribute("list");
31          if(list==null){
32              list = new ArrayList();
33              session.setAttribute("list", list);
34          }
35          list.add(book);
36          
37         // request.getRequestDispatcher("/servlet/ListCarServlet").forward(request, response);
38          response.sendRedirect(request.getContextPath()+"/servlet/ListCarServlet");
39     }
40 
41     public void doPost(HttpServletRequest request, HttpServletResponse response)
42             throws ServletException, IOException {
43 
44     }
45 
46 }
View Code
bubuko.com,布布扣
 1 package cn.itcast.shopping;
 2 
 3 import java.io.IOException;
 4 import java.io.PrintWriter;
 5 import java.util.List;
 6 
 7 import javax.servlet.ServletException;
 8 import javax.servlet.http.HttpServlet;
 9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11 import javax.servlet.http.HttpSession;
12 
13 import cn.itcast.Book;
14 
15 //显示用户购买商品
16 public class ListCarServlet extends HttpServlet {
17 
18     public void doGet(HttpServletRequest request, HttpServletResponse response)
19             throws ServletException, IOException {
20         
21         response.setCharacterEncoding("UTF-8");
22         response.setContentType("text/html;charset=UTF-8");
23         PrintWriter out = response.getWriter();
24 
25         HttpSession session = request.getSession(false);
26         if(session==null){
27             out.write("您没有购买任何商品!!");
28             return;
29         }
30         
31         out.write("您购买了如下商品:<br/>");
32         List<Book> list = (List) session.getAttribute("list");
33         for(Book book : list){
34             out.write(book.getName());
35         }
36     }
37 
38     
39     public void doPost(HttpServletRequest request, HttpServletResponse response)
40             throws ServletException, IOException {
41 
42         
43     }
44 
45 }
View Code
bubuko.com,布布扣
 1 package cn.itcast;
 2 
 3 import java.util.LinkedHashMap;
 4 import java.util.Map;
 5 
 6 public class Db {
 7     private static Map<String, Book> map = new LinkedHashMap();
 8     
 9     static{
10         map.put("1", new Book("1","javaweb开发","Zero","一本好书!!"));
11         map.put("2", new Book("2","jdbc开发","one","一本好书!!"));
12         map.put("3", new Book("3","spring开发","two","一本好书!!"));
13         map.put("4", new Book("4","struks开发","three","一本好书!!"));
14         map.put("5", new Book("5","hibernate开发","four","一本好书!!"));
15     }
16     
17     public static Map getAll(){
18         return map;
19     }
20 }
View Code
bubuko.com,布布扣
 1 package cn.itcast;
 2 
 3 import java.io.Serializable;
 4 
 5 
 6 public class Book implements Serializable {
 7     private String id;
 8     private String name;
 9     private String author;
10     private String description;
11     
12     public Book(String id, String name, String author, String description) {
13         super();
14         this.id = id;
15         this.name = name;
16         this.author = author;
17         this.description = description;
18     }
19 
20     public String getId() {
21         return id;
22     }
23 
24     public void setId(String id) {
25         this.id = id;
26     }
27 
28     public String getName() {
29         return name;
30     }
31 
32     public void setName(String name) {
33         this.name = name;
34     }
35 
36     public String getAuthor() {
37         return author;
38     }
39 
40     public void setAuthor(String author) {
41         this.author = author;
42     }
43 
44     public String getDescription() {
45         return description;
46     }
47 
48     public void setDescription(String description) {
49         this.description = description;
50     }
51 }
View Code

 

用session实现简单的购物,布布扣,bubuko.com

用session实现简单的购物

标签:des   style   blog   http   java   color   

原文地址:http://www.cnblogs.com/aineko/p/3834771.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!