上一篇博文中,讲到用cookie实现简单的购物车,但是有的用户,会因为不小心把cookie给禁止了,如果遇到这种情况,为了我不损失用户,那也是我们编程人员必须也能让用户感觉到,是在正常购物,下面我从原理讲起,到购物车案例
首先我教大家简单的session使用
下面先了解一下简单的使用方法Demo1创建session,并重定向到Demo2中
import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class Demo1 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); session.setAttribute("hello", "hello wolrd"); response.sendRedirect(request.getContextPath() + "/servlet/Demo2"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
Demo2显示demo1带过来的session数据
package com.itcast.simple; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class Demo2 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); String hello = (String) session.getAttribute("hello"); response.getWriter().write(hello); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
简单的讲解,现在来实现,没有cookie的支持,怎么实现购物功能,这里我们用两个类来模拟数据
1.book类来封装书的属性
package cn.itcast.session.javabean; public class Book { private String id; private String name; private String author; private String price;
package com.itcast.session.withoutcookie; import java.io.IOException; import java.util.ArrayList; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import cn.itcast.session.javabean.Book; import cn.itcast.session.javabean.DB; public class BuyServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String id = request.getParameter("id"); Book book = DB.Findbook(id);//通过id找到书 System.out.println(id); HttpSession session = request.getSession(); ArrayList list = (ArrayList) session.getAttribute("list");//第一次访问里面没有书 if (list == null) { list = new ArrayList<Book>(); } list.add(book); session.setAttribute("list", list); response.sendRedirect(request.getContextPath() + "/servlet/ListCart");//将url重写。使其带着sessionid数据传给ListCart } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }return id; } public void setId(String id) { this.id = id; } public Book() { super(); // TODO Auto-generated constructor stub } 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; } public String getPrice() { return price; } public void setPrice(String price) { this.price = price; } public Book(String id, String name, String author, String price) { super(); this.id = id; this.name = name; this.author = author; this.price = price; } }
2.用来模拟几本书
package cn.itcast.session.javabean; import java.util.Collection; import java.util.LinkedHashMap; import java.util.Map; public class DB { private static Map<String, Book> books = new LinkedHashMap<String, Book>(); public static Collection<Book> getAll() { return books.values(); } static { books.put("1", new Book("1", "php实战", "老路", "hello")); books.put("2", new Book("2", "android开发", "老徐", "110")); books.put("3", new Book("3", "jdbc开发", "老黎", "106")); books.put("4", new Book("4", "c++算法", "老方", "107")); } public static Book Findbook(String bookid) { return books.get(bookid); } }
3.显示购买数的列表,并在链接后面带上超连接
package com.itcast.session.withoutcookie; import java.io.IOException; import java.io.PrintWriter; import java.util.Collection; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import cn.itcast.session.javabean.Book; import cn.itcast.session.javabean.DB; public class ListBookServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); Collection<Book> all = DB.getAll(); for (Book book : all) { System.out.println(book.getId()); out.write("<a href=/day07_sisson/servlet/BuyServlet?id=" + book.getId() + ">" + book.getName() + "</a></br>"); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
4.利用传过来的id,找到书,将书添加到list集合,然后存进session中,最后,将url重写,将session的数据,带到ListCart
package com.itcast.session.withoutcookie; import java.io.IOException; import java.util.ArrayList; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import cn.itcast.session.javabean.Book; import cn.itcast.session.javabean.DB; public class BuyServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String id = request.getParameter("id"); Book book = DB.Findbook(id);//通过id找到书 System.out.println(id); HttpSession session = request.getSession(); ArrayList list = (ArrayList) session.getAttribute("list");//第一次访问里面没有书 if (list == null) { list = new ArrayList<Book>(); } list.add(book); session.setAttribute("list", list); response.sendRedirect(request.getContextPath() + "/servlet/ListCart");//将url重写。使其带着sessionid数据传给ListCart } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
5.展示购物车里面的数据
package com.itcast.session.withoutcookie; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import cn.itcast.session.javabean.Book; public class ListCart extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); PrintWriter writer = response.getWriter(); HttpSession session = request.getSession(); writer.write("您浏览过的商品</br>"); ArrayList<Book> list = (ArrayList<Book>) session.getAttribute("list"); for (Book book : list) { System.out.println(book.getName()); writer.write(book.getName() + "</br>"); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }