當前位置:首頁 » 網購平台 » JSP6簡單購物車實現
擴展閱讀
寧波奧德賽優惠價格 2021-03-15 14:26:02
丹尼斯購物卡能掛失么 2021-03-15 14:25:58
淘寶購物指紋驗證失敗 2021-03-15 14:24:44

JSP6簡單購物車實現

發布時間: 2021-03-04 15:23:46

『壹』 用JSP做個網站實現購物車功能

建個購物車對象
把購買好的 商品 放到 一個 集合里

再把 集合 存到 session中
然後 增刪改查 都 操作 session中的 集合
就OK了 不是很難 你試著 寫下 這樣 提高才會快

『貳』 jsp用session做一個簡單的購物車

在 Session 存放一個 Map<int,int> ,Key是商品ID,Value是數量。

『叄』 jsp中購物車實現的思路該如何解決

點擊復商品

  1. 寫庫 頁面展示制 2.商品 新增 插入數據 刪除 移除數據 修改 +直接修改 -判斷數量是否小於1是 刪除數據

  2. 生產訂單 購物車數據 移交訂單詳細表 生成訂單表 移除購物車數據表

以上為簡單邏輯 更具你的需求自行更改

『肆』 在JSP中怎樣實現購物車計數

問題比較怪異~
既然都會做購物車了~那麼計數又又什麼難的?
無非都是統計數字專罷了~
如果是問的購物車中總共又屬多少類商品,那麼只要統計集合中又多少個商品對象就可以了,用count或者size方法(視你所用集合而定)
如果問的是每類商品有多少個,那麼只要再再商品對象中加一個數量的屬性就OK了,無論增加或減小商品數量也只要修改這個屬性的值就可以~

『伍』 如何用java和jsp做一個簡單的購物車

頁面jsp :

<%@pagelanguage="java"contentType="text/html;charset=utf-8"
pageEncoding="utf-8"%>
<%@taglibprefix="c"uri="
<%@tagliburi="

<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""
<htmlxmlns="
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>
<title>易買網-首頁</title>
<linktype="text/css"rel="stylesheet"href="${pageContext.request.contextPath}/css/style.css"/>
<scripttype="text/javascript"src="${pageContext.request.contextPath}/js/jquery-2.1.1.js"></script>
<scripttype="text/javascript">
varcontextPath='${pageContext.request.contextPath}'
</script>
<scripttype="text/javascript"src="${pageContext.request.contextPath}/js/shopping.js"></script>
</head>
<body>
<jsp:includepage="top.jsp"/>
<divid="position"class="wrap">
您現在的位置:<ahref="Home">易買網</a>&gt;購物車
</div>
<divclass="wrap">
<divid="shopping">
<formaction=""method="post">
<table>
<tr>
<th>商品名稱</th>
<th>商品價格</th>
<th>購買數量</th>
<th>操作</th>
</tr>
<c:forEachitems="${sessionScope.shopCar}"var="item"varStatus="status">
<trid="proct_id_${item.proId}">
<tdclass="thumb"><imgsrc="${item.proImg}"height="50"width="30"/><ahref="Proct?action=view&entityId=${item.proId}">${item.proName}</a></td>
<tdclass="price"id="price_id_1">
<span><fmt:formatNumbervalue="${item.proPrice}"type="NUMBER"minFractionDigits="2"/></span>
<inputtype="hidden"value="${item.proPrice}"/>
</td>
<tdclass="number">
<dl>
<dt><spanonclick="sub('number_id_${item.proId}','${item.proId}')">-</span><inputid="number_id_${item.proId}"type="text"readonly="readonly"name="number"value="${item.proNum}"/><spanonclick="addNum('number_id_${item.proId}','${item.proId}')">+</span></dt>

</dl>
</td>
<tdclass="delete"><ahref="javascript:deleteItem('proct_id_${item.proId}','${item.proId}')">刪除</a></td>
</tr>
</c:forEach>
</table>
<divclass="button"><inputtype="submit"value=""/></div>
</form>
</div>
</div>
<divid="footer">
Copyright&;kaka292817678itjob遠標培訓.
</div>
</body>
</html>



頁面關聯的js 自己去網上下載一個jquery
/*數量減少*/
functionsub(id,proId){
//購買數量的值
varnum=$('#'+id).val();
if(num>1){
$('#'+id).val(num-1);
}

edit(id,proId);

}

