业务逻辑图,简单版要写各个Servlet

//ChangeCartCountServlet 使用ajax实现数量,增加或减少;

package com.cart.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cart.dao.BookDao;
import com.cart.dao.impl.BookDaoImpl;
import com.cart.entity.Book;
import com.cart.entity.CartItem;
import com.cart.entity.Userinfo;
import com.cart.service.CartService;

public class ChangeCartCountServlet extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		resp.setContentType("text/html;charset=utf-8");
		PrintWriter out = resp.getWriter();

		int bookid = Integer.parseInt(req.getParameter("bookid"));
		int count = Integer.parseInt(req.getParameter("count"));
		CartService cartService=new CartService();
		Userinfo userinfo=(Userinfo)req.getSession().getAttribute("userinfo");
		int uid=userinfo.getId();//获得当前用户id编号;

		CartItem cartItem=cartService.findCartItemById(uid,bookid);
		if(cartItem.getCount()<count){//如果根据总数量小于你增加的总数量,则不能购物,业务正确;
			out.print("false");
			return;
		}

		Map<Integer,CartItem> cart = (Map<Integer,CartItem>)req.getSession().getAttribute("cart");
		int result=-1;
		if(cart!=null){
			cartItem = cart.get(bookid); //获得购物车项
			cartItem.setCount(count);
			cart.put(bookid, cartItem);

			result=cartService.updateCartItemCount(cartItem);
		}
		if(result>0){
			out.print("true");
		}else{
			out.print("false");
		}

		out.flush();
		out.close();
	}
}
package com.cart.web;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.cart.entity.CartItem;
import com.cart.entity.Userinfo;
import com.cart.service.CartService;

public class ShowBookServlet extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		HttpSession session=req.getSession();
		Userinfo user=(Userinfo)session.getAttribute("userinfo");
		int uid=user.getId();
		CartService cartService=new CartService();
		Map<Integer,CartItem> cart = cartService.getCartItem(uid);

		session.setAttribute("cart", cart); //加入session用jstl读取;
		resp.sendRedirect("show_cart.jsp");

	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doGet(req, resp);
	}
}

package com.cart.web;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.cart.entity.Book;
import com.cart.entity.CartItem;
import com.cart.entity.Userinfo;
import com.cart.service.CartService;

public class DeleteCartServlet extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		HttpSession session=req.getSession();
		Userinfo user=(Userinfo)session.getAttribute("userinfo");
		int uid=user.getId();
		int bookid = Integer.parseInt(req.getParameter("bookid"));

				Map<Integer,CartItem> cart = (Map<Integer,CartItem>)req.getSession().getAttribute("cart");
				// 根据key(bookid)删除
				cart.remove(bookid);
				//需要根据carService进行操作;
				CartService cartService=new CartService();
				cartService.deletecartItem(uid, bookid);

				req.getSession().setAttribute("cart", cart);

				resp.sendRedirect("show_cart.jsp");
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doGet(req, resp);
	}
}

