购物车问题
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]]