functionedit(id,proId){
varurl=contextPath+'/HomeCarManager'
//修改後的數量,購物明細的商品的id
num=$('#'+id).val();
$.post(url,{"num":num,"proId":proId},function(msg){
/*
if(msg=='true'){
alert('修改成功');
}else{
alert('修改失敗');
}*/
});
}
/**
*數量增加
*@param{}id
*/
functionaddNum(id,proId){
//購買數量的值
varnum=$('#'+id).val();
$('#'+id).val(parseInt(num)+1);
edit(id,proId);
}

/**
*刪除購物明細
*/
functiondeleteItem(trId,proId){
//
//console.log($("#"+trId));
//js刪除頁面節點
//$("#"+trId).remove();
varurl=contextPath+'/HomeCarManager'
$.post(url,{"proId":proId},function(msg){
if(msg=='true'){
//js刪除頁面節點
$("#"+trId).remove();
}
});

}



後台servlet1
packagecom.kaka.web;

importjava.io.IOException;
importjava.io.PrintWriter;
importjava.util.ArrayList;
importjava.util.List;

importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
/**
*購物車處理類
*@author@authorITJob遠標培訓
*
*/

importcom.kaka.entity.Items;
importcom.kaka.entity.Proct;
importcom.kaka.service.ProctService;
importcom.kaka.service.impl.ProctServiceImpl;
{

=1L;
ProctServiceps=newProctServiceImpl();
@Override
protectedvoiddoPost(HttpServletRequestreq,HttpServletResponseresp)throwsServletException,IOException{
//獲取商品的id
StringproId=req.getParameter("proId");
resp.setContentType("text/html;charset=UTF-8");
PrintWriterwriter=resp.getWriter();
if(null!=proId&&!"".equals(proId)){
//返回添加購物車成功
//System.out.println("============="+proId);
//根據商品的id查詢商品
try{
IntegerpId=Integer.parseInt(proId);
Proctproct=ps.findProctById(pId);
if(null!=proct){
//查詢到了商品,將商品的相關參數構建一個購物明細放入到購物車
Itemsit=newItems();
it.setProId(proct.getProId());
it.setProName(proct.getProName());
it.setProPrice(proct.getProPrice());
it.setProImg(proct.getProImg());

//先判斷session范圍是否有購物車
List<Items>shopCar=(List<Items>)req.getSession().getAttribute("shopCar");
if(null==shopCar){
//購物車
shopCar=newArrayList<Items>();
}
//將商品加入到購物車之前,判斷購物車中是否已經包含了該購物明細,如果包含了,只需要修改購買的數量
if(shopCar.contains(it)){
intindex=shopCar.indexOf(it);//尋找購物車中包含購物明細在購物車中位置
Itemsitems=shopCar.get(index);//獲取購物車中存在的購物明細
items.setProNum(items.getProNum()+1);
}else{
shopCar.add(it);
}


//將購物車放入到session訪問
req.getSession().setAttribute("shopCar",shopCar);
//返回
writer.print(true);
}else{
writer.print(false);
}
}catch(Exceptione){
e.printStackTrace();
writer.print(false);
}
}else{
writer.print(false);
}
writer.flush();
writer.close();
}
@Override
protectedvoiddoGet(HttpServletRequestreq,HttpServletResponseresp)throwsServletException,IOException{
doPost(req,resp);
}
}



後台管理servlet
packagecom.kaka.web;

importjava.io.IOException;
importjava.io.PrintWriter;
importjava.util.ArrayList;
importjava.util.List;

importjavax.mail.FetchProfile.Item;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
/**
*購物車修改
*@authorITJob遠標培訓
*
*/

