<%@ page language="java" import="java.util.*,shoppingcart.*" pageEncoding="gbk"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'buy.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
String id=request.getParameter("id");
/* String name=request.getParameter("name");
System.out.println(name);*/
ArrayList <Product> list= (ArrayList)session.getAttribute("list");
int n=0;
if(list==null)
{
Product p=new Product();
list=new ArrayList<Product>();
if (id.equals("1"))
{
p.setId("1");
p.setName("苹果");
p.setDescription("富士山的苹果");
p.setPrice(10);
p.setNumber(1);
}
if (id.equals("2"))
{
p.setId("2");
p.setName("香蕉");
p.setDescription("海南香蕉");
p.setPrice(5);
p.setNumber(1);
}
if (id.equals("3"))
{
p.setId("3");
p.setName("橘子");
p.setDescription("驻马店橘子");
p.setPrice(3);
p.setNumber(1);
}
list.add(p);
}
else
{
Product p1=new Product();
for(int i=0;i<list.size();i++)
{
p1=(Product)list.get(i);
if(p1.getId().equals(id))
{
p1.setNumber(p1.getNumber()+1);
n++;
list.set(i,p1);
break;
}
}
if(n==0)
{
Product p2=new Product();
if(id.equals("1"))
{
p2.setId("1");
p2.setNumber(1);
p2.setName("苹果");
p2.setPrice(10);
p2.setDescription("富士山的苹果");
}
if(id.equals("2"))
{
p2.setId("2");
p2.setNumber(1);
p2.setName("香蕉");
p2.setPrice(5);
p2.setDescription("海南香蕉");
}
if(id.equals("3"))
{
p2.setId("3");
p2.setNumber(1);
p2.setName("橘子");
p2.setPrice(3);
p2.setDescription("驻马店橘子");
}
list.add(p2);
}
}
session.setAttribute("list",list);
response.sendRedirect("show.jsp");
%>
</body>
</html>