本文目錄一覽:
- 1、用jsp和數據庫做購物車,怎麼能通過點擊按鈕把購買數量和商品信息傳給購物車頁面,急!!下面是部分代碼
- 2、javascript+jsp實現在1.html把商品放購物車,在2.html顯示購物車內的信息.看問題補充
- 3、如何用java和jsp做一個簡單的購物車
- 4、jsp 實現簡易版session購物車,無論前面怎麼選擇,最後顯示的結果都為6個on,無法正確顯示選擇結果
用jsp和數據庫做購物車,怎麼能通過點擊按鈕把購買數量和商品信息傳給購物車頁面,急!!下面是部分代碼
你把購買的數量和商品信息做成一個javabean,然後把這個javabean存在session裡面,你點擊按鈕就向服務器端發出請求,然後服務器端處理請求後用jsp顯示,這樣就可以了呀
javascript+jsp實現在1.html把商品放購物車,在2.html顯示購物車內的信息.看問題補充
一般來說,購物車信息是放在數據庫的。不建議放在session。添加購物車就向數據庫添加一條數據,另外一個頁面刷新自然就可以獲取數據
如何用java和jsp做一個簡單的購物車
頁面jsp :
%@ page language=”java” contentType=”text/html; charset=utf-8″
pageEncoding=”utf-8″%
%@ taglib prefix=”c” uri=”
%@ taglib uri=”
!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “
html xmlns=”
head
meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ /
title易買網 – 首頁/title
link type=”text/css” rel=”stylesheet” href=”${pageContext.request.contextPath }/css/style.css” /
script type=”text/javascript” src=”${pageContext.request.contextPath }/js/jquery-2.1.1.js”/script
script type=”text/javascript”
var contextPath = ‘${pageContext.request.contextPath }’;
/script
script type=”text/javascript” src=”${pageContext.request.contextPath }/js/shopping.js”/script
/head
body
jsp:include page=”top.jsp” /
div id=”position” class=”wrap”
您現在的位置:a href=”Home”易買網/a gt; 購物車
/div
div class=”wrap”
div id=”shopping”
form action=”” method=”post”
table
tr
th商品名稱/th
th商品價格/th
th購買數量/th
th操作/th
/tr
c:forEach items=”${sessionScope.shopCar}” var=”item” varStatus=”status”
tr id=”product_id_${item.proId}”
td class=”thumb”img src=”${item.proImg }” height=”50″ width=”30″ /a href=”Product?action=viewentityId=${item.proId}”${item.proName}/a/td
td class=”price” id=”price_id_1″
spanfmt:formatNumber value=”${item.proPrice}” type=”NUMBER” minFractionDigits=”2″ //span
input type=”hidden” value=”${item.proPrice}” /
/td
td class=”number”
dl
dtspan onclick=”sub(‘number_id_${item.proId}’,’${item.proId}’)”-/spaninput id=”number_id_${item.proId}” type=”text” readonly=”readonly” name=”number” value=”${item.proNum}” /span onclick=”addNum(‘number_id_${item.proId}’,’${item.proId}’)”+/span/dt
/dl
/td
td class=”delete”a href=”javascript:deleteItem(‘product_id_${item.proId}’,’${item.proId}’)”刪除/a/td
/tr
/c:forEach
/table
div class=”button”input type=”submit” value=”” //div
/form
/div
/div
div id=”footer”
Copyright copy; kaka 292817678 itjob 遠標培訓.
/div
/body
/html
頁面關聯的js 自己去網上下載一個jquery
/*數量減少*/
function sub(id,proId){
//購買數量的值
var num = $(‘#’+id).val();
if(num 1){
$(‘#’+id).val(num – 1);
}
edit(id,proId);
}
function edit(id,proId){
var url = contextPath + ‘/HomeCarManager’;
//修改後的數量,購物明細的商品的id
num = $(‘#’+id).val();
$.post(url,{“num”:num,”proId”:proId},function (msg){
/*
if(msg == ‘true’){
alert(‘修改成功’);
} else {
alert(‘修改失敗’);
}*/
});
}
/**
* 數量增加
* @param {} id
*/
function addNum(id,proId){
//購買數量的值
var num = $(‘#’+id).val();
$(‘#’+id).val(parseInt(num) + 1);
edit(id,proId);
}
/**
* 刪除購物明細
*/
function deleteItem(trId,proId){
//
//console.log($(“#”+trId));
//js刪除頁面節點
//$(“#”+trId).remove();
var url = contextPath + ‘/HomeCarManager’;
$.post(url,{“proId”:proId},function (msg){
if(msg == ‘true’){
//js刪除頁面節點
$(“#”+trId).remove();
}
});
}
後台servlet1
package com.kaka.web;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 購物車處理類
* @author @author ITJob 遠標培訓
*
*/
import com.kaka.entity.Items;
import com.kaka.entity.Product;
import com.kaka.service.ProductService;
import com.kaka.service.impl.ProductServiceImpl;
public class HomeCar extends HttpServlet {
private static final long serialVersionUID = 1L;
ProductService ps = new ProductServiceImpl();
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//獲取商品的id
String proId = req.getParameter(“proId”);
resp.setContentType(“text/html;charset=UTF-8”);
PrintWriter writer = resp.getWriter();
if(null != proId !””.equals(proId)){
//返回添加購物車成功
//System.out.println(“=============” + proId);
//根據商品的id查詢商品
try {
Integer pId = Integer.parseInt(proId);
Product product = ps.findProductById(pId);
if(null != product){
//查詢到了商品,將商品的相關參數構建一個購物明細放入到購物車
Items it = new Items();
it.setProId(product.getProId());
it.setProName(product.getProName());
it.setProPrice(product.getProPrice());
it.setProImg(product.getProImg());
//先判斷session範圍是否有購物車
ListItems shopCar = (ListItems)req.getSession().getAttribute(“shopCar”);
if(null == shopCar){
//購物車
shopCar = new ArrayListItems();
}
//將商品加入到購物車之前,判斷購物車中是否已經包含了該購物明細,如果包含了,只需要修改購買的數量
if(shopCar.contains(it)){
int index = shopCar.indexOf(it);//尋找購物車中包含購物明細在購物車中位置
Items items = 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 (Exception e) {
e.printStackTrace();
writer.print(false);
}
} else {
writer.print(false);
}
writer.flush();
writer.close();
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
}
後台管理servlet
package com.kaka.web;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.mail.FetchProfile.Item;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 購物車修改
* @author ITJob 遠標培訓
*
*/
import com.kaka.entity.Items;
import com.kaka.entity.Product;
import com.kaka.service.ProductService;
import com.kaka.service.impl.ProductServiceImpl;
public class HomeCarManager extends HttpServlet {
private static final long serialVersionUID = 1L;
ProductService ps = new ProductServiceImpl();
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType(“text/html;charset=UTF-8”);
PrintWriter writer = resp.getWriter();
//獲取參數
String proId = req.getParameter(“proId”);
String num = req.getParameter(“num”);
if(null != proId null != num
!””.equals(proId) !””.equals(num)){
try {
Integer pId = Integer.parseInt(proId);
Float pNum = Float.parseFloat(num);
//根據商品的id獲取對應的明細項
// 先判斷session範圍是否有購物車
ListItems shopCar = (ListItems) req.getSession().getAttribute(“shopCar”);
for(Items it : shopCar){
if(it.getProId()== pId){
it.setProNum(pNum);
}
}
writer.print(true);
} catch (Exception e) {
e.printStackTrace();
}
} else {
//刪除的操作
try {
Integer pId = Integer.parseInt(proId);
//根據商品的id獲取對應的明細項
// 先判斷session範圍是否有購物車
ListItems shopCar = (ListItems) req.getSession().getAttribute(“shopCar”);
Items items = null;
for(Items it : shopCar){
if(it.getProId()== pId){
items = it;
break;
}
}
if(null != items){
shopCar.remove(items);
req.getSession().setAttribute(“shopCar”,shopCar);
}
writer.print(true);
} catch (Exception e) {
e.printStackTrace();
}
}
writer.flush();
writer.close();
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
}
jsp 實現簡易版session購物車,無論前面怎麼選擇,最後顯示的結果都為6個on,無法正確顯示選擇結果
session.setAttribute(這個裡面放List),
你有多少個商品全放在list裡面就行了。
然後購物車展示頁面就循環輸出list
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/254473.html