❶ 求有关jsp购物车的代码
你的描述太简单了,你告诉我你的架构,我给你写一个。[email protected]你发给我看看吧,但我不确定一定能完成!
❷ 用jsp和数据库做购物车,怎么能通过点击按钮把购买数量和商品信息传给购物车页面,急!!下面是部分代码
你把购买的数量和商品信息做成一个javabean,然后把这个javabean存在session里面,你点击按钮就向服务器端发出请求,然后服务器端处理请求后用jsp显示,这样就可以了呀
❸ jsp购物车代码
//shopping.html
<html>
<head><title>shopping stor</title></head>
<body>
<form action="carts.jsp" target="post">
<br>
please select the item that you want to buy
<br>
<select name="item">
<option>book:old man and the sea
<option>x-box game machine
<option>mp3 player
<option>cce
<option>book:jsp programming
<option>cd "the endless love"
<option>dvd "gone with the wind"
</select>
<br>
<input type="submit" name="submit" value="add">
<input type="submit" name="submit" value="remove">
</form>
</body>
</html>
------------------------------------------------------------------
//carts.jsp
<%@page contentType="text/html;charset=ISO8859_1" %>
<html>
<jsp:useBean id="cart" scope="session" class="test.DummyCart"/>
<jsp:setProperty name="cart" property="*"/>
<%
cart.processRequest();
%>
<br>
<ol>
you have chosen these items:
<%
String []items=cart.getItems();
for(int i=0;i<items.length;i++)
{
%>
<li><%=items[i] %></li>
<%
}
%>
</ol>
<hr>
<%@include file="shopping.htm" %>
</html>
---------------------------------------------------------------------//DummyCart.java
package test;
import javax.servlet.http.*;
import java.util.Vector;
import java.util.Enumeration;
public class DummyCart
{
Vector v = new Vector();
String submit=null;
String item= null;
private void addItem(String name)
{
v.addElement(name);
}
private void removeItem(String name)
{
v.removeElement(name);
}
public void setItem(String s)
{
item=s;
}
public void setSubmit(String s)
{
submit=s;
}
public String[] getItems()
{
String []s=new String[v.size()];
v.Into(s);
return s;
}
public void processRequest()
{
if(submit==null)
addItem(item);
if(submit.equals("add"))
addItem(item);
else if (submit.equals("remove"))
removeItem(item);
reset();
}
private void reset()
{
submit=null;
item=null;
}
}
----------------------------------------------------------------------
上面是一个简单的例子,功能都能实现,对网页效果要求更漂亮些的可做一些修改。
❹ jsp购物车:如何在不同页面传递ArrayList对象
servlet 会不会啊?
基础先打牢吧,先接触点儿简单的东西。
servlet传递对象的例子:
//mvc_login.htm
<form action="mvcdemo.mldn" method="POST">
输入姓名:<input type="text" name="uname">
<input type="submit" value="提交">
</form>
//MVCCheck.java 封装数据的类
package cn.mldn.lxh.bean ;
public class MVCCheck
{
private String name ;
public void setName(String name)
{
this.name = name ;
}
public String getName()
{
return this.name ;
}
// 验证
public boolean isValidate()
{
if(this.name==null||"".equals(this.name))
{
return false ;
}
else
{
return true ;
}
}
};
//MVCServlet.java (含xml配置语句)
package cn.mldn.lxh.servlet ;
import java.io.* ;
import javax.servlet.* ;
import javax.servlet.http.* ;
import cn.mldn.lxh.bean.MVCCheck ;
public class MVCServlet extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException
{
this.doPost(req,resp) ;
}
public void doPost(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException
{
String name = req.getParameter("uname") ;
MVCCheck dx = new MVCCheck() ;
// 将请求内容设置到mc对象之中
dx.setName(name) ;
String path = null ;
if(dx.isValidate())
{
// 保存“mc对象”在request范围之中
req.setAttribute("dx",dx) ;
path = "mvc_success.jsp" ;
}
else
{
path = "mvc_failure.jsp" ;
}
// 进行跳转
req.getRequestDispatcher(path).forward(req,resp) ;
}
};
/*
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>cn.mldn.lxh.servlet.MVCServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>/mvcdemo.mldn</url-pattern>
</servlet-mapping>
*/
//mvc_success.jsp 接收对象
<%@page contentType="text/html;charset=gb2312"%>
<h1>输入成功!!!</h1>
<h2>欢迎${dx.name}光临!!!</h2>
❺ 求jsp购物车代码
太长了
复制不下
❻ JSP购物车代码详解
SELECT 1
SCAN
SCATTER MEMVAR
SELECT 2
APPEND BLANK
GATHER MEMVAR
ENDSCAN
❼ jsp+servlet做的购物网站怎么绑定用户和购物车(实现每个用户有独立的购物车)
当然是持久化到数据库中了;
建一个购物车表,用户ID作为外键约束,添加删除内购物车的时候直接容操作数据库就行了;
如果要实现用户不登录也有购物车的功能,就加一个cookie,把商品存在cookie里,浏览器不关闭就都在;
❽ jsp购物车中,当多个用户看中唯一商品时,都要加入购物车,用什么方法解决
什么吗。商品唯一,为什么要限制只能一个人买呢。
再说,如果用户只是暂时放在购物车里了专,但真正属付款的时候,他决定不买了,那岂不是耽误了别人购买吗。
所以你这种解决方案本身就不太好,应该在购买的再去解决冲突。 购买的方法设置一个同步就可以了。
❾ jsp购物车的代码和数据库
//购物车类ShoppingCart,仅供参考
packagebookshop;
importjava.util.*;
importjava.sql.*;
importjava.text.*;
publicclassShoppingCart{
HashMapitems=null;
publicShoppingCart(){
items=newHashMap();
}
publicsynchronizedvoidadd(StringbookID)throwsException{
if(items.containsKey(bookID))
{
ShoppingCartItemitem=(ShoppingCartItem)items.get(bookID);
item.quantity++;
}
else
{
Stringsql="SELECT*FROMtb_BookWHEREbookID='"+bookID
+"'";
DBHandledbhandle=newDBHandle();
ResultSetrs=dbhandle.executeQuery(sql);
ShoppingCartItemnewItem=newShoppingCartItem();
if(rs.next()){
newItem.bookID=bookID;
newItem.isbn=rs.getString("ISBN");
newItem.bookName=rs.getString("bookName");
newItem.bookImage=rs.getString("bookImage");
newItem.categoryID=rs.getString("categoryID");
newItem.author=rs.getString("author");
newItem.price=rs.getFloat("price");
newItem.description=rs.getString("description");
newItem.quantity=1;
dbhandle.closeResource();
items.put(bookID,newItem);
}
}
}
publicsynchronizedvoidsetItem(StringBookID,intnum){
if(items.containsKey(BookID)){
ShoppingCartItemitem=(ShoppingCartItem)items.get(BookID);
item.quantity=num;
}
}
publicsynchronizedvoidremove(StringBookID){
items.remove(BookID);
}
(){
Collectionc=items.values();
returnc.iterator();
}
protectedvoidfinalize()throwsThrowable{
items.clear();
}
(){
returnitems.size();
}
(){
doubleamount=0.0;
for(Iteratori=getItems();i.hasNext();){
ShoppingCartItemitem=(ShoppingCartItem)i.next();
amount+=item.quantity*item.price;
}
returnroundOff(amount);
}
privatedoubleroundOff(doublex){
longval=Math.round(x*100);//cents
returnval/100;
}
publicsynchronizedvoidclear(){
items.clear();
}
publicintpayOrder(StringuserName,StringtrueName,Stringpostcode,
Stringaddress,Stringtelephone,Stringmemo)throwsException{
Connectionconn=null;
intorderID=0;
DBHandledbhandle=newDBHandle();
try{
conn=dbhandle.getConnection();
conn.setAutoCommit(false);
Statementstmt=conn.createStatement();
doubletotal=getTotal();
SimpleDateFormatdataFormat=newSimpleDateFormat(
"yyyy-MM-ddHH:mm:ss");
StringorderDate=dataFormat.format(newjava.util.Date());
Stringsql="INSERTINTOtb_Orders(userName,trueName,address,postcode,tel,memo,totalPrice,isPay,isDeliver,orderDate)VALUES('"
+userName+"','"+trueName
+"','"
+address
+"','"
+postcode
+"','"
+telephone
+"','"
+memo
+"',"
+total+",'0','0','"+orderDate+"')";
System.out.println(sql);
stmt.executeUpdate(sql);
sql="SELECTMAX(orderID)ASmaxOrderIDFROMtb_Orders";//改动
System.out.println(sql);
ResultSetrs=stmt.executeQuery(sql);
rs.next();
orderID=rs.getInt(1);
Iteratori=getItems();
while(i.hasNext()){
ShoppingCartItemitem=(ShoppingCartItem)i.next();
intquantity=item.quantity;
Stringid=item.bookID;
doubleprice=item.price;
sql="INSERTINTOtb_OrderDetail(orderID,bookID,quantity,price)VALUES("
+orderID
+","
+id
+","
+quantity
+","
+price
+")";
stmt.executeUpdate(sql);
}
conn.commit();
conn.setAutoCommit(true);
}catch(Exceptionex){
conn.rollback();
System.out.println(ex.getMessage());
throwex;
}finally{
dbhandle.closeResource();
}
returnorderID;
}
}
//图书信息类,ShoppingCartItem
packagebookshop;
publicclassShoppingCartItem{
publicStringbookID=null;
publicStringisbn=null;
publicStringbookName=null;
publicfloatprice=0.0F;
publicStringdescription=null;
publicStringbookImage=null;
publicStringauthor=null;
publicStringcategoryID=null;
publicintquantity;
}