基于javaweb+mysql的jsp+servlet婚品婚纱商城系统(java+servlet+jsp+javascript+mysql)
运行环境
Java≥8、MySQL≥5.7、Tomcat≥8
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
基于javaweb+mysql的JSP+Servlet婚品婚纱商城系统(java+servlet+jsp+javascript+mysql)
一、项目运行
运行环境
jdk8+tomcat8+mysql+IntelliJ IDEA( Eclispe , MyEclispe ,Sts 都支持,代码与开发环境运行无关啦,只需要调整环境即可)
项目技术
Servlet、JSP 、、MySQL 、 Ajax 、JavaScript、CSS、等等
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
}, Date.class);
BeanUtils.populate(product, request.getParameterMap());
//商品类别
String cid = request.getParameter("categoryId");
String cname = null;
List<Map<String, Object>> mapListCategory = service.findOneById("category", cid);
for (Map<String, Object> map : mapListCategory) {
cname = (String) map.get("cname");
}
product.setCategory(new Category(cid, cname));
//商品编号
product.setPid(CommonUtils.getUUID().replace("-", ""));
//图片处理
Part file = request.getPart("file");
if (file != null) {
String oldName = file.getHeader("content-disposition");
if (oldName != null && oldName.indexOf(".") > 0) {
String newName = UUID.randomUUID() + oldName.substring(oldName.lastIndexOf("."), oldName.length() - 1);
product.setPimage("img/product/" + newName);
file.write(imageUploadPath + newName);
}
}
service.add(product);
response.sendRedirect(request.getContextPath() + "/admin?method=findAll&code=product");
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
//查询所有商品类别,返回json数据
public void findAllCategory(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
public void findAllCategory(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Category> categoryList = service.findAllCategory();
Gson gson = new Gson();
String json = gson.toJson(categoryList);
response.getWriter().write(json);
}
//修改商品
public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Product product = new Product();
try {
//将生日String转换成Date类型
ConvertUtils.register(new Converter() {
//实现类型转换,第一个参数:目标数据类型 第二个参数:需要转换的数据
@Override
public Object convert(Class cla, Object value) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = sdf.parse(value.toString());
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
}, Date.class);
BeanUtils.populate(product, request.getParameterMap());
String cid = request.getParameter("categoryId");
String cname = null;
List<Map<String, Object>> mapListCategory = service.findOneById("category", cid);
for (Map<String, Object> map : mapListCategory) {
cname = (String) map.get("cname");
}
product.setCategory(new Category(cid, cname));
//图片处理
Part file = request.getPart("file");
if (file != null) {
String oldName = file.getHeader("content-disposition");
double subTotal =buyNum * product.getShop_price();
//先从会话中获取购物车
Cart cart = (Cart) session.getAttribute("cart");
//若是第一次从会话中获取购物车
if(cart==null){
cart = new Cart();
}
double newSubTotal = subTotal;
//获取会话中购物车明细
Map<String, CartItem> cartItemMap = cart.getMap();
//当前会话中是否存在正在添加的商品
if(cartItemMap.containsKey(pid)){
int oldNum = cartItemMap.get(pid).getBuyNum();
//合并后的总数量
buyNum += oldNum;
//当前合并后,重新计算小计
newSubTotal = buyNum * product.getShop_price();
}
//封装CartItem购物车明细
CartItem cartItem = new CartItem(product,buyNum,newSubTotal);
//封装购物车
cartItemMap.put(pid,cartItem);
//将购物车明细添加进来
cart.setMap(cartItemMap);
//将总金额添加进来
double total = cart.getTotal() + subTotal;
cart.setTotal(total);
session.setAttribute("cart",cart);
response.sendRedirect("cart.jsp");
}
//删除当前购物项
public void delCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//先从会话中获取购物车
Cart cart = (Cart) session.getAttribute("cart");
//若是第一次从会话中获取购物车
if(cart==null){
cart = new Cart();
}
double newSubTotal = subTotal;
//获取会话中购物车明细
Map<String, CartItem> cartItemMap = cart.getMap();
//当前会话中是否存在正在添加的商品
if(cartItemMap.containsKey(pid)){
int oldNum = cartItemMap.get(pid).getBuyNum();
//合并后的总数量
buyNum += oldNum;
//当前合并后,重新计算小计
newSubTotal = buyNum * product.getShop_price();
}
//封装CartItem购物车明细
CartItem cartItem = new CartItem(product,buyNum,newSubTotal);
//封装购物车
cartItemMap.put(pid,cartItem);
//将购物车明细添加进来
cart.setMap(cartItemMap);
//将总金额添加进来
double total = cart.getTotal() + subTotal;
cart.setTotal(total);
session.setAttribute("cart",cart);
response.sendRedirect("cart.jsp");
}
//删除当前购物项
public void delCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String pid = request.getParameter("pid");
Product product = service.viewProductById(pid);
//计算当前加入购物车的商品的小计
double subTotal =buyNum * product.getShop_price();
//先从会话中获取购物车
Cart cart = (Cart) session.getAttribute("cart");
//若是第一次从会话中获取购物车
if(cart==null){
cart = new Cart();
}
double newSubTotal = subTotal;
//获取会话中购物车明细
Map<String, CartItem> cartItemMap = cart.getMap();
//当前会话中是否存在正在添加的商品
if(cartItemMap.containsKey(pid)){
int oldNum = cartItemMap.get(pid).getBuyNum();
//合并后的总数量
buyNum += oldNum;
//当前合并后,重新计算小计
newSubTotal = buyNum * product.getShop_price();
}
//封装CartItem购物车明细
CartItem cartItem = new CartItem(product,buyNum,newSubTotal);
//封装购物车
cartItemMap.put(pid,cartItem);
//将购物车明细添加进来
cart.setMap(cartItemMap);
//将总金额添加进来
double total = cart.getTotal() + subTotal;
cart.setTotal(total);
session.setAttribute("cart",cart);
response.sendRedirect("cart.jsp");
}
//删除当前购物项
public void delCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String pid = request.getParameter("pid");
//1.从会话中获取购物车
HttpSession session = request.getSession();
Cart cart = (Cart)session.getAttribute("cart");
file.write(imageUploadPath + newName);
}
}
service.add(product);
response.sendRedirect(request.getContextPath() + "/admin?method=findAll&code=product");
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
//查询所有商品类别,返回json数据
public void findAllCategory(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Category> categoryList = service.findAllCategory();
Gson gson = new Gson();
String json = gson.toJson(categoryList);
response.getWriter().write(json);
}
//修改商品
public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Product product = new Product();
try {
//将生日String转换成Date类型
ConvertUtils.register(new Converter() {
//实现类型转换,第一个参数:目标数据类型 第二个参数:需要转换的数据
@Override
public Object convert(Class cla, Object value) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = sdf.parse(value.toString());
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
}, Date.class);
BeanUtils.populate(product, request.getParameterMap());
String cid = request.getParameter("categoryId");
String cname = null;
List<Map<String, Object>> mapListCategory = service.findOneById("category", cid);
for (Map<String, Object> map : mapListCategory) {
cname = (String) map.get("cname");
}
product.setCategory(new Category(cid, cname));
//图片处理
public void findAllCategory(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Category> categoryList = service.findAllCategory();
Gson gson = new Gson();
String json = gson.toJson(categoryList);
response.getWriter().write(json);
}
//修改商品
public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Product product = new Product();
try {
//将生日String转换成Date类型
ConvertUtils.register(new Converter() {
//实现类型转换,第一个参数:目标数据类型 第二个参数:需要转换的数据
@Override
public Object convert(Class cla, Object value) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = sdf.parse(value.toString());
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
}, Date.class);
BeanUtils.populate(product, request.getParameterMap());
String cid = request.getParameter("categoryId");
String cname = null;
List<Map<String, Object>> mapListCategory = service.findOneById("category", cid);
for (Map<String, Object> map : mapListCategory) {
cname = (String) map.get("cname");
}
product.setCategory(new Category(cid, cname));
//图片处理
Part file = request.getPart("file");
if (file != null) {
String oldName = file.getHeader("content-disposition");
if (oldName != null && oldName.indexOf(".") > 0) {
String newName = UUID.randomUUID() + oldName.substring(oldName.lastIndexOf("."), oldName.length() - 1);
product.setPimage("img/product/" + newName);
file.write(imageUploadPath + newName);
}
}
String currentImg = product.getPimage();
if (currentImg == null) { //未上传
List<Map<String, Object>> mapList = service.findOneById("product", product.getPid());
String img = null;
//首页展示
public void index(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//热门商品
List<Product> productsHot = service.viewHot();
request.setAttribute("productsHot",productsHot);
//跳转至index.jsp
request.getRequestDispatcher("index.jsp").forward(request,response);
}
//查看商品列表
public void viewProductListByCid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//从请求参数中获取当前页
String page = request.getParameter("pageNow");
int pageNow = 1; //默认查询第一页
if(page!=null){
pageNow = Integer.parseInt(page);
}
//从请求参数中获取查询条件 pname cid
String pname = request.getParameter("pname");
if(pname==null){
pname = ""; //默认查询所有
}
String cid = request.getParameter("cid");
// 执行service的分页方法
PageVO<Product> vo = service.viewProductListByCid(cid, pname, pageNow);
//将vo传递到页面中
request.setAttribute("vo",vo);
request.setAttribute("cid",cid);
request.getRequestDispatcher("shop.jsp").forward(request,response);
}
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
//查询所有商品类别,返回json数据
public void findAllCategory(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Category> categoryList = service.findAllCategory();
Gson gson = new Gson();
String json = gson.toJson(categoryList);
response.getWriter().write(json);
}
//修改商品
public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Product product = new Product();
try {
//将生日String转换成Date类型
ConvertUtils.register(new Converter() {
//实现类型转换,第一个参数:目标数据类型 第二个参数:需要转换的数据
@Override
public Object convert(Class cla, Object value) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = sdf.parse(value.toString());
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
}, Date.class);
BeanUtils.populate(product, request.getParameterMap());
String cid = request.getParameter("categoryId");
String cname = null;
List<Map<String, Object>> mapListCategory = service.findOneById("category", cid);
for (Map<String, Object> map : mapListCategory) {
cname = (String) map.get("cname");
}
product.setCategory(new Category(cid, cname));
//图片处理
Part file = request.getPart("file");
if (file != null) {
String oldName = file.getHeader("content-disposition");
if (oldName != null && oldName.indexOf(".") > 0) {
String newName = UUID.randomUUID() + oldName.substring(oldName.lastIndexOf("."), oldName.length() - 1);
product.setPimage("img/product/" + newName);
file.write(imageUploadPath + newName);
}
}
if(user!=null){
//状态码为1,才可以登录
if(user.getState()==1){
//记住用户名
String remember = request.getParameter("remember");
//自动登录
String free = request.getParameter("free");
//记住用户名,若点击remember,则在Cookie中存储用户名
if(remember!=null && remember.equals("remember")){
Cookie usernameCookie = new Cookie("remember", URLEncoder.encode(username,"UTF-8"));
usernameCookie.setMaxAge(7*24*60*60);
response.addCookie(usernameCookie);
}
//自动登录,若点击free,则再Cookie中存储用户名和密码
else if(free!=null && free.equals("free")){
Cookie usernameCookie = new Cookie("username", URLEncoder.encode(username,"UTF-8"));
Cookie passwordCookie = new Cookie("password",password);
usernameCookie.setMaxAge(7*24*60*60);
passwordCookie.setMaxAge(7*24*60*60);
response.addCookie(usernameCookie);
response.addCookie(passwordCookie);
}
session.setAttribute("user",user);
response.sendRedirect(request.getContextPath()+"/product?method=index");
request.getRequestDispatcher("index.jsp").forward(request,response);
}else{
request.setAttribute("msg","当前账户未激活,请尽快前往邮箱激活");
request.getRequestDispatcher("login.jsp").forward(request,response);
}
}else{
request.setAttribute("msg","用户名与密码不匹配");
request.getRequestDispatcher("login.jsp").forward(request,response);
}
}
//登出
public void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
session.invalidate();
orders.setState(0);
//从会话红获取登录用户的信息
User user = (User) session.getAttribute("user");
//设置下单者信息
orders.setUser(user);
//从会话中的购物车获取了所有的购物项
Map<String, CartItem> cartMap = cart.getMap();
for (Map.Entry<String, CartItem> entry : cartMap.entrySet()) {
CartItem cartItem = entry.getValue();
//创建订单明细
OrderItem orderItem = new OrderItem();
//设置明细编号
orderItem.setItemid(CommonUtils.getUUID().replaceAll("-", ""));
//设置购买数量
orderItem.setCount(cartItem.getBuyNum());
//设置小计
orderItem.setSubtotal(cartItem.getSubTotal());
//设置购买商品信息
orderItem.setProduct(cartItem.getProduct());
//设置所属订单
orderItem.setOrders(orders);
//设置订单中的多个订单明细
orders.getItemsList().add(orderItem);
}
//提交订单
boolean flag = service.submitOrder(orders);
if (flag) {
//提交成功
session.setAttribute("orders", orders);
response.sendRedirect("order.jsp");
} else {
//提交失败
request.setAttribute("msg", "当前订单提交失败,请重新提交");
request.getRequestDispatcher("cart.jsp").forward(request, response);
}
}
//确认订单
@WebServlet(name = "AdminServlet", urlPatterns = "/admin")
@MultipartConfig
public class AdminServlet extends BaseServlet {
public static String imageUploadPath = "C:\\Users\\Administrator\\Desktop\\200.ye\\project\\web\\img\\product\\";
private AdminService service = new AdminService();
//登录
public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String adminName = request.getParameter("adminName");
String adminPwd = request.getParameter("adminPwd");
//商品导出
public void download(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//表格表头信息
String[] title = {"商品编号", "商品名称", "商品描述", "商品价格", "商品图片", "商品日期"};
String[] prop = {"pid", "pname", "pdesc", "shop_price", "pimage", "pdate"};
//excel的名字
String fileName = "goods.xls";
//sheet页的名字
String sheetName = "商品详情";
//当前页码
int pageNow = Integer.parseInt(request.getParameter("pageNow"));
//数据
List<Product> goods = service.findAll("product", "", pageNow).getList();
HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName, title, prop, goods, null);
//输出文件
response.setContentType("application/octet-stream;charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
response.addHeader("Pargam", "no-cache");
response.addHeader("Cache-Control", "no-cache");
OutputStream os = response.getOutputStream();
wb.write(os);
os.flush();
os.close();
}
//首页
public void showIndex(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<HomeVO> list = service.findNumByMonth();
Gson gson = new Gson();
String json = gson.toJson(list);
response.getWriter().write(json);
}
//添加和修改商品类别
public void updateCategory(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String code = request.getParameter("code");
Category category = new Category();
}
}
//登录过滤器
@WebFilter(filterName = "f2" , urlPatterns = "/order/*")
public class LoginFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest)servletRequest;
HttpServletResponse response = (HttpServletResponse)servletResponse;
//获取会话对象
HttpSession session = request.getSession();
//从会话中获取登录者的信息
User user = (User)session.getAttribute("user");
//若user为null,则直接跳转至登录界面
if(user==null){
response.sendRedirect("login.jsp");
return;
}
//若user不为null,则直接将过滤链传递下去
filterChain.doFilter(request,response);
}
@WebServlet(urlPatterns = "/product")
public class ProductServlet extends BaseServlet {
private ProductService service = new ProductService();
private CategoryService categoryService = new CategoryService();
//首页展示
public void index(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//热门商品
List<Product> productsHot = service.viewHot();
request.setAttribute("productsHot",productsHot);
//跳转至index.jsp
request.getRequestDispatcher("index.jsp").forward(request,response);
}
//查看商品列表
public void viewProductListByCid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//从请求参数中获取当前页
String page = request.getParameter("pageNow");
int pageNow = 1; //默认查询第一页
if(page!=null){
pageNow = Integer.parseInt(page);
}
//从请求参数中获取查询条件 pname cid
String pname = request.getParameter("pname");
if(pname==null){
pname = ""; //默认查询所有
}
String cid = request.getParameter("cid");
// 执行service的分页方法
PageVO<Product> vo = service.viewProductListByCid(cid, pname, pageNow);
//将vo传递到页面中
request.setAttribute("vo",vo);
request.setAttribute("cid",cid);
Cookie passwordCookie = new Cookie("password","");
usernameCookie.setMaxAge(0);
passwordCookie.setMaxAge(0);
response.addCookie(usernameCookie);
response.addCookie(passwordCookie);
response.sendRedirect(request.getContextPath()+"/product?method=index");
// request.getRequestDispatcher("login.jsp").forward(request,response);
}
}
/**
* 订单的控制层
*/
@WebServlet(urlPatterns = "/order")
public class OrderServlet extends BaseServlet {
private OrderService service = new OrderService();
//提交订单
public void submitOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
double newSubTotal = subTotal;
//获取会话中购物车明细
Map<String, CartItem> cartItemMap = cart.getMap();
//当前会话中是否存在正在添加的商品
if(cartItemMap.containsKey(pid)){
int oldNum = cartItemMap.get(pid).getBuyNum();
//合并后的总数量
buyNum += oldNum;
//当前合并后,重新计算小计
newSubTotal = buyNum * product.getShop_price();
}
//封装CartItem购物车明细
CartItem cartItem = new CartItem(product,buyNum,newSubTotal);
//封装购物车
cartItemMap.put(pid,cartItem);
//将购物车明细添加进来
cart.setMap(cartItemMap);
//将总金额添加进来
double total = cart.getTotal() + subTotal;
cart.setTotal(total);
session.setAttribute("cart",cart);
response.sendRedirect("cart.jsp");
}
//删除当前购物项
public void delCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String pid = request.getParameter("pid");
//1.从会话中获取购物车
HttpSession session = request.getSession();
Cart cart = (Cart)session.getAttribute("cart");
//2.获取购物车中的map集合(购物车明细)
Map<String, CartItem> cartMap = cart.getMap();
//3.重新计算总金额 = 原来购物车的总金额 - 当前移除的购物项的小计
public class ProductServlet extends BaseServlet {
private ProductService service = new ProductService();
private CategoryService categoryService = new CategoryService();
//首页展示
public void index(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//热门商品
List<Product> productsHot = service.viewHot();
request.setAttribute("productsHot",productsHot);
//跳转至index.jsp
request.getRequestDispatcher("index.jsp").forward(request,response);
}
//查看商品列表
public void viewProductListByCid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//从请求参数中获取当前页
String page = request.getParameter("pageNow");
int pageNow = 1; //默认查询第一页
if(page!=null){
pageNow = Integer.parseInt(page);
}
//从请求参数中获取查询条件 pname cid
String pname = request.getParameter("pname");
if(pname==null){
pname = ""; //默认查询所有
}
String cid = request.getParameter("cid");
// 执行service的分页方法
PageVO<Product> vo = service.viewProductListByCid(cid, pname, pageNow);
//将vo传递到页面中
request.setAttribute("vo",vo);