怎么用cookie实现购物车? 以前用过 session 做过一个购物车,现在想用cookie 实现购物车,哪位大侠给小弟讲讲思路,最好有源代码,小弟感激不尽! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://wangyu.javaeye.com/blog/210827,这里面讲的很详细,看看吧 生成cookie:Cookie cookie = new Cookie("clothes","2");cookie.setMaxAge("3600*12");request.addCookie(cookie);取cookie:request.getCookies(); 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(); } } } #region 添加到购物车AddShoppingCar /// <summary> /// 添加到购物车AddShoppingCar /// </summary> /// <param name="num">数量 如果存在产品 负数是减少 /// 正数是增加 如果不存在 直接增加</param> /// <param name="id">货物ID</param> /// <param name="expires">cookies保存的天数</param> /// <param name="Products">cookies的名字</param> /// <res>这里的方法就是把在原有的Cookie基础上判断是否有 /// 这个产品 如果有 在原有数量上增加 没有 就直接增加 如果是负 /// 数 就是减少 如果负数的数量大于等于 /// 原有数量 设置为0 对应后面的读出操作</res> 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 http://m.cnblogs.com/40900/1345435.html很经典 如何实现一个 字符串类 ?? this能否调用static修饰的类变量和类方法呢? jdbc创建的连接同一个数据库的对象,是否是singleton? Socket问题! 命令窗口运行 [建议]:增加Java-GUI板块。[希望支持。并散分] 初学者的问题:RMI使用的几个问题。谢谢! 我的独立日8.1!摆脱微软!(我对人们对Linux态度的一些看法) 请问如何用JAVA做画流程图的软件 64分问一个关于ObjectWrite()的问题 怎么建立字母与数字之间的对应关系 java如何判断一个正数是否是2的幂次?
Cookie cookie = new Cookie("clothes","2");
cookie.setMaxAge("3600*12");
request.addCookie(cookie);取cookie:
request.getCookies();
* 写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();
}
}
}
/// <summary>
/// 添加到购物车AddShoppingCar
/// </summary>
/// <param name="num">数量 如果存在产品 负数是减少
/// 正数是增加 如果不存在 直接增加</param>
/// <param name="id">货物ID</param>
/// <param name="expires">cookies保存的天数</param>
/// <param name="Products">cookies的名字</param>
/// <res>这里的方法就是把在原有的Cookie基础上判断是否有
/// 这个产品 如果有 在原有数量上增加 没有 就直接增加 如果是负
/// 数 就是减少 如果负数的数量大于等于
/// 原有数量 设置为0 对应后面的读出操作</res>
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
很经典