importcom.kaka.entity.Items;
importcom.kaka.entity.Proct;
importcom.kaka.service.ProctService;
importcom.kaka.service.impl.ProctServiceImpl;
{

=1L;
ProctServiceps=newProctServiceImpl();
@Override
protectedvoiddoPost(HttpServletRequestreq,HttpServletResponseresp)throwsServletException,IOException{
resp.setContentType("text/html;charset=UTF-8");
PrintWriterwriter=resp.getWriter();
//獲取參數
StringproId=req.getParameter("proId");
Stringnum=req.getParameter("num");
if(null!=proId&&null!=num
&&!"".equals(proId)&&!"".equals(num)){
try{
IntegerpId=Integer.parseInt(proId);
FloatpNum=Float.parseFloat(num);

//根據商品的id獲取對應的明細項
//先判斷session范圍是否有購物車
List<Items>shopCar=(List<Items>)req.getSession().getAttribute("shopCar");
for(Itemsit:shopCar){
if(it.getProId()==pId){
it.setProNum(pNum);
}
}
writer.print(true);
}catch(Exceptione){
e.printStackTrace();
}
}else{
//刪除的操作
try{
IntegerpId=Integer.parseInt(proId);
//根據商品的id獲取對應的明細項
//先判斷session范圍是否有購物車
List<Items>shopCar=(List<Items>)req.getSession().getAttribute("shopCar");
Itemsitems=null;
for(Itemsit:shopCar){
if(it.getProId()==pId){
items=it;
break;
}
}
if(null!=items){
shopCar.remove(items);
req.getSession().setAttribute("shopCar",shopCar);
}

writer.print(true);
}catch(Exceptione){
e.printStackTrace();
}
}

writer.flush();
writer.close();
}
@Override
protectedvoiddoGet(HttpServletRequestreq,HttpServletResponseresp)throwsServletException,IOException{
doPost(req,resp);
}
}

『陸』 用JSP和sql資料庫做一個簡單的購物車

使用map。我給你,hi我吧

『柒』 JSP,購物車生成訂單,急求代碼!!

// end else if
my_mc.cat._visible =0;
dati = filexml.firstChild.childNodes[categoriasito].childNodes;
for (i=0; i<dati.length; i++) {
容my_mc.cat.plicateMovieClip("cat"+i, i*1+5000);
my_mc["cat"+i].loader.loadMovie(dati[i].attributes.urlimage);
my_mc["cat"+i].input_txt.text = dati[i].attributes.desc;
if (i<colonne) {
my_mc["cat"+i]._y = 0;
my_mc["cat"+i]._x = 105*i+20;
} else {

『捌』 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;
}

}

----------------------------------------------------------------------
上面是一個簡單的例子,功能都能實現,對網頁效果要求更漂亮些的可做一些修改。

『玖』 求ejb+jsp實現簡單購物車功能的代碼

修改下述的例子,增加web功能

(1)提供登錄頁面Login.jsp;
只需要登錄名與密碼(可在程序中硬編碼)
(2)圖書列表頁面
列出所有可出售的圖書供用戶選擇;
圖書只需給出圖書名(可在程序中硬編碼)
(3)提供購物車商品的管理頁面:
能夠向購物車中添加圖書(從圖書列表中選擇);
能夠刪除購物車中的圖書;
能夠察看購物車中的圖書信息
(4)用戶可主動退出系統

一個EJB例子如下(購物車)
import java.util.*;
import javax.ejb.Stateful;
import javax.ejb.*;

@Stateful(mappedName="cart")
public class CartBean implements Cart{
String customerName;
String customerId;
List<String> contents;
//ArrayList<String> contents;

public void initialize(String person) throws BookException {
if (person == null) {
throw new BookException("不允許沒有用戶!");
} else {
customerName = person;
}
customerId = "0";
contents = new ArrayList<String>();
}

public void initialize(String person, String id)
throws BookException {
if (person == null) {
throw new BookException("不允許沒有用戶!");
} else {
customerName = person;
}
IdVerifier idChecker = new IdVerifier();
if (idChecker.validate(id)) {
customerId = id;
} else {
throw new BookException("無效的ID: " + id);
}
contents = new ArrayList<String>();
}

public void addBook(String title) {
contents.add(title);
}
public void removeBook(String title) throws BookException {
boolean result = contents.remove(title);
if (result == false) {
throw new BookException(title + " 不在購物車中。");
}
}
public List<String> getContents() {
return contents;
}
@Remove()
public void remove() {
contents = null;
}
}

『拾』 jsp購物車是怎麼實現的,請具體解釋下,不要代碼,要用自己的話說

其實很簡單,就是復每次選擇制了商品之後保存起來就可以了
頁面的話可以將商品id用ajax傳給後台,而頁面要保存一個map,key是當前登錄用戶,從session中得到,value則是個list,list存商品相關信息,包括數量,價格等。