package com.itheima.web.servlet;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.google.gson.Gson;
import com.itheima.domain.Cart;
import com.itheima.domain.CartItem;
import com.itheima.domain.Category;
import com.itheima.domain.PageBean;
import com.itheima.domain.Product;
import com.itheima.service.ProductService;
import com.itheima.utils.JedisPoolUtils;
import redis.clients.jedis.Jedis;
public class PrdocutServlet extends BaseServlet {
/*public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获得请求的哪个方法的method
String methodName = request.getParameter("method");
if("productList".equals(methodName)){
productList(request,response);
}else if("categoryList".equals(methodName)){
categoryList(request,response);
}else if("index".equals(methodName)){
index(request,response);
}else if("productInfo".equals(methodName)){
productInfo(request,response);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
*/
//清空购物车
public void clearCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
session.removeAttribute("cart");
//跳转回cart.jsp
response.sendRedirect(request.getContextPath()+"/cart.jsp");
}
//删除单一商品
public void delProFromCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获得要删除的item的pid
String pid = request.getParameter("pid");
//删除session中的购物车中的购物项集合中的item
HttpSession session = request.getSession();
Cart cart = (Cart) session.getAttribute("cart");
if(cart!=null){
Map<String, CartItem> cartItems = cart.getCartItems();
//需要修改总价
cart.setTotal(cart.getTotal()-cartItems.get(pid).getSubtotal());
//删除
cartItems.remove(pid);
cart.setCartItems(cartItems);
}
session.setAttribute("cart", cart);
//跳转回cart.jsp
response.sendRedirect(request.getContextPath()+"/cart.jsp");
}
//将商品添加到购物车
public void addProductToCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
ProductService service = new ProductService();
//获得要放到购物车的商品的pid
String pid = request.getParameter("pid");
//获得该商品的购买数量
int buyNum = Integer.parseInt(request.getParameter("buyNum"));
//获得product对象
Product product = service.findProductByPid(pid);
//计算小计
double subtotal = product.getShop_price()*buyNum;
//封装CartItem
CartItem item = new CartItem();
item.setProduct(product);
item.setBuyNum(buyNum);
item.setSubtotal(subtotal);
//获得购物车---判断是否在session中已经存在购物车
Cart cart = (Cart) session.getAttribute("cart");
if(cart==null){
cart = new Cart();
}
//将购物项放到车中---key是pid
//先判断购物车中是否已将包含此购物项了 ----- 判断key是否已经存在
//如果购物车中已经存在该商品----将现在买的数量与原有的数量进行相加操作
Map<String, CartItem> cartItems = cart.getCartItems();
double newsubtotal = 0.0;
if(cartItems.containsKey(pid)){
//取出原有商品的数量
CartItem cartItem = cartItems.get(pid);
int oldBuyNum = cartItem.getBuyNum();
oldBuyNum+=buyNum;
cartItem.setBuyNum(oldBuyNum);
cart.setCartItems(cartItems);
//修改小计
//原来该商品的小计
double oldsubtotal = cartItem.getSubtotal();
//新买的商品的小计
newsubtotal = buyNum*product.getShop_price();
cartItem.setSubtotal(oldsubtotal+newsubtotal);
}else{
//如果车中没有该商品
cart.getCartItems().put(product.getPid(), item);
newsubtotal = buyNum*product.getShop_price();
}
//计算总计
double total = cart.getTotal()+newsubtotal;
cart.setTotal(total);
//将车再次访问session
session.setAttribute("cart", cart);
//直接跳转到购物车页面
response.sendRedirect(request.getContextPath()+"/cart.jsp");
}
//显示商品的类别的的功能
public void categoryList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ProductService service = new ProductService();
//先从缓存中查询categoryList 如果有直接使用 没有在从数据库中查询 存到缓存中
//1、获得jedis对象 连接redis数据库
Jedis jedis = JedisPoolUtils.getJedis();
String categoryListJson = jedis.get("categoryListJson");
//2、判断categoryListJson是否为空
if(categoryListJson==null){
System.out.println("缓存没有数据 查询数据库");
//准备分类数据
List<Category> categoryList = service.findAllCategory();
Gson gson = new Gson();
categoryListJson = gson.toJson(categoryList);
jedis.set("categoryListJson", categoryListJson);
}
response.setContentType("text/html;charset=UTF-8");
response.getWriter().write(categoryListJson);
}
//显示首页的功能
//显示商品的类别的的功能
public void index(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ProductService service = new ProductService();
//准备热门商品---List<Product>
List<Product> hotProductList = service.findHotProductList();
//准备最新商品---List<Product>
List<Product> newProductList = service.findNewProductList();
//准备分类数据
//List<Category> categoryList = service.findAllCategory();
//request.setAttribute("categoryList", categoryList);
request.setAttribute("hotProductList", hotProductList);
request.setAttribute("newProductList", newProductList);
request.getRequestDispatcher("/index.jsp").forward(request, response);
}
//显示商品的详细信息功能
public void productInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获得当前页
String currentPage = request.getParameter("currentPage");
//获得商品类别
String cid = request.getParameter("cid");
//获得要查询的商品的pid
String pid = request.getParameter("pid");
ProductService service = new ProductService();
Product product = service.findProductByPid(pid);
request.setAttribute("product", product);
request.setAttribute("currentPage", currentPage);
request.setAttribute("cid", cid);
//获得客户端携带cookie---获得名字是pids的cookie
String pids = pid;
Cookie[] cookies = request.getCookies();
if(cookies!=null){
for(Cookie cookie : cookies){
if("pids".equals(cookie.getName())){
pids = cookie.getValue();
//1-3-2 本次访问商品pid是8----->8-1-3-2
//1-3-2 本次访问商品pid是3----->3-1-2
//1-3-2 本次访问商品pid是2----->2-1-3
//将pids拆成一个数组
String[] split = pids.split("-");//{3,1,2}
List<String> asList = Arrays.asList(split);//[3,1,2]
LinkedList<String> list = new LinkedList<String>(asList);//[3,1,2]
//判断集合中是否存在当前pid
if(list.contains(pid)){
//包含当前查看商品的pid
list.remove(pid);
list.addFirst(pid);
}else{
//不包含当前查看商品的pid �
没有合适的资源?快使用搜索试试~ 我知道了~
源代码-网上商城之HeimaShop-2.rar
共462个文件
jpg:180个
gif:65个
png:58个
需积分: 5 0 下载量 133 浏览量
2023-08-20
08:56:10
上传
评论
收藏 26.84MB RAR 举报
温馨提示
一、功能的演示 1.用户模块 用户注册功能 发送邮件 激活用户 表单的校验 用户的登录功能 自动登录 用户的注销功能 2.商品模块 首页热门商品和最新商品功能 商品分类 商品的列表(分页) 商品详细信息 浏览记录功能 3.购物车模块 将商品加入购物车 展示购物车功能 4.订单模块(多表和事务) 提交订单 展示订单 在线支付。。 5.后台的分类的模块 分类信息的增删改查 6.后台的商品模块 商品的信息的增删改查(文件上传) 7.后台的订单的模块 所有的订单的信息展示。。 二、一般项目开发流程(公司之间是有差异) 1)确定项目需求-----拿下一个项目 2)编写《需求说明书》----不涉及技术,只涉及业务需求 3)编写《概要设计说明书》----- 涉及技术的的宏观的内容,数据库设计,页面原型 4)编写《详细设计说明书》----- 相当于伪代码 5)编码阶段coding----根据《详细设计说明书》--- 单元测试 6)联测-----项目组内部的行为 7)测试组进行全面的专业测试----《测试报告》 8)上线(测试阶段) 9)维护和二次开发 三、网上商城项目的环境的搭建 (1)创建项
资源推荐
资源详情
资源评论
收起资源包目录
源代码-网上商城之HeimaShop-2.rar (462个子文件)
common.css.bak 20KB
dy.bmp 6KB
sh.bmp 6KB
zx.bmp 6KB
pingan.bmp 6KB
cmb.bmp 6KB
hx.bmp 6KB
cib.bmp 6KB
sfz.bmp 6KB
gf.bmp 6KB
beijingnongshang.bmp 6KB
abc.bmp 6KB
cmbc.bmp 6KB
bj.bmp 6KB
icbc.bmp 6KB
bcc.bmp 6KB
post.bmp 6KB
guangda.bmp 6KB
ccb.bmp 6KB
ningbo.bmp 6KB
bc.bmp 6KB
shpd.bmp 6KB
bh.bmp 6KB
nanjing.bmp 6KB
zheshang.bmp 6KB
PrdocutServlet.class 9KB
RegisterServlet.class 3KB
ProductDao.class 3KB
ProductService.class 3KB
Product.class 2KB
User.class 2KB
JedisPoolUtils.class 2KB
DataSourceUtils.class 2KB
UserDao.class 2KB
MailUtils.class 2KB
PageBean.class 2KB
CheckUsernameServlet.class 2KB
MD5Utils.class 2KB
BaseServlet.class 2KB
ActiveServlet.class 1KB
UserService.class 1KB
RegisterServlet$1.class 1KB
Cart.class 1KB
CartItem.class 992B
Category.class 719B
MailUtils$1.class 672B
CommonsUtils.class 468B
.classpath 841B
org.eclipse.wst.common.component 479B
org.eclipse.wst.jsdt.ui.superType.container 49B
bootstrap.css 144KB
bootstrap.min.css 120KB
bootstrap.min.css 120KB
product.css 29KB
bootstrap-theme.css 26KB
bootstrap-theme.min.css 23KB
common.css 20KB
Style1.css 13KB
index.css 9KB
datepicker.css 5KB
cart.css 3KB
slider.css 2KB
register.css 2KB
login.css 2KB
dtree.css 795B
style.css 786B
left.css 220B
glyphicons-halflings-regular.eot 20KB
common.gif 14KB
common.gif 14KB
loading_bar.gif 11KB
product.gif 9KB
product.gif 9KB
product.gif 9KB
logo1.gif 8KB
logo.gif 8KB
member.gif 7KB
review.gif 7KB
cart.gif 6KB
cart.gif 6KB
index.gif 5KB
logo.gif 3KB
logo.gif 3KB
article.gif 3KB
info.gif 3KB
gif53_029.gif 3KB
finalbutton.gif 3KB
septwolves.gif 2KB
eifini.gif 2KB
yishion.gif 2KB
sentubila.gif 2KB
vimly.gif 2KB
loading_icon.gif 2KB
hengyuanxiang.gif 2KB
beijirong.gif 2KB
sundance.gif 2KB
jackjones.gif 2KB
order.gif 2KB
maoren.gif 2KB
lining.gif 2KB
共 462 条
- 1
- 2
- 3
- 4
- 5
资源评论
常驻客栈
- 粉丝: 1w+
- 资源: 1366
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功