jsp电子商务购物车之五 数据库存储篇2的更多相关文章

  1. jsp电子商务购物车之四 数据库存储篇

    为了方便用户下次登录,仍然可以看到自己的购物车内容,所以,需要在数据库存储相应的购物车项目,本处增加购物车项表;uid和bid是复合主键. package com.cart.entity; //购物车 ...

  2. jsp电子商务 购物车实现之一 设计篇

    购物车的功能实现. 查询的资料,找到三种方法: 1.用cookie实现购物车: 2.用session实现购物车: 3.用cookie和数据库(购物车信息持久化)实现购物车: ============= ...

  3. jsp电子商务 购物车实现之二 登录和分页篇

    登录页面核心代码 <div id="login"> <h2>用户登陆</h2> <form method="post" ...

  4. jsp电子商务 购物车实现之三 购物车

    CartServlet参考代码 : public void doPost(HttpServletRequest req, HttpServletResponse resp) throws Servle ...

  5. android之存储篇——SQLite数据库

    转载:android之存储篇_SQLite数据库_让你彻底学会SQLite的使用 SQLite最大的特点是你可以把各种类型的数据保存到任何字段中,而不用关心字段声明的数据类型是什么. 例如:可以在In ...

  6. Spring Boot干货系列:(八)数据存储篇-SQL关系型数据库之JdbcTemplate的使用

    Spring Boot干货系列:(八)数据存储篇-SQL关系型数据库之JdbcTemplate的使用 原创 2017-04-13 嘟嘟MD 嘟爷java超神学堂 前言 前面几章介绍了一些基础,但都是静 ...

  7. 时序数据库深入浅出之存储篇——本质LSMtree,同时 metric(比如温度)+tags 分片

    什么是时序数据库 先来介绍什么是时序数据.时序数据是基于时间的一系列的数据.在有时间的坐标中将这些数据点连成线,往过去看可以做成多纬度报表,揭示其趋势性.规律性.异常性:往未来看可以做大数据分析,机器 ...

  8. DDD开发框架ABP之本地化资源的数据库存储扩展

    在上一篇<DDD开发框架ABP之本地化/多语言支持>中,我们知道,ABP开发框架中本地化资源存储可以采用XML文件,RESX资源文件,也提供了其他自定义的存储方式的扩展接口.ABP框架默认 ...

  9. Atitit.数据库存储引擎的原理与attilax 总结

    Atitit.数据库存储引擎的原理与attilax 总结 1. 存储引擎是什么1 2. 其它数据库系统(包括大多数商业选择)仅支持一种类型的数据存储2 3. 表的存储有三个文件:结构+数据+索引2 4 ...

随机推荐

  1. Python内嵌函数与Lambda表达式

    //2018.10.29 内嵌函数与lambda 表达式 1.如果在内嵌函数中需要改变全局变量的时候需要用到global语句对于变 量进行一定的说明与定义 2.内部的嵌套函数不可以直接在外部进行访问 ...

  2. SpriteKit手机游戏摇杆JoyStick的使用 -- by iFIERO游戏开发教程

    ### 工欲善其事,必先利其器 有时候学习如何应用第三方库是非常重要的,因为我们不用再自己重复造轮子,在这里,我们就把原先利用重力感应来操控飞机改为用游戏摇杆joystick来操控,具体的操作如下:` ...

  3. Java开发工程师(Web方向) - 03.数据库开发 - 期末考试

    期末考试 编程题 本编程题包含4个小题,覆盖知识点从基础的JDBC.连接池到MyBatis. 1(10分) 有一款在线教育产品“天天向上”主要实现了在手机上查看课程表的功能.该产品的后端系统有一张保存 ...

  4. NO.01---今天聊聊Vuex的简单入门

    作为一款个人认为非常牛x的框架,个人使用起来得心应手,所以近期就记录一下这款框架吧. 首先说一说 Vuex 是什么? 官方给出的解释:Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它 ...

  5. Spring Cloud(五):Hystrix 监控面板【Finchley 版】

    Spring Cloud(五):Hystrix 监控面板[Finchley 版]  发表于 2018-04-16 |  更新于 2018-05-10 |  在上一篇 Hystrix 的介绍中,我们提到 ...

  6. hdu刷题2

    hdu1021 给n,看费波纳列数能否被3整除 算是找规律吧,以后碰到这种题就打打表找找规律吧 #include <stdio.h> int main(void) { int n; whi ...

  7. Windows环境下使用kafka单机模式

    测试运行环境 Win10 kafka_2.11-1.0.0 zookeeper-3.4.10 1.安装Zookeeper Kafka的运行依赖于Zookeeper,所以在运行Kafka之前我们需要安装 ...

  8. springMVC对jsp页面的数据进行校验

    一. 使用注解校验 a) 引入校验依赖包 <dependency> <groupId>javax.validation</groupId> <artifact ...

  9. Cassandra 类型转换限制

    原文地址:http://stackoverflow.com/questions/31880381/cassandra-alter-column-type-which-types-are-compati ...

  10. 如何重新安装Orchard CMS?

    Orchard CMS安装后,配置文件和数据库保存在App_Data目录中.这个目录是受保护的,是不能通过网址访问到的. 如果要完全重装你的站点,你可以删除此目录中的所有文件,但是最好先备份!删除后重 ...