业务逻辑图,简单版要写各个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. Linux命令应用大词典-第 15章 文件、目录权限和属性

    15.1 chmod:更改文件和目录的模式 15.2 chown:更改文件和目录的用户所有者和组群所有者 15.3 chgrp:更改文件或目录的所属组 15.4 umask:显示和设置文件及目录创建默 ...

  2. word record 4

    word record 4 pledge p le g vt. 保证,许诺 snowflake falke->n. 小薄片:火花 deputy de piu ti n. 代理人,代表 etch ...

  3. CF245H Queries for Number of Palindromes

    题目描述 给你一个字符串s由小写字母组成,有q组询问,每组询问给你两个数,l和r,问在字符串区间l到r的字串中,包含多少回文串. 时空限制 5000ms,256MB 输入格式 第1行,给出s,s的长度 ...

  4. Redis命令续

     Redis 集合命令 下表列出了 Redis 集合基本命令: 序号 命令及描述 1 SADD key member1 [member2] 向集合添加一个或多个成员 2 SCARD key 获取集合的 ...

  5. 孤荷凌寒自学python第八十五天配置selenium并进行模拟浏览器操作1

    孤荷凌寒自学python第八十五天配置selenium并进行模拟浏览器操作1 (完整学习过程屏幕记录视频地址在文末) 要模拟进行浏览器操作,只用requests是不行的,因此今天了解到有专门的解决方案 ...

  6. CDH组件目录\主机资源分配\端口

    目录: /var/log/cloudera-scm-installer : 安装日志目录. /var/log/* : 相关日志文件(相关服务的及CM的). /usr/share/cmf/ : 程序安装 ...

  7. 【转】CentOS: 开放80、22、3306端口操作

    #/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT#/sbin/iptables -I INPUT -p tcp --dport 22 -j AC ...

  8. Centos7 下nginx nginx-1.13.4 安装

    环境:CentOS Linux release 7.3.1611 (Core)  Linux localhost.localdomain 3.10.0-514.26.2.el7.x86_64 #1 S ...

  9. Automatic Judge

    Description Welcome to HDU to take part in the second CCPC girls’ competition! A new automatic judge ...

  10. 20172330 2017-2018-1 《Java程序设计》第九周学习总结

    20172330 2017-2018-1 <程序设计与数据结构>第九周学习总结 教材学习内容总结 本周的学习包括两章内容,分别为异常和递归. 异常 错误和异常都是对象,代表非正常情况或者无 ...