Session小案例-----简单购物车的使用
同上篇一样,这里的处理请求和页面显示同样用的都是servlet.
功能实现如下:
1,显示网站的所有商品
2,用户点击购买后,能够记住用户选择的商品
3,实现了多个会话共享一个session
4, 实现了浏览器禁用cookie后数据共享问题的处理
首页:
package cn.itcast.shopping; import java.io.IOException; import java.io.PrintWriter; import java.io.Serializable; import java.util.HashMap; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; //首页,显示网站所有商品 public class IndexServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); response.setCharacterEncoding("UTF-8"); PrintWriter out = response.getWriter(); //为了用户第一次访问 request.getSession(); //1.显示网站所有商品 out.print("本网站有如下书籍:<br/>"); Map<String,Book> map = DB.getMap(); for(Map.Entry<String, Book> entry : map.entrySet()){ Book book = entry.getValue(); String url = "/day07/servlet/BuyServlet?id="+book.getId(); url = response.encodeURL(url); //得到重写后的url out.print(book.getName() + "<a href='"+url+"'>购买</a><br/>"); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } class DB{ private static Map<String,Book> map = new HashMap(); static{ map.put("1", new Book("1","javaweb开发","老张")); map.put("2", new Book("2","jdbc开发","老黎")); map.put("3", new Book("3","struts2开发","老张")); map.put("4", new Book("4","spring开发","老黎")); map.put("5", new Book("5","hibernate开发","老张")); } public static Map getMap(){ return map; } } class Book implements Serializable{ private String id; private String name; private String author; public Book() { super(); // TODO Auto-generated constructor stub } public Book(String id, String name, String author) { super(); this.id = id; this.name = name; this.author = author; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } }
购买页面:package cn.itcast.shopping; import java.io.IOException; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; //购买 public class BuyServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1.得到用户想买的书 String id = request.getParameter("id"); Book book = (Book) DB.getMap().get(id); //2.把用户买的书加在session中用于管理所有书的集合中了 HttpSession session = request.getSession(); //覆盖服务器的JSESSIONID的cookie Cookie cookie = new Cookie("JSESSIONID",session.getId()); cookie.setMaxAge(30*60); cookie.setPath("/day07"); response.addCookie(cookie); List list = (List) session.getAttribute("list"); if(list==null){ list = new ArrayList(); session.setAttribute("list", list); } list.add(book); //3.跳转到购物车显示列表 //request.getRequestDispatcher("/servlet/ListCartServlet").forward(request, response); //自动带上session的ID号 String url = response.encodeRedirectURL("/day07/servlet/ListCartServlet"); //实现 url重写 response.sendRedirect(url); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
购物车列表显示页面:package cn.itcast.shopping; import java.io.IOException; import java.io.PrintWriter; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; //显示用户买的所有书(购物车列表显示页面) public class ListCartServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); response.setCharacterEncoding("UTF-8"); PrintWriter out = response.getWriter(); out.print("您购买了如下商品:<br/>"); //先判断浏览器有没以cookie的形式带sessionID过来,如果没有则判断URL地址有没有带sessionID List<Book> list = (List) request.getSession().getAttribute("list"); for(Book book : list){ out.print(book.getName() + "<br/>"); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
原文地址:http://blog.csdn.net/u010590318/article/details/40756279