购物车商品重复添加的问题
<%@ page contentType="text/html; charset=GBK" import="java.util.*" %>
<html>
<head>
<title>
添加至购物车.jsp
</title>
</head>
<body>
<%
String goodsname;
String goodsname1=request.getParameter("goodsname");
goodsname1=new String(goodsname1.getBytes("ISO-8859-1"));
ArrayList list=null;
list=(ArrayList)session.getAttribute("list");
if(list==null)
{
list=new ArrayList();
list.add(goodsname1);
session.setAttribute("list",list);
}
else {
for(int i=0;i<list.size();i++)
{
if(list.get(i)==goodsname1){list.remove(list.get(i)); }}
list=(ArrayList)session.getAttribute("list");
list.add(goodsname1);
out.print("<h1 align=center>");
out.print(goodsname1+"添加成功!");
out.print("<br>");
out.print("<a href=chakan.jsp>继续购物</a>");
out.print(" ");
out.print("<a href=pay.jsp>查看购物车</a>");
out.print("</h1>");
}
%>
</body>
</html>
为什么我第一次添加的时候,什么也不会出现,当我刷新时,就添加了二次,查看购物车时,里面存在同样的商品,我不是已经移除了吗?
------解决方案--------------------
for(int i=0;i<list.size();i++)
{
if(list.get(i)==goodsname1){list.remove(list.get(i)); }}
list=(ArrayList)session.getAttribute("list");
list.add(goodsname1);
goodnam1被移除以后,该list没有set到session,又从session中重新去出了新的list,
新的list为移除前的状态