當前位置:編程學習大全網 - 源碼下載 - 如何用java和jsp做壹個簡單的購物車

如何用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?.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..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範圍是否有購物車

List<Items>?shopCar?=?(List<Items>)req.getSession().getAttribute("shopCar");

if(null?==?shopCar){

//購物車

shopCar?=?new?ArrayList<Items>();

}

//將商品加入到購物車之前,判斷購物車中是否已經包含了該購物明細,如果包含了,只需要修改購買的數量

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..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範圍是否有購物車

List<Items>?shopCar?=?(List<Items>)?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範圍是否有購物車

List<Items>?shopCar?=?(List<Items>)?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);

}

}

  • 上一篇:股利支付率計算公式(股息率和股利支付率)
  • 下一篇:導航指南頁面源代碼
  • copyright 2024編程學習大全網