购物车
**shop**
<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="GB18030"%>
<%
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 'show.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>
<table width="600px" border="1px" cellpadding="0" cellspacing="0" align="center">
<tr align="center" >
<td>商品表编号</td>
<td>商品名称</td>
<td>商品描述</td>
<td>商品价格</td>
<td>我要购买</td>
<td>查看购物车</td>
</tr>
<%
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost/shop","root","root");
Statement stmt= con.createStatement();
ResultSet rs=stmt.executeQuery("select * from goods");
while (rs.next())
{
out.print("<tr align='center'>");
out.print("<td>"+rs.getString("id")+"</td>");
out.print("<td>"+rs.getString("name")+"</td>");
out.print("<td>"+rs.getString("description")+"</td>");
out.print("<td>"+rs.getString("price")+"</td>");
out.print("<td><a href=buy.jsp?id="+rs.getString("id")+">购买</a></td>");
out.print("<td><a href=look.jsp>查看我的购物车</a></td>");
out.print("</tr>");
}
%>
</table>
</body>
</html>
**buy**
<%@ page language="java" import="java.util.*,shopping.cart.*,java.sql.*" pageEncoding="GB18030"%>
<%
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");
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost/shop","root","root");
Statement stmt= con.createStatement();
ResultSet rs=stmt.executeQuery("select * from goods where id='"+id+"'");
ArrayList <Product> list =(ArrayList)session.getAttribute("list");
int n=0;
rs.next();
if (list==null)
{ Product p=new Product();
list=new ArrayList<Product>();
p.setId(rs.getString("id"));
p.setName(rs.getString("name"));
p.setDescription(rs.getString("description"));
p.setPrice(rs.getDouble("price"));
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)
{ out.print("111111111111");
Product p2=new Product();
p2.setId(rs.getString("id"));
p2.setName(rs.getString("name"));
p2.setDescription(rs.getString("description"));
p2.setPrice(rs.getDouble("price"));
p2.setNumber(1);
list.add(p2);
}
}
session.setAttribute("list",list);
response.sendRedirect("show.jsp");
%>
</body>
</html>
**look**
<%@ page language="java" import="java.util.*,shopping.cart.*" pageEncoding="GB18030"%>
<%
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 'look.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>
<%
ArrayList <Product> l =(ArrayList)session.getAttribute("list");
%>
<table width="600px" border="1px" cellpadding="0" cellspacing="0" align="center">
<caption align="center">我的购物车</caption>
<tr align="center" >
<td>商品表编号</td>
<td>商品名称</td>
<td>商品描述</td>
<td>商品数量</td>
<td>商品价格</td>
<td>商品总价 </td>
<td>删除物品 </td>
</tr>
<%
Product p=new Product();
for(int i=0;i<l.size();i++)
{
p=(Product)l.get(i);
%>
<tr align="center">
<td><%=p.getId() %></td>
<td><%=p.getName() %></td>
<td><%=p.getDescription() %></td>
<td><%=p.getNumber() %></td>
<td><%=p.getPrice() %></td>
<td><%=p.getPrice()*p.getNumber() %></td>
<td><a href=del.jsp?id=<%=p.getId() %>>删除商品</a></td>
</tr>
<%
}
%>
</table>
</body>
</html>
**del**
<%@ page language="java" import="java.util.*,shopping.cart.*" pageEncoding="GB18030"%>
<%
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");
ArrayList <Product> list =(ArrayList)session.getAttribute("list");
for(int i=0;i<list.size();i++)
{
if (list.get(i).getId().equals(id))
{
list.remove(i);
break;
}
}
session.setAttribute("list",list);
response.sendRedirect("look.jsp");
%>
</body>
</html>