| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 926 人关注过本帖
标题:购物车问题
只看楼主 加入收藏
罗彬
Rank: 1
等 级:新手上路
帖 子:141
专家分:0
注 册:2006-4-24
结帖率:100%
收藏
 问题点数:0 回复次数:0 
购物车问题
buybook.jsp:
<body>
<jsp:include page="header.jsp"/>
<%    

    
    String Isbn=new String(request.getParameter("isbn").trim().getBytes("ISO-8859-1"));
    int Buynum=Integer.parseInt(request.getParameter("buynum"));
    Vector buylist = (Vector)session.getValue("shopcar");
    
    int ismatch = 0;
    %>
     <jsp:useBean id="book" scope="session" class="shop.car.Book"/>
    <jsp:setProperty name="book" property="isbn" param="isbn"/>
    <jsp:setProperty name="book" property="buynum" param="buynum"/>
    <%try       //将每个ISBN信息写入类中,让shopcar中的类好存取
    {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    }catch(ClassNotFoundException e){}
    
            Connection con=DriverManager.getConnection("Jdbc:Odbc:Bookinfo","sa","");
            Statement sql=con.createStatement();
            ResultSet rs=sql.executeQuery("select * from bookinfo where isbn ='"+Isbn+"'");
            if(rs.next()){
             String bkname=rs.getString("title");
             Float bkprice=rs.getFloat("price");
             book.setTitle(bkname);
             book.setPrice(bkprice);
                }
        if (buylist == null)        
        {
            buylist = new Vector();
            buylist.addElement(book);
        }
        else
        {
            for(int i=0; i<buylist.size(); i++)
                {%>
                    <jsp:useBean id="_book" scope="session" class="shop.car.Book"/>
                    <% _book =(Book)buylist.elementAt(i);
                    if(_book.getIsbn().equals(book.getIsbn()))
                        {
                            _book.setBuynum(_book.getBuynum()+book.getBuynum());
                            buylist.setElementAt(_book,i);
                            ismatch = 1;
                        }
                }
        if(ismatch==0)
            buylist.addElement(book);
        }
        session.putValue("shopcar",buylist);
        
        response.sendRedirect("shopcar.jsp");
    %>
</body>
</html>


shopcar.jsp
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<%@ page import="java.util.*"%>
<%@ page import="shop.car.Book"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head>

<body>
<jsp:include page="header.jsp"/>
<br><a class="title">查看购物车</a><br>
<%
    Vector buylist = (Vector)session.getValue("shopcar");
    if(buylist!=null&&(buylist.size()>0))
        {
            float pricenum =0;
%>            
<form name="form1" method="post" action="checkout.jsp">
  <table width="700" border="1" cellpadding="0" cellspacing="0">
    <tr>
      <td width="430" height="22"><div align="center">书名</div></td>
      <td width="148"><div align="center">单价</div></td>
      <td width="114"><div align="center">购买数量</div></td>
    </tr>
<%
    for(int i=0;i<buylist.size();i++)
        {
            %>
            <jsp:useBean id="book" scope="session" class="shop.car.Book"/>
            <%book=(Book)buylist.elementAt(i);
            pricenum = pricenum + book.getBuynum()*book.getPrice();
            
%>
    <tr>
      <td height="21"><div align="center"><%=book.getTitle()%></div></td>
      <td><div align="center"><%=book.getPrice()%></div></td>
      <td><div align="center"><%=book.getBuynum()%></div></td>
    </tr>
<%}
%>    
    <tr>
      <td height="45" colspan="3"><div align="center"></div>        <div align="center"></div>        
        <div align="center">总计价格:<%=pricenum%>元</div></td>
    </tr>
  </table>
  <p>
    <input type="submit" name="Submit" value="确定购买">
    <a href="booklist.jsp">继续</a>
  </p>
</form>
<%}
    else    
{%>
<br>购物车为空!<br>

<%}%>
<p>?</p>

</body>
</html>
为什么只能买一样书,买第二本书时,就会自动的把第一本替换掉

[[it] 本帖最后由 罗彬 于 2008-3-29 19:28 编辑 [/it]]
搜索更多相关主题的帖子: 购物车 
2008-03-29 15:50
快速回复:购物车问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.090859 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved