博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
用session实现简单的购物
阅读量:5260 次
发布时间:2019-06-14

本文共 5726 字,大约阅读时间需要 19 分钟。

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("本网站有如下商品:
");25 Map
map = Db.getAll();26 for (Map.Entry
entry : map.entrySet()) {27 Book book = entry.getValue();28 out.print(book.getName()+"
购买
");29 }30 31 }32 33 public void doPost(HttpServletRequest request, HttpServletResponse response)34 throws ServletException, IOException {35 36 37 }38 39 }
View Code
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
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("您购买了如下商品:
");32 List
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
1 package cn.itcast; 2  3 import java.util.LinkedHashMap; 4 import java.util.Map; 5  6 public class Db { 7     private static Map
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
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

 

转载于:https://www.cnblogs.com/aineko/p/3834771.html

你可能感兴趣的文章
转获取sql维护的表关系
查看>>
网络基础——TCP/IP五层模型
查看>>
Java 序列化
查看>>
Java 时间处理实例
查看>>
Java 多线程编程
查看>>
Java 文件操作
查看>>
Java 数组实例
查看>>
Java 方法实例
查看>>
Java 异常处理
查看>>
Java 目录
查看>>
Java 数据结构
查看>>
MYSQL5.7.24编译安装
查看>>
mysql启动过程
查看>>
应用Python来计算排列中的逆序数个数
查看>>
2017前端面试题总结
查看>>
Http GetPost网络请求
查看>>
SWIFT国际资金清算系统
查看>>
Sping注解:注解和含义
查看>>
站立会议第四天
查看>>
如何快速掌握一门技术
查看>>