package com.itcast.web;
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.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.google.gson.Gson;
import com.itcast.domain.Cart;
import com.itcast.domain.CartItem;
import com.itcast.domain.Category;
import com.itcast.domain.PageBean;
import com.itcast.domain.Product;
import com.itcast.service.ProductService;
public class ProductServlet extends BaseServlet {
// public void doGet(HttpServletRequest request, HttpServletResponse response)
// throws ServletException, IOException {
// String method = request.getParameter("method");
// if("category".equals(method)){
// category(request, response);
// } else if("index".equals(method)){
// index(request, response);
// } else if("productListByCid".equals(method)){
// productListByCid(request, response);
// } else if("findProductByPid".equals(method)){
// findProductByPid(request, response);
// }
//
// }
//
// public void doPost(HttpServletRequest request, HttpServletResponse response)
// throws ServletException, IOException {
// // TODO Auto-generated method stub
// doGet(request, response);
// }
public void clearCart(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
session.removeAttribute("cart");
response.sendRedirect(request.getContextPath());
}
public void delProFromCart(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String pid = request.getParameter("pid");
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);
}
session.setAttribute("cart", cart);
response.sendRedirect(request.getContextPath()+"/cart.jsp");
}
public void addProductToCart(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
String pid = request.getParameter("pid");
String buyNumStr = request.getParameter("buyNum");
int buyNum = Integer.parseInt(buyNumStr);
ProductService service = new ProductService();
Product product = service.findProductByPid(pid);
double subtotal = buyNum*product.getShop_price();
CartItem cartItem = new CartItem();
cartItem.setBuyNum(buyNum);
cartItem.setProduct(product);
cartItem.setSubtotal(subtotal);
Cart cart = (Cart) session.getAttribute("cart");
if(cart==null){
cart=new Cart();
}
Map<String, CartItem> cartItems = cart.getCartItems();
double newsubtotal = 0.0;
if(cartItems.containsKey(pid)){
//取出原有商品的数量
CartItem item = cartItems.get(pid);
int oldBuyNum = item.getBuyNum();
oldBuyNum+=buyNum;
item.setBuyNum(oldBuyNum);
cart.setCartItems(cartItems);
//修改小计
//原来该商品的小计
double oldsubtotal = item.getSubtotal();
//新买的商品的小计
newsubtotal = buyNum*product.getShop_price();
item.setSubtotal(oldsubtotal+newsubtotal);
}else{
//如果车中没有该商品
cart.getCartItems().put(product.getPid(), cartItem);
newsubtotal = buyNum*product.getShop_price();
}
//计算总计
double total = cart.getTotal()+newsubtotal;
cart.setTotal(total);
//将车再次访问session
session.setAttribute("cart", cart);
// request.getRequestDispatcher("/cart.jsp").forward(request, response);
response.sendRedirect(request.getContextPath()+"/cart.jsp");
}
public void category(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ProductService service = new ProductService();
List<Category> cetegory = service.getCategory();
response.setContentType("text/html;charset=UTF-8");
Gson gson = new Gson();
String categoryList = gson.toJson(cetegory);
// System.out.println(categoryList);
response.getWriter().write(categoryList);
}
public void index(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ProductService service = new ProductService();
// List<Category> cetegory = service.getCategory();
List<Product> hotProduct = service.getHotProduct();
List<Product> newProduct = service.getNewProduct();
request.setAttribute("hotProductList", hotProduct);
request.setAttribute("newProductList", newProduct);
request.getRequestDispatcher("/index.jsp").forward(request, response);
}
public void productListByCid(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String currentPageString = request.getParameter("currentPage");
if(currentPageString==null){
currentPageString="1";
}
int currentPage = Integer.parseInt(currentPageString);
int currentCount =12;
String cid = request.getParameter("cid");
ProductService service = new ProductService();
PageBean pageBean = service.getProductListByCid(cid,currentCount,currentPage);
request.setAttribute("pageBean", pageBean);
request.setAttribute("cid", cid);
List<Product> historyProductList = new ArrayList<Product>();
Cookie[] cookies = request.getCookies();
if(cookies!=null){
for(Cookie cookie:cookies){
if("pids".equals(cookie.getName())){
String pids = cookie.getValue();
String[] split = pids.split("-");
for(String pid:split){
Product pro = service.findProductByPid(pid);
historyProductList.add(pro);
}
}
}
}
request.setAttribute("historyProductList", historyProductList);
request.getRequestDispatcher("/product_list.jsp").forward(request, response);
}
public void findProductByPid(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String pid = request.getParameter("pid");
String cid = request.getParameter("cid");
String currentPage = request.getParameter("currentPage");
ProductService service = new ProductService();
Product product = service .findProductByPid(pid);
request.setAttribute("product", product);
request.setAttribute("cid", cid);
request.setAttribute("currentPage", currentPage);
String pids = pid;
Cookie[] cookies = request.getCookies();
if(cookies!=null){
for(Cookie cookie:cookies){
if("pids".equals(cookie.getName())){
pids = cookie.getValue();
String[] split = pids.split("-");
List<String> asList = Arrays.asList(split);
LinkedList<String> list = new LinkedList<String>(asList);
if(list.contains(pid)){
list.remove(pid);
}
list.addFirst(pid);
StringBuffer sb = new StringBuffer();
for(int i=0;i<list.size()& i<7;i++){
sb.append(list.get(i));
sb.append("-");
}
pids = sb.substring(0, sb.length()-1);
}
}
}
Cookie cookie_new = new Cookie("pids", pids);
response.addCookie(cookie_new);
request.getRequestDispatcher("/product_info.jsp").forward(request, response);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
原生servlet+C3P0数据库连接池搭建的商城系统.zip (400个子文件)
common.css.bak 20KB
ccb.bmp 6KB
cmb.bmp 6KB
zx.bmp 6KB
gf.bmp 6KB
dy.bmp 6KB
pingan.bmp 6KB
cmbc.bmp 6KB
abc.bmp 6KB
post.bmp 6KB
hx.bmp 6KB
sfz.bmp 6KB
bj.bmp 6KB
cib.bmp 6KB
beijingnongshang.bmp 6KB
sh.bmp 6KB
icbc.bmp 6KB
bc.bmp 6KB
ningbo.bmp 6KB
guangda.bmp 6KB
nanjing.bmp 6KB
bcc.bmp 6KB
zheshang.bmp 6KB
bh.bmp 6KB
shpd.bmp 6KB
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
register.css 2KB
slider.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
adidas.gif 2KB
semir.gif 1KB
nike.gif 1KB
globe.gif 1KB
trash.gif 1KB
question.gif 1KB
base.gif 1KB
register.gif 933B
nolines_plus.gif 870B
nolines_minus.gif 861B
login.gif 847B
password.gif 847B
cart1.gif 671B
musicfolder.gif 633B
imgfolder.gif 622B
page.gif 582B
folderopen.gif 376B
folder.gif 372B
message.gif 365B
cd.gif 239B
i_edit.gif 132B
i_del.gif 125B
plus.gif 89B
plusbottom.gif 88B
minus.gif 86B
共 400 条
- 1
- 2
- 3
- 4
资源评论
白话机器学习
- 粉丝: 8726
- 资源: 7682
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功