forked from maxliaops/Java_Web_Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcart_Add.jsp
More file actions
43 lines (42 loc) · 1.26 KB
/
cart_Add.jsp
File metadata and controls
43 lines (42 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<%@ page import="java.util.Vector"%>
<%@ page import="beans.bookelement"%>
<jsp:useBean id="connDB" scope="page" class="beans.connDB"/>
<jsp:useBean id="chStr" scope="page" class="beans.chStr"/>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>添加购物车</title>
</head>
<body>
<%
String ISBN=chStr.chStr(request.getParameter("ISBN"));
String sql="select * from tb_bookinfo where ISBN='"+ISBN+"'";
ResultSet rs=connDB.executeQuery(sql);
float price=0;
if(rs.next()){
price=rs.getInt("price");
}
bookelement mybookelement=new bookelement();
mybookelement.ISBN=ISBN;
mybookelement.price=price;
mybookelement.number=1;
boolean Flag=true;
Vector cart=(Vector)session.getAttribute("cart");
if(cart==null){
cart=new Vector();
}else{
for(int i=0;i<cart.size();i++){
bookelement bookitem=(bookelement)cart.elementAt(i);
if(bookitem.ISBN.equals(mybookelement.ISBN)){ //此处必须用equals方法,不能用==
bookitem.number++;
cart.setElementAt(bookitem,i);
Flag=false;
}
}
}
if(Flag)cart.addElement(mybookelement);
session.setAttribute("cart",cart);
rs.close();
response.sendRedirect("cart_see.jsp");
%>