Java Web Session购物商店小实例

  1. package cn.com.shopping;  
  2.   
  3. import java.io.IOException;  
  4. import java.util.ArrayList;  
  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. public class BuyServlet extends HttpServlet {  
  14.     private static final long serialVersionUID = 1L;  
  15.      
  16.     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  17.         String id=request.getParameter("id");  
  18.         Book book=(Book)Db.getAll().get(id);  
  19.         //再加上那个关闭Cookie时session的剞劂方案  
  20.         //阻止session的时候解决方案  
  21.           
  22.         HttpSession session=request.getSession(false);  
  23.         //从session中得到用户的保存所有书的集合(购物车)  
  24.         List list=(List)session.getAttribute("list");  
  25.         if(list==null)  
  26.         {  
  27.             list=new ArrayList();  
  28.             session.setAttribute("list", list);  
  29.         }  
  30.         list.add(book);  
  31.           
  32.         String url=response.encodeRedirectURL("/Session/SessionCountDemo");  
  33.         response.sendRedirect(url);  
  34.     }  
  35.   
  36.       
  37.     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  38.         doGet(request,response);  
  39.     }  
  40.   
  41. }  
  42. package cn.com.shopping;  
  43.   
  44. import java.io.IOException;  
  45. import java.io.PrintWriter;  
  46. import java.util.LinkedHashMap;  
  47. import java.util.Map;  
  48.   
  49. import javax.servlet.ServletException;  
  50. import javax.servlet.http.HttpServlet;  
  51. import javax.servlet.http.HttpServletRequest;  
  52. import javax.servlet.http.HttpServletResponse;  
  53. import javax.servlet.http.HttpSession;  
  54.   
  55. //显示书  
  56. public class ListBookServlet extends HttpServlet {  
  57.     private static final long serialVersionUID = 1L;  
  58.          
  59.       
  60.     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  61.         response.setCharacterEncoding("UTF-8");  
  62.         response.setContentType("text/html;charset=UTF-8");  
  63.         PrintWriter out=response.getWriter();  
  64.           
  65.         HttpSession session=request.getSession();  
  66.           
  67.         out.print("本店有如下的商品:<br/>");  
  68.         Map<String ,Book > map=Db.getAll();  
  69.         for(Map.Entry<String, Book> entry:map.entrySet())  
  70.         {  
  71.             Book book=entry.getValue();  
  72.             String url=response.encodeURL("/Session/BuyServlet?id="+book.getId());  
  73.             out.print(book.getName()+"<a href='"+url+"' target='_blank' >购买</a><br/>");  
  74.         }  
  75.           
  76.     }  
  77.   
  78.     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  79.         doGet(request,response);  
  80.     }  
  81.   
  82. }  
  83. //Db作为数据库  
  84. class Db  
  85. {  
  86.     private static Map<String ,Book> map=new LinkedHashMap();  
  87.     static  
  88.     {  
  89.         map.put("1"new Book("1","Java WEB开发","WY","好书"));  
  90.         map.put("2"new Book("2","WEB开发","zt","一般"));  
  91.         map.put("3"new Book("3","程序设计","df","较好书"));  
  92.         map.put("4"new Book("4","计算机组成","as","一般好书"));  
  93.         map.put("5"new Book("5","编译原理","ty","很好书"));  
  94.         map.put("6"new Book("6","网络维护","hj","非常好书"));  
  95.     }  
  96.       
  97.     public static Map getAll()  
  98.     {  
  99.         return map;  
  100.     }  
  101. }  
  102. //书  
  103. class Book  
  104. {  
  105.     private String id;  
  106.     private String name;  
  107.     private String author;  
  108.     private String description;  
  109.       
  110.     public Book() {  
  111.         super();  
  112.         // TODO Auto-generated constructor stub  
  113.     }  
  114.     public Book(String id, String name, String author, String description) {  
  115.         super();  
  116.         this.id = id;  
  117.         this.name = name;  
  118.         this.author = author;  
  119.         this.description = description;  
  120.     }  
  121.     public String getId() {  
  122.         return id;  
  123.     }  
  124.     public void setId(String id) {  
  125.         this.id = id;  
  126.     }  
  127.     public String getName() {  
  128.         return name;  
  129.     }  
  130.     public void setName(String name) {  
  131.         this.name = name;  
  132.     }  
  133.     public String getAuthor() {  
  134.         return author;  
  135.     }  
  136.     public void setAuthor(String author) {  
  137.         this.author = author;  
  138.     }  
  139.     public String getDescription() {  
  140.         return description;  
  141.     }  
  142.     public void setDescription(String description) {  
  143.         this.description = description;  
  144.     }  
  145.       
  146.       
  147. }  
  148. package cn.com.shopping;  
  149.   
  150. import java.io.IOException;  
  151. import java.io.PrintWriter;  
  152. import java.util.List;  
  153.   
  154. import javax.servlet.ServletException;  
  155. import javax.servlet.http.HttpServlet;  
  156. import javax.servlet.http.HttpServletRequest;  
  157. import javax.servlet.http.HttpServletResponse;  
  158. import javax.servlet.http.HttpSession;  
  159.   
  160.   
  161. public class SessionCountDemo extends HttpServlet {  
  162.     private static final long serialVersionUID = 1L;  
  163.          
  164.      
  165.     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  166.         response.setCharacterEncoding("UTF-8");  
  167.         response.setContentType("text/html;charset=UTF-8");  
  168.         PrintWriter out=response.getWriter();  
  169.         HttpSession session=request.getSession();  
  170.         if(session==null)  
  171.         {  
  172.             out.write("您没买任何的商品!");  
  173.             return;  
  174.         }  
  175.         out.write("您购买了如下的商品:");  
  176.         List<Book> list=(List) session.getAttribute("list");  
  177.         for(Book book:list)  
  178.         {  
  179.             out.write(book.getName());  
  180.         }  
  181.           
  182.     }  
  183.   
  184.       
  185.     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  186.         doGet(request,response);  
  187.     }  
  188.   
  189. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值