标签:用户 contain param bsp cookies 项目 -- https 联系
之前介绍过cookie和session的原理和区别。下面举例说明一下cookie在实际项目中的使用。
使用cookie实现购物车功能:
主要是要点是:以产品id作为主键进行判断。如果这个添加的产品id信息已经在cookie里面存在,则添加失败(这里不考虑同一个商品重复添加多个的问题。因为只是cookie的简单举例);否则则把该产品的信息保存在cookie里面。
因此存在用户手动清楚cookie的情况,所以需要判断:
1 if(cookieMap.containsKey(key)){ 2 ... 3 } else { 4 //移除购物车商品失败 5 }
完整代码如下:
1 import java.util.HashMap; 2 import java.util.Map; 3 import javax.servlet.http.Cookie; 4 import javax.servlet.http.HttpServletRequest; 5 import javax.servlet.http.HttpServletResponse; 6 7 import net.sf.json.JSONObject; 8 9 /** 10 * 我的购物车 11 * @author Administrator 12 * 13 */ 14 public class MyShoppingCar { 15 16 /** 17 * 添加商品 18 * @param request 19 * @param response 20 * @param prodId 商品id 21 * @param prodPrice 商品价格 22 * @return 23 * @throws Exception 24 */ 25 public static JSONObject addInShoppingCar(HttpServletRequest request,HttpServletResponse response,String opId,String prodId,String prodPrice) throws Exception{ 26 //保存在cookie 27 String key = opId; 28 String value = prodId + "*" + prodPrice; 29 if("".equals(key)||"".equals(value)){ 30 throw new Exception("操作员信息不存在或者商品信息不存在,请联系管理员"); 31 } 32 JSONObject jsonObject = new JSONObject(true); 33 Map<String,Cookie> cookieMap = ReadCookieMap(request);//key--cookie 34 35 if(cookieMap.containsKey(key)){ 36 Cookie cookie = cookieMap.get(key); 37 String cookieVal = cookie.getValue(); 38 String[] vals = cookieVal.split(","); 39 for (int i = 0; i < vals.length; i++) { 40 if(prodId.equals(vals[i].split("\\*")[0])){ 41 jsonObject.put("status", "-1"); 42 jsonObject.put("msg", "添加购物车失败"); 43 return jsonObject; 44 } else { 45 if("".equals(cookieVal) || null == cookieVal){ 46 cookieVal = value; 47 } else { 48 cookieVal += "," + cookieVal; 49 } 50 cookie.setValue(value); 51 cookie.setPath("/"); 52 cookie.setMaxAge(24*60*60); 53 response.addCookie(cookie); 54 } 55 } 56 } else { 57 Cookie newCookie = new Cookie(key,value); 58 newCookie.setMaxAge(24*60*60); 59 newCookie.setPath("/"); 60 response.addCookie(newCookie); 61 } 62 jsonObject.put("status", "1"); 63 jsonObject.put("msg", "添加购物车成功"); 64 return jsonObject; 65 } 66 67 /** 68 * 返回cookieMap 69 * @param request 70 * @return 71 */ 72 private static Map<String, Cookie> ReadCookieMap(HttpServletRequest request) { 73 Cookie[] cookies = request.getCookies(); 74 Map<String,Cookie> cookieMap = null; 75 if(null != cookies && cookies.length>0){ 76 cookieMap = new HashMap<String,Cookie>(); 77 for (int i = 0; i < cookies.length; i++) { 78 cookieMap.put(cookies[i].getName(), cookies[i]); 79 } 80 } 81 return cookieMap; 82 } 83 84 /** 85 * 移除购物车里面的商品 86 * @param request 87 * @param response 88 * @param opId 89 * @param prodId 90 * @return 91 */ 92 public static JSONObject delFromShoppingCar(HttpServletRequest request,HttpServletResponse response,String opId,String prodId){ 93 JSONObject jsonObject = new JSONObject(); 94 Map<String, Cookie> cookieMap = ReadCookieMap(request); 95 if(cookieMap.containsKey(opId)){ 96 Cookie cookie = cookieMap.get(opId);//123*34,234*45,456*67 97 String cookieVal = cookie.getValue(); 98 String[] cvalue = cookieVal.split(","); 99 String newCval = ""; 100 for (int i = 0; i < cvalue.length; i++) { 101 if(!prodId.equals(cvalue[i].split("\\*")[0])){ 102 if("".equals(newCval) || null == newCval ){ 103 newCval = cvalue[i]; 104 } else { 105 newCval += "," + cvalue[i]; 106 } 107 } 108 } 109 //删除cookie 110 cookie.setMaxAge(0); 111 //重新创建cookie并保存购物车商品信息 112 Cookie newCookie = new Cookie(opId,newCval); 113 newCookie.setPath("/"); 114 newCookie.setMaxAge(24*60*60); 115 response.addCookie(newCookie); 116 jsonObject.put("status", "1"); 117 jsonObject.put("msg", "删除商品成功"); 118 119 } else { 120 jsonObject.put("status", "-1"); 121 jsonObject.put("msg", "删除商品失败"); 122 } 123 return jsonObject; 124 } 125 }
标签:用户 contain param bsp cookies 项目 -- https 联系
原文地址:http://www.cnblogs.com/codetree/p/6363636.html