Java Web Session购物商店小实例

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

标签