基于javaweb的家居购物商城系统(java+html+jdbc+mysql)
一、项目简述
功能: Java Web精品项目源码,家居商城分类展示,商品展示, 商品下单,购物车,个人中心,后台管理,用户管理,商品管理,分类管理等等。
二、项目运行
环境配置: Jdk1.8 + Tomcat8.5 + mysql + Eclispe (IntelliJ IDEA,Eclispe,MyEclispe,Sts 都支持)
项目技术: Jdbc+ Servlert + html+ css + JavaScript + JQuery + Ajax + Fileupload
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
request.setAttribute("count",count);
request.setAttribute("product",p);
request.setAttribute("totalPrice",totalPrice);
//查询购物车数量
String cartCount = "0";
CartService service3 = new CartServiceImpl();
if (user != null) {
cartCount = String.valueOf(service3.findCartCountByUserId(user.getUser_id()));
}else {
cartCount = "?";
}
request.setAttribute("cartCount",cartCount);
//
// OrderList orderList = new OrderList(order_id,product_id,product_quantity);
//
// OrderListService service5 = new OrderListServiceImpl();
// service5.addOrderList(orderList);
request.getRequestDispatcher("order.jsp").forward(request, response);
}else {
response.sendRedirect("login.jsp");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
验证码控制器:
/**
* 验证码
*/
@WebServlet("/checkCodeServlet")
public class CheckCodeServlet extends HttpServlet {
//获取画笔
Graphics g = image.getGraphics();
//设置画笔颜色为灰色
g.setColor(Color.GRAY);
//填充图片
g.fillRect(0,0, width,height);
//产生4个随机验证码,12Ey
String checkCode = getCheckCode();
//将验证码放入HttpSession中
request.getSession().setAttribute("CHECKCODE_SERVER",checkCode);
//设置画笔颜色为黄色
g.setColor(Color.YELLOW);
//设置字体的小大
g.setFont(new Font("黑体",Font.BOLD,30));
//向图片上写入验证码
g.drawString(checkCode,36,28);
//将内存中的图片输出到浏览器
//参数一:图片对象
//参数二:图片的格式,如PNG,JPG,GIF
//参数三:图片输出到哪里去
ImageIO.write(image,"PNG",response.getOutputStream());
}
int a1 = 0;
int a2 = 0;
int a3 = 0;
int a4 = 0;
for (Order o:orders) {
if ("0".equals(o.getIs_pay()) && "0".equals(o.getIs_ship()) && "0".equals(o.getIs_receipt())) {
a1++;
}
if ("1".equals(o.getIs_pay()) && "0".equals(o.getIs_ship()) && "0".equals(o.getIs_receipt())) {
a2++;
}
if ("1".equals(o.getIs_pay()) && "1".equals(o.getIs_ship()) && "0".equals(o.getIs_receipt())) {
a3++;
}
if ("1".equals(o.getIs_pay()) && "1".equals(o.getIs_ship()) && "1".equals(o.getIs_receipt())) {
a4++;
}
}
//计算商品价格
String pid = request.getParameter("pid");
String count = request.getParameter("count");
ProductService service2 = new ProductServiceImpl();
Product p = service2.findProductById(Integer.valueOf(pid));
int price = p.getProduct_price();
int totalPrice = price*Integer.valueOf(count);
request.setAttribute("price",price);
request.setAttribute("count",count);
request.setAttribute("product",p);
request.setAttribute("totalPrice",totalPrice);
//查询购物车数量
String cartCount = "0";
CartService service3 = new CartServiceImpl();
if (user != null) {
cartCount = String.valueOf(service3.findCartCountByUserId(user.getUser_id()));
}else {
cartCount = "?";
request.setAttribute("clist",clist);
AddressService service = new AddressServiceImpl();
List<Address> addresses = service.findAddressByUserId(user.getUser_id());
request.setAttribute("address",addresses);
//计算商品价格
String pid = request.getParameter("pid");
String oid = request.getParameter("oid");
String count = request.getParameter("count");
ProductService service2 = new ProductServiceImpl();
Product p = service2.findProductById(Integer.valueOf(pid));
int price = p.getProduct_price();
int totalPrice = price*Integer.valueOf(count);
request.setAttribute("price",price);
//查看该用户的各种订单数量
OrderService service2 = new OrderServiceImpl();
List<Order> orders = service2.findOrderByUserId(user.getUser_id());
int a1 = 0;
int a2 = 0;
int a3 = 0;
int a4 = 0;
for (Order o:orders) {
if ("0".equals(o.getIs_pay()) && "0".equals(o.getIs_ship()) && "0".equals(o.getIs_receipt())) {
a1++;
}
if ("1".equals(o.getIs_pay()) && "0".equals(o.getIs_ship()) && "0".equals(o.getIs_receipt())) {
a2++;
List<Category> clist = service1.findCategoryListByName("child");
request.setAttribute("flist",flist);
request.setAttribute("clist",clist);
request.setAttribute("user",newUser);
//查看该用户的各种订单数量
OrderService service2 = new OrderServiceImpl();
List<Order> orders = service2.findOrderByUserId(user.getUser_id());
int a1 = 0;
int a2 = 0;
int a3 = 0;
}
}
购物车数量信息:
@WebServlet("/selectProductList")
public class SelectProductList extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//查询购物车数量
HttpSession session = request.getSession();
User user = (User) session.getAttribute("name");
String cartCount = "0";
CartService service1 = new CartServiceImpl();
if (user != null) {
cartCount = String.valueOf(service1.findCartCountByUserId(user.getUser_id()));
}else {
cartCount = "?";
}
request.setAttribute("cartCount",cartCount);
String cid = request.getParameter("cid");
if (cid != null && !cid.trim().equals("")) {
ProductService service = new ProductServiceImpl();
List<Product> list = service.findProductByCategoryCid(Integer.valueOf(cid));
request.setAttribute("productList",list);
if (user != null) {
cartCount = String.valueOf(service3.findCartCountByUserId(user.getUser_id()));
}else {
cartCount = "?";
}
request.setAttribute("cartCount",cartCount);
//添加此订单为待支付订单
//添加此订单的订单列表
//查询该订单中商品及数量
int product_quantity = Integer.valueOf(count);
int product_id = p.getProduct_id();
Order order = new Order(System.currentTimeMillis() +""+ user.getUser_id(),user.getUser_id(),totalPrice,new Date(),"0","0","0",product_id,product_quantity);
ProductService service5 = new ProductServiceImpl();
Product product = service5.findProductById(product_id);
order.setProduct_name(product.getProduct_name());
order.setProduct_photo(product.getProduct_photo());
order.setProduct_price(product.getProduct_price());
request.setAttribute("order",order);
OrderService service4 = new OrderServiceImpl();
service4.addOrder(order);
}
// String product_quantity = count;
// String order_id = order.getOrder_id();
// int product_id = p.getProduct_id();
//查看该用户的各种订单数量
OrderService service2 = new OrderServiceImpl();
List<Order> orders = service2.findOrderByUserId(user.getUser_id());
int a1 = 0;
int a2 = 0;
int a3 = 0;
int a4 = 0;
for (Order o:orders) {
if ("0".equals(o.getIs_pay()) && "0".equals(o.getIs_ship()) && "0".equals(o.getIs_receipt())) {
a1++;
}
}else {
response.sendRedirect("login.jsp");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
验证码控制器:
/**
* 验证码
*/
@WebServlet("/checkCodeServlet")
public class CheckCodeServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
ProductService service = new ProductServiceImpl();
List<Product> list = service.findProductByCategoryCid(Integer.valueOf(cid));
request.setAttribute("productList",list);
CategoryService service2 = new CategoryServiceImpl();
Category c = service2.findCategoryByCid(Integer.valueOf(cid));
System.out.println(c.getCategory_name());
request.setAttribute("childC",c);
Category f = service2.findCategoryByCid(c.getCategory_parentid());
System.out.println(f.getCategory_name());
request.setAttribute("fatherC",f);
List<Category> flist = service2.findCategoryListByName("father");
request.setAttribute("flist", flist);
List<Category> clist = service2.findCategoryListByName("child");
request.setAttribute("clist", clist);
//没实现分类添加图片
if (f.getCategory_id() == 1) {
request.setAttribute("link", "images/temp/banner1.jpg");
}else if (f.getCategory_id() == 2){
request.setAttribute("link", "images/temp/bzbig.jpg");
}else if (f.getCategory_id() == 3){
request.setAttribute("link", "images/temp/banner2.jpg");
//查看该用户的各种订单数量
OrderService service2 = new OrderServiceImpl();
List<Order> orders = service2.findOrderByUserId(user.getUser_id());
int a1 = 0;
int a2 = 0;
int a3 = 0;
int a4 = 0;
for (Order o:orders) {
if ("0".equals(o.getIs_pay()) && "0".equals(o.getIs_ship()) && "0".equals(o.getIs_receipt())) {
a1++;
}
if ("1".equals(o.getIs_pay()) && "0".equals(o.getIs_ship()) && "0".equals(o.getIs_receipt())) {
a2++;
}
if ("1".equals(o.getIs_pay()) && "1".equals(o.getIs_ship()) && "0".equals(o.getIs_receipt())) {
a3++;
}
if ("1".equals(o.getIs_pay()) && "1".equals(o.getIs_ship()) && "1".equals(o.getIs_receipt())) {
request.getRequestDispatcher("productlist.jsp").forward(request, response);
}else {
//有fid
String fid = request.getParameter("fid");
CategoryService service2 = new CategoryServiceImpl();
Category f = service2.findCategoryByCid(Integer.valueOf(fid));
request.setAttribute("fatherC",f);
List<Category> flist = service2.findCategoryListByName("father");
request.setAttribute("flist", flist);
List<Category> clist = service2.findCategoryListByName("child");
request.setAttribute("clist", clist);
//没实现分类添加图片
if (f.getCategory_id() == 1) {
request.setAttribute("link", "images/temp/banner1.jpg");
}else if (f.getCategory_id() == 2){
request.setAttribute("link", "images/temp/bzbig.jpg");
}else if (f.getCategory_id() == 3){
request.setAttribute("link", "images/temp/banner2.jpg");
}else if (f.getCategory_id() == 4){
request.setAttribute("link", "images/temp/perfume_Banner.jpg");
}else if (f.getCategory_id() == 5){
//产生4个随机验证码,12Ey
String checkCode = getCheckCode();
//将验证码放入HttpSession中
request.getSession().setAttribute("CHECKCODE_SERVER",checkCode);
//设置画笔颜色为黄色
g.setColor(Color.YELLOW);
//设置字体的小大
g.setFont(new Font("黑体",Font.BOLD,30));
//向图片上写入验证码
g.drawString(checkCode,36,28);
//将内存中的图片输出到浏览器
//参数一:图片对象
//参数二:图片的格式,如PNG,JPG,GIF
//参数三:图片输出到哪里去
ImageIO.write(image,"PNG",response.getOutputStream());
}
//添加此订单为待支付订单
//添加此订单的订单列表
//查询该订单中商品及数量
int product_quantity = Integer.valueOf(count);
int product_id = p.getProduct_id();
Order order = new Order(System.currentTimeMillis() +""+ user.getUser_id(),user.getUser_id(),totalPrice,new Date(),"0","0","0",product_id,product_quantity);
ProductService service5 = new ProductServiceImpl();
Product product = service5.findProductById(product_id);
order.setProduct_name(product.getProduct_name());
order.setProduct_photo(product.getProduct_photo());
order.setProduct_price(product.getProduct_price());
request.setAttribute("order",order);
OrderService service4 = new OrderServiceImpl();
service4.addOrder(order);
}
// String product_quantity = count;
// String order_id = order.getOrder_id();
// int product_id = p.getProduct_id();
//
// OrderList orderList = new OrderList(order_id,product_id,product_quantity);
//
// OrderListService service5 = new OrderListServiceImpl();
// service5.addOrderList(orderList);
request.getRequestDispatcher("order.jsp").forward(request, response);
* 验证码
*/
@WebServlet("/checkCodeServlet")
public class CheckCodeServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
//服务器通知浏览器不要缓存
response.setHeader("pragma","no-cache");
response.setHeader("cache-control","no-cache");
response.setHeader("expires","0");
//在内存中创建一个长80,宽30的图片,默认黑色背景
//参数一:长
//参数二:宽
request.getRequestDispatcher("productlist.jsp").forward(request, response);
}else {
//有fid
String fid = request.getParameter("fid");
CategoryService service2 = new CategoryServiceImpl();
Category f = service2.findCategoryByCid(Integer.valueOf(fid));
request.setAttribute("fatherC",f);
List<Category> flist = service2.findCategoryListByName("father");
request.setAttribute("flist", flist);
List<Category> clist = service2.findCategoryListByName("child");
request.setAttribute("clist", clist);
//没实现分类添加图片
if (f.getCategory_id() == 1) {
request.setAttribute("link", "images/temp/banner1.jpg");
}else if (f.getCategory_id() == 2){
request.setAttribute("link", "images/temp/bzbig.jpg");
}else if (f.getCategory_id() == 3){
request.setAttribute("link", "images/temp/banner2.jpg");
}else if (f.getCategory_id() == 5){
request.setAttribute("link", "img/idea1.jpg");
}else {
request.setAttribute("link", "img/banner1.jpg");
}
request.getRequestDispatcher("productlist.jsp").forward(request, response);
}else {
//有fid
String fid = request.getParameter("fid");
CategoryService service2 = new CategoryServiceImpl();
Category f = service2.findCategoryByCid(Integer.valueOf(fid));
request.setAttribute("fatherC",f);
List<Category> flist = service2.findCategoryListByName("father");
request.setAttribute("flist", flist);
List<Category> clist = service2.findCategoryListByName("child");
request.setAttribute("clist", clist);
//没实现分类添加图片
public class ToOrderServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//判断是否登录
HttpSession session = request.getSession();
User user = (User) session.getAttribute("name");
if (user != null) {
//判断是否有参数uid
String uid = request.getParameter("uid");
if (uid == null) {
//我的订单中心进入,无需添加商品
CategoryService service1 = new CategoryServiceImpl();
List<Category> flist = service1.findCategoryListByName("father");
List<Category> clist = service1.findCategoryListByName("child");
request.setAttribute("flist",flist);
request.setAttribute("clist",clist);
AddressService service = new AddressServiceImpl();
List<Address> addresses = service.findAddressByUserId(user.getUser_id());
request.setAttribute("address",addresses);
//计算商品价格
String pid = request.getParameter("pid");
String oid = request.getParameter("oid");
String count = request.getParameter("count");
ProductService service2 = new ProductServiceImpl();
Product p = service2.findProductById(Integer.valueOf(pid));
}
用户信息服务:
@WebServlet("/toUserInfoServlet")
public class ToUserInfoServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
User user = (User) session.getAttribute("name");
if (user != null) {
UserService service = new UserServiceImpl();
User newUser = service.findUserByUserId(user.getUser_id());
CategoryService service1 = new CategoryServiceImpl();
List<Category> flist = service1.findCategoryListByName("father");
List<Category> clist = service1.findCategoryListByName("child");
request.setAttribute("flist",flist);
request.setAttribute("clist",clist);
request.setAttribute("user",newUser);
request.setAttribute("order",o);
}else {
CategoryService service1 = new CategoryServiceImpl();
List<Category> flist = service1.findCategoryListByName("father");
List<Category> clist = service1.findCategoryListByName("child");
request.setAttribute("flist",flist);
request.setAttribute("clist",clist);
AddressService service = new AddressServiceImpl();
List<Address> addresses = service.findAddressByUserId(user.getUser_id());
request.setAttribute("address",addresses);
//计算商品价格
String pid = request.getParameter("pid");
String count = request.getParameter("count");
ProductService service2 = new ProductServiceImpl();
request.setAttribute("cartCount",cartCount);
String cid = request.getParameter("cid");
if (cid != null && !cid.trim().equals("")) {
ProductService service = new ProductServiceImpl();
List<Product> list = service.findProductByCategoryCid(Integer.valueOf(cid));
request.setAttribute("productList",list);
CategoryService service2 = new CategoryServiceImpl();
Category c = service2.findCategoryByCid(Integer.valueOf(cid));
System.out.println(c.getCategory_name());
request.setAttribute("childC",c);
Category f = service2.findCategoryByCid(c.getCategory_parentid());
System.out.println(f.getCategory_name());
request.setAttribute("fatherC",f);
List<Category> flist = service2.findCategoryListByName("father");
request.setAttribute("flist", flist);
g.setColor(Color.GRAY);
//填充图片
g.fillRect(0,0, width,height);
//产生4个随机验证码,12Ey
String checkCode = getCheckCode();
//将验证码放入HttpSession中
request.getSession().setAttribute("CHECKCODE_SERVER",checkCode);
//设置画笔颜色为黄色
g.setColor(Color.YELLOW);
//设置字体的小大
g.setFont(new Font("黑体",Font.BOLD,30));
//向图片上写入验证码
g.drawString(checkCode,36,28);
//将内存中的图片输出到浏览器
//参数一:图片对象
//参数二:图片的格式,如PNG,JPG,GIF
//参数三:图片输出到哪里去
ImageIO.write(image,"PNG",response.getOutputStream());
}
/**
String count = request.getParameter("count");
ProductService service2 = new ProductServiceImpl();
Product p = service2.findProductById(Integer.valueOf(pid));
int price = p.getProduct_price();
int totalPrice = price*Integer.valueOf(count);
request.setAttribute("price",price);
request.setAttribute("count",count);
request.setAttribute("product",p);
request.setAttribute("totalPrice",totalPrice);
//查询购物车数量
String cartCount = "0";
CartService service3 = new CartServiceImpl();
if (user != null) {
cartCount = String.valueOf(service3.findCartCountByUserId(user.getUser_id()));
}else {
cartCount = "?";
}
request.setAttribute("cartCount",cartCount);
//添加此订单为待支付订单
//添加此订单的订单列表
//查询该订单中商品及数量
int product_quantity = Integer.valueOf(count);