使用session技术来实现网上商城购物车的功能
首先、简单的了解session和cookie的区别:
一、session和cookie的区别:
session是把用户的首写到用户独占的session中(服务器端)
cookie是把用户的数据写给用户的浏览器。
session对象由服务器创建,开发人员可以调用request对象的getsession方法得到session对象。
首先写页面代码,仍然借助于商品浏览记录来写:
1、jsp购买页面代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@page import="java.util.*"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>购物页面</title> </head> <body> <h1>商品列表</h1> <ul > <li><a href="/myday07/addcookie?id=1">java基础</a><a href="/myday07/buysession?id=1">购买</a></li> <li><a href="/myday07/addcookie?id=2">javaweb</a><a href="/myday07/buysession?id=2">购买</a></li> <li><a href="/myday07/addcookie?id=3">DHTML</a><a href="/myday07/buysession?id=3">购买</a></li> <li><a href="/myday07/addcookie?id=4">数据库</a><a href="/myday07/buysession?id=4">购买</a></li> <li><a href="/myday07/addcookie?id=5">Struts</a><a href="/myday07/buysession?id=5">购买</a></li> <li><a href="/myday07/addcookie?id=6">Spring</a><a href="/myday07/buysession?id=6">购买</a></li> <li><a href="/myday07/addcookie?id=7">Hibernate</a><a href="/myday07/buysession?id=7">购买</a></li> </ul> <h1>最近访问记录</h1><h5><a href="/myday07/clearcookie">清空数据</a> </h5> <% String[] arr = {"java基础","javaweb","DHTML","数据库","Struts","Spring","Hibernate"}; // 获得所有cookie Cookie[] cookies = request.getCookies(); // 在cookie中查找商品浏览记录 String visitlist = null; if(cookies!=null){ for (Cookie cookie : cookies) { if (cookie.getName().equals("visitlist")) { visitlist = cookie.getValue(); break; } } if(visitlist==null){ // 商品列表不存在 out.println("无"); }else{ String[] existIds = visitlist.split(","); for(String existId:existIds){ int id = Integer.parseInt(existId); out.println(arr[id-1]+"<br/>"); } } } %> <hr> <h1>购物车列表</h1> <% //1.获得购物车列表 Map<String, Integer> cart=(Map<String, Integer>)session.getAttribute("cart"); //2.遍历map输出 if(cart!=null){ Set<String> keys=cart.keySet(); for(String id:keys) { int number=cart.get(id);//每件商品的数量 int idInt=Integer.parseInt(id); String productName=arr[idInt-1]; out.println("商品名称:"+productName+",商品数量:"+number+"<br/>"); } } %> <hr/> </body> </html>
2、编写商品浏览记录的servlet页面
package cn.itcast.cookie; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class addCookie extends HttpServlet { // 获得用户当前访问商品,更新到商品浏览记录列表 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 自动刷新 response.setHeader("refresh", "2;url=/myday07/products1.jsp"); // 1、获得当前用户浏览商品id String id = request.getParameter("id"); // 2、获得用户浏览记录列表 Cookie[] cookies = request.getCookies(); // 查找访问记录列表 name:visitlist ----- 1,3,5,6 String visitlist = null; if (cookies != null) { // 存在cookie for (Cookie cookie : cookies) { if (cookie.getName().equals("visitlist")) { visitlist = cookie.getValue(); break; } } if (visitlist == null) { // 列表不存在,当前商品第一件商品 Cookie cookie = new Cookie("visitlist", id); cookie.setMaxAge(60 * 60); response.addCookie(cookie); response.getWriter().println("add success"); } else { // 3、判断当前商品是否已经在列表中 String[] existIds = visitlist.split(","); for (String exsitId : existIds) { if (exsitId.equals(id)) { // id存在在列表中 response.getWriter().println("add success"); return; } } // 4、如果当前商品不在列表中,添加到列表 Cookie cookie = new Cookie("visitlist", visitlist + "," + id); cookie.setMaxAge(60 * 60); response.addCookie(cookie); response.getWriter().println("add success"); } } else { // 表示 并没有该站点的任何cookie Cookie cookie = new Cookie("visitlist", id); cookie.setMaxAge(60 * 60); response.addCookie(cookie); response.getWriter().println("add success"); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
3、编写清除缓存的cookie页面
package cn.itcast.cookie; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class clearCookie extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Cookie cookie=new Cookie("visitlist",""); cookie.setMaxAge(0); response.addCookie(cookie); response.sendRedirect("/myday07/products1.jsp"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
4、编写商品购买页面
package cn.itcast.cookie; import java.io.IOException; 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; import javax.servlet.http.HttpSession; public class BuySession extends HttpServlet { //添加商品到购物车 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1.获得商品的id String id=request.getParameter("id"); //2.获得购物车cart--从session得到 HttpSession session=request.getSession(); Map<String, Integer> cart=(Map<String, Integer>)session.getAttribute("cart");//如果session中没有购物车呢 if(cart==null) {//原来的session中并不存在购物车 cart=new HashMap<String, Integer>(); } //3.判断商品是否存在购物车中 if(cart.containsKey(id)) { //4.存在数量+1 int number=cart.get(id); cart.put(id, number+1); }else{ //5.不存在 将商品存入购物车 数量1 cart.put(id, 1); } session.setAttribute("cart", cart); response.sendRedirect("/myday07/products1.jsp");//重定向回去 } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
最终效果如下: