shopcart.jsp
来自「jsp的技术教程」· JSP 代码 · 共 224 行
JSP
224 行
<%@ page language="java" import="java.sql.*" %>
<jsp:useBean id="workM" scope="page" class="test.faq" />
<%@ include file="function.inc.jsp"%>
<%
if(session.getAttribute("username")==null||session.getAttribute("username")=="")
{
response.sendRedirect("../member/login.jsp?url="+request.getRequestURI());
}
%>
<%!
public String getStr(String str)
{
try
{
String temp_p=str;
byte[] temp_t=temp_p.getBytes("GBK");
String temp=new String(temp_t,"ISO8859_1");
return temp;
}
catch(Exception e)
{
}
return "null";
}
%>
<%
String book_id=request.getParameter("book_id");
//是否已经选择该货物
String sql="select ID from orders where book_id="+book_id+" and user_name='"+session.getAttribute("username")+"' and status=0";
ResultSet RS=workM.executeQuery(sql);
//out.println(sql);
int rowscount=0;
try
{
while(RS.next())
{
rowscount++;
}
}
catch(Exception e)
{
}
//把货物放入购物车
//out.println(rowscount==0);
if(rowscount==0)
{
String sqlBook1="select book.* from book where id="+book_id;
ResultSet RSBook1=workM.executeQuery(sqlBook1);
while(RSBook1.next())
{
String sqlCart="insert into orders(user_name,book_id,book_number,status,goods_price) Values('"+session.getAttribute("username")+"','"+book_id+"','1','0','"+RSBook1.getDouble("price")+"')";
//out.println(sqlCart);
workM.executeQuery(sqlCart);
}
}
else
{
String sqlBook2="select book.* from book where id="+book_id;
ResultSet RSBook2=workM.executeQuery(sqlBook2);
while(RSBook2.next())
{
String sqlAdd="update orders set book_number=book_number+1,goods_price="+RSBook2.getDouble("price")+" where book_id="+book_id+" and user_name='"+session.getAttribute("username")+"' and status=0";
//out.println(sqlAdd);
workM.executeQuery(sqlAdd);
}
}
/*
int count;
count=0;
String sqlBook3="select * from price where user_name="+session.getAttribute("username");
ResultSet RSBook3=workM.executeQuery(sqlBook3);
while(RSBook3.next())
{
count=count+1;
}
if(count==0)
{
String sqlBook4="select * from orders where user_name="+session.getAttribute("username");
ResultSet RSBook4=workM.executeQuery(sqlBook4);
while(RSBook4.next())
{
String sqlPrice="insert into price(user_name,goods_price,total_price) values('"+session.getAttribute("username")+"','"+(double)(RSBook4.getDouble("goods_price")*RSBook4.getInt("book_number"))+"','goods_price+5')";
workM.executeQuery(sqlPrice);
}
}
else
{
String sqlBook5="select * from orders where user_name="+session.getAttribute("username");
ResultSet RSBook5=workM.executeQuery(sqlBook5);
while(RSBook5.next())
{
String sqlPrice="update price set goods_price="+(double)(RSBook5.getDouble("goods_price")*RSBook5.getInt("book_number"))+",total_price=goods_price+5";
workM.executeQuery(sqlPrice);
}
}*/
response.sendRedirect("cart.jsp");
%>
<html>
<head>
<title>[详细资料]</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body bgcolor="#FFFFFF">
<div align="center">
<table width="100%" border="0" bgcolor="#CCCCFF">
<tr>
<td>
<div align="center"><b><font color="#FF0000">购 物 车</font></b> </div>
</td>
</tr>
</table>
<p>您的购物车中包含以下货物:</p>
<table width="100%" border="0" align="center">
<tr bgcolor="#FFCCCC">
<td width="15%">
<div align="center"><font color="#0000FF">购买数量</font></div>
</td>
<td width="56%">
<div align="center"><font color="#0000FF">书名</font></div>
</td>
<td width="13%">
<div align="center"><font color="#0000FF">单价</font></div>
</td>
<td width="16%">
<div align="center"><font color="#0000FF">总价格</font></div>
</td>
</tr>
<%
String sqlList="select * from orders where user_name='"+session.getAttribute("username")+"' and status=0";
ResultSet RSList=workM.executeQuery(sqlList);
try
{
while(RSList.next())
{
int b_num;
b_num=RSList.getInt("book_number");
%>
<tr bgcolor="#CCFFCC">
<td width="15%">
<div align="center"> <font color="#0000FF">
<input type="text" name="book_number" size="4" value=<%=b_num%>>
</font></div>
</td>
<%
String sqlBook="select book.* from book where id="+RSList.getInt("book_id");
ResultSet RSBook=workM.executeQuery(sqlBook);
while(RSBook.next())
{
%>
<td width="56%">
<div align="center"><font color="#0000FF"><%=getStr(RSBook.getString("name"))%></font></div>
</td>
<td width="13%">
<%
double price;
price=RSBook.getDouble("price");
%>
<div align="center"><font color="#0000FF"><%=price%>元</font></div>
</td>
<td width="16%">
<div align="center"><font color="#0000FF">¥<%=(double)price*b_num%>元</font></div>
</td>
<%}%>
</tr>
<tr bgcolor="#CCCCFF">
<td colspan="3">
<div align="center"><font color="#0000FF">货物价格</font></div>
</td>
<td width="16%">
<div align="center"><font color="#0000FF">¥</font></div>
</td>
</tr>
<%
}
}
catch(Exception e)
{
}
%>
<tr>
<td height="18" colspan="3" bgcolor="#CCCC99"><font color="#0000FF">
</font>
<div align="center"><font color="#0000FF">运输费用</font></div>
</td>
<td height="18" width="16%" bgcolor="#CCCC99">
<div align="center"><font color="#0000FF">¥</font></div>
</td>
</tr>
<tr bgcolor="#66FF66">
<td colspan="3">
<div align="center"><b><font color="#FF0000">总费用</font></b></div>
</td>
<td width="16%">
<div align="center"><b><font color="#FF0000">¥</font></b></div>
</td>
</tr>
<tr align="center">
<td colspan="4">
<form name="form1" method="post" >
<input type="button" value="继续购物" onclick="javascript:self.close();">
<input type="submit" name="Submit" value="重新计算价格">
<input type="submit" name="Submit2" value="填写订单">
</form>
</td>
</tr>
</table>
<p> </p>
</div>
</body>
</html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?