2021-10-19

购物车

**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>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_48691931

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值