jsp电子商务购物车之五 数据库存储篇2
业务逻辑图,简单版要写各个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的更多相关文章
- jsp电子商务购物车之四 数据库存储篇
为了方便用户下次登录,仍然可以看到自己的购物车内容,所以,需要在数据库存储相应的购物车项目,本处增加购物车项表;uid和bid是复合主键. package com.cart.entity; //购物车 ...
- jsp电子商务 购物车实现之一 设计篇
购物车的功能实现. 查询的资料,找到三种方法: 1.用cookie实现购物车: 2.用session实现购物车: 3.用cookie和数据库(购物车信息持久化)实现购物车: ============= ...
- jsp电子商务 购物车实现之二 登录和分页篇
登录页面核心代码 <div id="login"> <h2>用户登陆</h2> <form method="post" ...
- jsp电子商务 购物车实现之三 购物车
CartServlet参考代码 : public void doPost(HttpServletRequest req, HttpServletResponse resp) throws Servle ...
- android之存储篇——SQLite数据库
转载:android之存储篇_SQLite数据库_让你彻底学会SQLite的使用 SQLite最大的特点是你可以把各种类型的数据保存到任何字段中,而不用关心字段声明的数据类型是什么. 例如:可以在In ...
- Spring Boot干货系列:(八)数据存储篇-SQL关系型数据库之JdbcTemplate的使用
Spring Boot干货系列:(八)数据存储篇-SQL关系型数据库之JdbcTemplate的使用 原创 2017-04-13 嘟嘟MD 嘟爷java超神学堂 前言 前面几章介绍了一些基础,但都是静 ...
- 时序数据库深入浅出之存储篇——本质LSMtree,同时 metric(比如温度)+tags 分片
什么是时序数据库 先来介绍什么是时序数据.时序数据是基于时间的一系列的数据.在有时间的坐标中将这些数据点连成线,往过去看可以做成多纬度报表,揭示其趋势性.规律性.异常性:往未来看可以做大数据分析,机器 ...
- DDD开发框架ABP之本地化资源的数据库存储扩展
在上一篇<DDD开发框架ABP之本地化/多语言支持>中,我们知道,ABP开发框架中本地化资源存储可以采用XML文件,RESX资源文件,也提供了其他自定义的存储方式的扩展接口.ABP框架默认 ...
- Atitit.数据库存储引擎的原理与attilax 总结
Atitit.数据库存储引擎的原理与attilax 总结 1. 存储引擎是什么1 2. 其它数据库系统(包括大多数商业选择)仅支持一种类型的数据存储2 3. 表的存储有三个文件:结构+数据+索引2 4 ...
随机推荐
- hdu2037今年暑假不AC(贪心,活动安排问题)
今年暑假不AC Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submi ...
- selenium,unittest——两个class连续运行
将多个class放在一个文件内一起运行,这是一个多用例不同网站进行测试的方法 #encoding=utf-8from selenium import webdriverimport time,unit ...
- Python文件操作大全
Python 编程文件操作大全 文件打开模式 打开模式 执行操作 'r' 以只读方式打开文件(默认) 'w' 以写入的方式打开文件,会覆盖已存在的文件 'x' 如果文件已经存在,使用此模式打开将引 ...
- TW实习日记:第18天
今天的bug没有那么多了,都是些小bug,一下就改好了.或者是接口那边数据返回的有问题,通知一下同事就ok了.主要今天是在赶功能进度,然而有一个功能模块需求里并没有写,实在是不知道要做成什么样子,真的 ...
- JS的六大对象:Global、Math、Number、Date、JSON、console,运行在服务器上方的支持情况分析
在ASP中使用runat="server"来调用JS的相关函数,代码如下: <script runat="server" language="j ...
- Hadoop第二课:Hadoop集群环境配置
一.Yum配置 1.检查Yum是否安装 rpm -qa|grep yum 2.修改yum源,我使用的是163的镜像源(http://mirrors.163.com/),根据自己的系统选择源, #进入目 ...
- python leveldb 文档
标签(空格分隔): python leveldb import leveldb db = leveldb.LevelDB('./db') db.Put('hello', 'world') print ...
- scatter注记词2
couch ranch bind ski extra bring note embrace tape they stick legend
- Codeforces 96D Volleyball(最短路径)
Petya loves volleyball very much. One day he was running late for a volleyball match. Petya hasn't b ...
- 在linux下PHP和Mysql环境搞事情
研发部门同事开发了一个接口管理辅助工具Shepherd,要求搭建在内网环境中,遇到点小问题记一下. 将开发的文件上传只web目录下,更改数据库ip,可以正常打开 登陆用户信息,此时需要连接数据库来验证 ...