博客
关于我
强烈建议你试试无所不能的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

你可能感兴趣的文章
MySQL数据库远程连接的配置方案
查看>>
Spring注解设置需要扫描的包
查看>>
Discuz建站教程:本地安装discuz网站
查看>>
【转载】.NET/C#-uploadify视频文件or大文件上传
查看>>
daemon框架
查看>>
偶入博客园
查看>>
栈和队列
查看>>
人脸识别的数据库汇总
查看>>
64位版本为什么叫amd64,而不是intel64
查看>>
MySQL 可以用localhost 连接,但不能用IP连接的问题,局域网192.168.*.* 无法连接mysql...
查看>>
JAVA8 ARRAY、LIST操作 汇【5】)- JAVA8 LAMBDA LIST统计(求和、最大、最小、平均)...
查看>>
bzoj1135:[POI2009]Lyz
查看>>
Linux0.11内核--系统调用机制分析
查看>>
Linux0.11内核--进程调度分析之2.调度
查看>>
iOS6 与iOS7以及7以上状态栏的颜色设置
查看>>
一个点击事件引发的案件
查看>>
Android.mk介绍
查看>>
【Demo】动态库创建示例
查看>>
The 2014 ACMICPC Asia Regional Xian Online
查看>>
oracle 触发器
查看>>