怎么用cookie实现购物车?

bigbigaaa 2010-01-07 10:23:02
以前用过 session 做过一个购物车,现在想用cookie 实现购物车,哪位大侠给小弟讲讲思路,最好有源代码,小弟感激不尽!
...全文
267 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
http://m.cnblogs.com/40900/1345435.html
很经典
  • 打赏
  • 举报
回复
#region 添加到购物车AddShoppingCar
/// <summary>
/// 添加到购物车AddShoppingCar
/// </summary>
/// <param name="num">数量 如果存在产品 负数是减少
/// 正数是增加 如果不存在 直接增加</param>
/// <param name="id">货物ID</param>
/// <param name="expires">cookies保存的天数</param>
/// <param name="Products">cookies的名字</param>
/// <remarks>这里的方法就是把在原有的Cookie基础上判断是否有
/// 这个产品 如果有 在原有数量上增加 没有 就直接增加 如果是负
/// 数 就是减少 如果负数的数量大于等于
/// 原有数量 设置为0 对应后面的读出操作</remarks>
public static void AddShoppingCar(string num, string id, int expires, string products)
{
if (System.Web.HttpContext.Current.Request.Cookies[products] != null)
{
System.Web.HttpCookie cookie;
string cookievalue = System.Web.HttpContext.Current.Request.Cookies[products].Value;
if (System.Web.HttpContext.Current.Request.Cookies[products].Values[id.ToString()] == null)
{
cookievalue = cookievalue + "&" + id + "=" + num;

}
else
{
int num1 = int.Parse(System.Web.HttpContext.Current.Request.Cookies[products].Values[id.ToString()].ToString()) + int.Parse(num);
if (num1 > 0)
{
System.Web.HttpContext.Current.Request.Cookies[products].Values[id.ToString()] = num1.ToString();
}
else
{
System.Web.HttpContext.Current.Request.Cookies[products].Values[id.ToString()] = "0";
}
cookievalue = System.Web.HttpContext.Current.Request.Cookies[products].Value;
}
cookie = new System.Web.HttpCookie(products, cookievalue);

if (expires != 0)
{
DateTime dt = DateTime.Now;
TimeSpan ts = new TimeSpan(expires, 0, 0, 20);
cookie.Expires = dt.Add(ts);
}
System.Web.HttpContext.Current.Response.AppendCookie(cookie);
}
else
{
System.Web.HttpCookie newcookie = new HttpCookie(products);
if (expires != 0)
{
DateTime dt = DateTime.Now;
TimeSpan ts = new TimeSpan(expires, 0, 0, 20);

newcookie.Expires = dt.Add(ts);
}
newcookie.Values[id.ToString()] = num;
System.Web.HttpContext.Current.Response.AppendCookie(newcookie);
}
}
#endregion
SambaGao 2010-01-07
  • 打赏
  • 举报
回复
Java读写cookie

/*
* 写cookie
*/
Cookie namecookie = new Cookie("name",name);
Cookie passwordcookie = new Cookie("password",password);
Cookie optioncookie = new Cookie("option","1");

//生命周期
namecookie.setMaxAge(60*60*24*365);
passwordcookie.setMaxAge(60*60*24*365);
optioncookie.setMaxAge(60*60*24*365);

response.addCookie(namecookie);
response.addCookie(passwordcookie);
response.addCookie(optioncookie);

/*
* 读cookie
*/
Cookie[] cookies = request.getCookies();
if(cookies!=null)
{
String name = "";
String password = "";
String option = "";
for (int i = 0; i < cookies.length; i++)
{
Cookie c = cookies[i];
if(c.getName().equalsIgnoreCase("name"))
{
name = c.getValue();
}
else if(c.getName().equalsIgnoreCase("password"))
{
password = c.getValue();
}
else if(c.getName().equalsIgnoreCase("option"))
{
option = c.getValue();
}
}
}

阿_布 2010-01-07
  • 打赏
  • 举报
回复
生成cookie:
Cookie cookie = new Cookie("clothes","2");
cookie.setMaxAge("3600*12");
request.addCookie(cookie);

取cookie:
request.getCookies();
liwenso 2010-01-07
  • 打赏
  • 举报
回复
顶楼上
lenovo_itcast 2010-01-07
  • 打赏
  • 举报
回复
http://wangyu.javaeye.com/blog/210827,这里面讲的很详细,看看吧

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