JavaWeb+MySql实现简易商城系统
文章目录
- JavaWeb+MySql实现简易商城系统
- 前言
- 一、系统结构
- E-R图展示:
- 项目结构:
- 效果展示:
- 首页
- 购物车
- 管理员
- 修改商品
- 二、具体实现
- 1.src
- 配置c3p0文件:
- filter包:
- utils包:
- model包:
- dao包
- service包
- servlet包
- 2.web
- jsp
- css样式
- 三、源码
前言
该系统使用MVC架构实现了商城的购物车功能,除此之外,基本上都是不同实体之间的增删改查。在一些地方还需要优化(但是我懒癌犯了,不想改了)例如在管理员和用户登录时,应该是由同一个窗口登录,通过数据库存储的字段判断跳转页面。除此之外还有一个问题,当管理员给商品更换图片后,在idea中图片加载成功,在数据库中存储地址也没有问题,但是在页面中有时候不能正常刷新出来,需要再次刷新。
一、系统结构
E-R图展示:
项目结构:
效果展示:
首页
购物车
管理员
修改商品
二、具体实现
1.src
配置c3p0文件:
com.mysql.jdbc.Driver jdbc:mysql://localhost:3306/florist root 15194538986a filter包:
filter:
package filter; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import java.io.IOException; @WebFilter(filterName = "EncodeFilter",urlPatterns = "/*") public class EncodeFilter implements Filter { public void destroy() { } public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException { // 更改编码格式 req.setCharacterEncoding("utf-8"); resp.setContentType("text/html;charset=utf-8"); // 放行 chain.doFilter(req, resp); } public void init(FilterConfig config) throws ServletException { } }
utils包:
DBUtil:
package utils; import com.mchange.v2.c3p0.ComboPooledDataSource; import javax.sql.DataSource; import java.sql.Connection; import java.sql.SQLException; public class DBUtil { private static DataSource ds = new ComboPooledDataSource(); // 数据源 public static DataSource getDataSource(){ DataSource ds = new ComboPooledDataSource(); return ds; } // 拿到连接对象 public static Connection getConnection() throws SQLException { return ds.getConnection(); } }
PriceUtil
package utils; import java.math.BigDecimal; public class PriceUtil { // float add public static float add(float a,float b){ BigDecimal bigA = new BigDecimal(Float.toString(a)); BigDecimal bigB = new BigDecimal(Float.toString(b)); return bigA.add(bigB).floatValue(); } // double add public static double add(double a,double b){ BigDecimal bigA = new BigDecimal(Double.toString(a)); BigDecimal bigB = new BigDecimal(Double.toString(b)); return bigA.add(bigB).doubleValue(); } // float subtract public static float subtract(float a,float b){ BigDecimal bigA = new BigDecimal(Float.toString(a)); BigDecimal bigB = new BigDecimal(Float.toString(b)); return bigA.subtract(bigB).floatValue(); } // double subtract public static double subtract(double a,double b){ BigDecimal bigA = new BigDecimal(Double.toString(a)); BigDecimal bigB = new BigDecimal(Double.toString(b)); return bigA.subtract(bigB).doubleValue(); } }
UploadUtils:
package utils; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.Part; import java.io.File; import java.io.IOException; import java.util.UUID; public class UploadUtils {//工具类 public static String upload(Part part, HttpServletRequest request, HttpServletResponse response){ if(part.getSubmittedFileName().equals("")){ return null; } String photo = part.getSubmittedFileName();//获取文件名 photo= UUID.randomUUID()+photo;//为防止重名。加uid String[] aa = photo.split(".");//判断类型 if(!(photo.endsWith("jpg")||photo.endsWith("png")||photo.endsWith("jpeg"))){ request.setAttribute("type","err"); try { request.getRequestDispatcher("/html/xiangyingtupian.html").forward(request,response);//如果格式不对,跳转 } catch (ServletException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } photo=""; } String path="D:\\JAVA\\id\\workspace\\florist\\web\\img";//本地目录 File file = new File(path); if(!file.exists()){//如果目录不存在就新建一个 file.mkdirs(); } try { part.write(path+"/"+photo);//本地名字+文件名字 将文件的名字写入本地 } catch (IOException e) { e.printStackTrace(); } return photo;//返回图片的名字 } }
model包:
Flower:
package model; import java.io.Serializable; public class Flower { private int id; private String name; private String kind; private String price; private String path; public String getPath() { return path; } public void setPath(String path) { this.path = path; } public Flower(int id, String name, String kind, String price, String path) { this.id = id; this.name = name; this.kind = kind; this.price = price; this.path = path; } public Flower() { } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getKind() { return kind; } public void setKind(String kind) { this.kind = kind; } public String getPrice() { return price; } public void setPrice(String price) { this.price = price; } }
Manager:
package model; public class Manager { private int id; private String username; private String password; public Manager(int id, String username, String password) { this.id = id; this.username = username; this.password = password; } public Manager() { } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
User:
package model; public class User { private int id; private String username; private String password; private String name; private String phone; private String address; public User(int id, String username, String password, String name, String phone, String address) { this.id = id; this.username = username; this.password = password; this.name = name; this.phone = phone; this.address = address; } public User() { } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
dao包
UserDao:
package dao; import model.Flower; import model.User; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanHandler; import org.apache.commons.dbutils.handlers.BeanListHandler; import utils.DBUtil; import java.sql.*; import java.util.List; public class UserDao { // add方法 public static void addUser(User user) throws SQLException { // 拿到执行者对象 QueryRunner r = new QueryRunner(DBUtil.getDataSource()); // sql String sql="insert into user(username,password,name,phone,address) values(?,?,?,?,?)"; // 执行 r.update(sql,user.getUsername(),user.getPassword(),user.getName(),user.getPhone(),user.getAddress()); } // 判断username是否存在 public static boolean isUsernameExist(String username) throws SQLException { // 拿到执行者对象 QueryRunner r = new QueryRunner(DBUtil.getDataSource()); // sql String sql = "select * from user where username = ?"; // 执行 User user = r.query(sql,new BeanHandler
(User.class),username); if(user == null){ return false; }else{ return true; } } // username password 查询 public static User selectUsernamePassword(String username,String password) throws SQLException { // 拿到执行者对象 QueryRunner r = new QueryRunner(DBUtil.getDataSource()); // sql String sql = "select * from user where username = ? and password = ?"; // 执行sql return r.query(sql,new BeanHandler (User.class),username,password); } // 获取全部用户 public static List getAllUser() throws SQLException { // 拿到执行者对象 QueryRunner r =new QueryRunner(DBUtil.getDataSource()); // sql String sql = "select * from user"; // 执行sql return r.query(sql,new BeanListHandler (User.class)); } // 删除用户 public static void deleteUser(int id) throws SQLException { // 拿到执行者对象 QueryRunner r =new QueryRunner(DBUtil.getDataSource()); // sql String sql = "delete from user where id = ?"; // 执行sql r.update(sql,id); } // 修改用户信息 public static void updateUser(User user) throws SQLException { // 拿到执行者对象 QueryRunner r = new QueryRunner(DBUtil.getDataSource()); // sql String sql = "update user set password=?,name=?,phone=?,address=? where username=?"; r.update(sql,user.getPassword(),user.getName(),user.getPhone(),user.getAddress(),user.getUsername()); } // 通过id查询用户 public static User getUserById(int id) throws SQLException { // 拿到执行者对象 QueryRunner r =new QueryRunner(DBUtil.getDataSource()); // sql String sql = "select * from user where id =?"; // 执行 return r.query(sql,new BeanHandler (User.class),id); } } FlowerDao:
package dao; import model.Flower; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanHandler; import org.apache.commons.dbutils.handlers.BeanListHandler; import utils.DBUtil; import java.sql.SQLException; import java.util.List; public class FlowerDao { // 获取全部花 public static List
getAll() throws SQLException { // 拿到执行者对象 QueryRunner r = new QueryRunner(DBUtil.getDataSource()); // sql String sql = "select * from flowers"; // 执行 return r.query(sql,new BeanListHandler (Flower.class)); } // 通过id查询花 public static Flower getFlowerById(int id) throws SQLException { // 拿到执行者对象 QueryRunner r = new QueryRunner(DBUtil.getDataSource()); // sql String sql = "select * from flowers where id=?"; // 执行 return r.query(sql,new BeanHandler (Flower.class),id); } // 模糊查询 public static List getSearchFlower(String name) throws SQLException { // 拿到执行者对象 QueryRunner r = new QueryRunner(DBUtil.getDataSource()); // sql String sql = "select * from flowers where name like ?"; // 执行 return r.query(sql,new BeanListHandler (Flower.class),"%"+name+"%"); } // 删除花 public static void deleteFlower(int id) throws SQLException { // 拿到执行者对象 QueryRunner r = new QueryRunner(DBUtil.getDataSource()); // sql String sql = "delete from flowers where id=?"; // 执行 r.update(sql,id); } // 修改flower 信息 public static void updateFlower(Flower flower) throws SQLException { // 拿到执行者对象 QueryRunner r =new QueryRunner(DBUtil.getDataSource()); // sql String sql = null; if(flower.getPath()!=null){ sql = "update flowers set price=?,kind=?,path=? where id=?"; // 执行 r.update(sql,flower.getPrice(),flower.getKind(),flower.getPath(),flower.getId()); }else{ sql = "update flowers set price=?,kind=? where id=?"; // 执行 r.update(sql,flower.getPrice(),flower.getKind(),flower.getId()); } } // 新建花朵 public static void addFlower(Flower flower) throws SQLException { // 拿到执行者对象 QueryRunner r = new QueryRunner(DBUtil.getDataSource()); // sql String sql = "insert into flowers (name,price,kind,path) values(?,?,?,?)"; r.update(sql,flower.getName(),flower.getPrice(),flower.getKind(),flower.getPath()); } } ManagerDao:
package dao; import model.Manager; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanHandler; import utils.DBUtil; import java.sql.SQLException; public class ManagerDao { // 用户名 密码 查询 public static Manager selectManagernamePassword(String name,String password) throws SQLException { // 拿到执行者对象 QueryRunner r = new QueryRunner(DBUtil.getDataSource()); // sql String sql = "select * from manager where name = ? and password = ?"; // 执行sql return r.query(sql,new BeanHandler
(Manager.class),name,password); } } CartDao:
package dao; import model.Flower; import org.apache.commons.dbutils.DbUtils; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanHandler; import org.apache.commons.dbutils.handlers.BeanListHandler; import org.apache.commons.dbutils.handlers.ColumnListHandler; import org.apache.commons.dbutils.handlers.ScalarHandler; import utils.DBUtil; import javax.management.Query; import java.nio.channels.FileLock; import java.sql.SQLException; import java.util.HashMap; import java.util.List; public class CartDao { // 获取与用户匹配的 flower_id flower_count 集合 public static List
getFlowerId(int user_id) throws SQLException { // 拿到执行者对象 QueryRunner r = new QueryRunner(DBUtil.getDataSource()); // sql String sql = "select flower_id from cart where user_id= ?"; // 通过用户的id获取cart 中的 flower id return (List ) r.query(sql, new ColumnListHandler(),user_id); } // 获取匹配用户的flower_count public static int getFlowerCount(int user_id,int flower_id) throws SQLException { // 拿到执行者对象 QueryRunner r = new QueryRunner(DBUtil.getDataSource()); // sql String sql = "select flower_count from cart where user_id = ? and flower_id=?"; // 返回 flower_count; return (int) r.query(sql,new ScalarHandler(),user_id,flower_id); } // 新建 public static void addFlower(int user_id,int flower_id) throws SQLException { // 拿到执行者对象 QueryRunner r = new QueryRunner(DBUtil.getDataSource()); // sql String sql = "insert into cart (user_id,flower_id,flower_count) values(?,?,?)"; // 执行 r.update(sql,user_id,flower_id,1); } // 修改数量 public static void updateFlowerCount(int user_id , int flower_id, int flower_count) throws SQLException { // 拿到执行者对象 QueryRunner r =new QueryRunner(DBUtil.getDataSource()); // sql String sql = "update cart set flower_count= ? where user_id=? and flower_id=?"; // 执行 r.update(sql,flower_count,user_id,flower_id); } // 删除flower public static void deleteFLower(int user_id , int flower_id) throws SQLException { // 拿到执行者对象 QueryRunner r = new QueryRunner((DBUtil.getDataSource())); // sql String sql = "delete from cart where user_id=? and flower_id=?"; // 执行 r.update(sql,user_id,flower_id); } // 获取用户的所有flower public static List getUserFlowers(int user_id) throws SQLException { // 拿到执行者对象 QueryRunner r = new QueryRunner(DBUtil.getDataSource()); // sql String sql = "select id,name,price,kind,path from flowers,cart where id=flower_id and user_id=?"; // 执行 return r.query(sql,new BeanListHandler (Flower.class),user_id); } // 判断flower是否存在 public static boolean isFlowerExist(int user_id,int flower_id) throws SQLException { // 拿到执行者对象 QueryRunner r = new QueryRunner(DBUtil.getDataSource()); // sql String sql = "select * from cart where user_id = ? and flower_id = ?"; // 执行 Flower flower = r.query(sql,new BeanHandler (Flower.class),user_id,flower_id); if (flower==null){ return false; }else{ return true; } } } service包
UserService:
package service; import dao.FlowerDao; import dao.UserDao; import model.Flower; import model.User; import java.sql.SQLException; import java.util.List; public class UserService { // 注册功能 public boolean register(User user) { try { // 用户是否注册 if (UserDao.isUsernameExist(user.getUsername())) { return false; } else { UserDao.addUser(user); return true; } } catch (SQLException throwables) { throwables.printStackTrace(); } return false; } // 登录业务 public static User login(String username, String password) { User user = null; try { user = UserDao.selectUsernamePassword(username, password); } catch (SQLException throwables) { throwables.printStackTrace(); } return user; } // 获取全部用户 public static List getAllUser() { try { return UserDao.getAllUser(); } catch (SQLException throwables) { throwables.printStackTrace(); } return null; } // 注销用户 public static void deleteUser(int id) { try { UserDao.deleteUser(id); } catch (SQLException throwables) { throwables.printStackTrace(); } } // 修改用户信息 业务 public static void updateUser(User user) { try { UserDao.updateUser(user); } catch (SQLException throwables) { throwables.printStackTrace(); } } }
FlowerService:
package service; import dao.FlowerDao; import model.Flower; import java.sql.SQLException; import java.util.List; public class FlowerService { // 加载页面时,获取全部商品列表展示 public static List
getGoodsList(){ List list = null; try { list= FlowerDao.getAll(); } catch (SQLException throwables) { throwables.printStackTrace(); } return list; } // 查询业务 public static List getSearchFlower(String name) throws SQLException { return FlowerDao.getSearchFlower(name); } // 删除业务 public static void deleteFlower(int id){ try { // id存在 if(FlowerDao.getFlowerById(id)!=null){ // 删除 FlowerDao.deleteFlower(id); } } catch (SQLException throwables) { throwables.printStackTrace(); } } // 修改花 public static void updateFlower(Flower flower){ try { // 更新 FlowerDao.updateFlower(flower); } catch (SQLException throwables) { throwables.printStackTrace(); } } // 添加 public static void addFlower(Flower flower){ try { FlowerDao.addFlower(flower); } catch (SQLException throwables) { throwables.printStackTrace(); } } } ManagerService:
package service; import dao.ManagerDao; import model.Manager; import java.sql.SQLException; public class ManagerService { public static Manager login(String name,String password) throws SQLException { Manager manager=ManagerDao.selectManagernamePassword(name,password); return manager; } }
CartService
package service; import dao.CartDao; import model.Flower; import model.User; import utils.PriceUtil; import java.sql.SQLException; import java.util.HashMap; import java.util.List; import java.util.Map; public class CartService { // 增加 public static void addGoods(User user, Flower flower) throws SQLException { // 购物车中存在,数量加一 if(CartDao.isFlowerExist(user.getId(),flower.getId())){ CartDao.updateFlowerCount(user.getId(),flower.getId(), CartDao.getFlowerCount(user.getId(),flower.getId())+1); return; } CartDao.addFlower(user.getId(), flower.getId()); } // 删除 public static void deletGoods(User user,Flower flower) throws SQLException { // 删除相应对象 CartDao.deleteFLower(user.getId(),flower.getId()); } // 减一 public static void cutGoods(User user,Flower flower) throws SQLException { int count =CartDao.getFlowerCount(user.getId(),flower.getId()); if(count>1){ CartDao.updateFlowerCount(user.getId(),flower.getId(),count-1); } } // 总价 public static double priceGoods(Map
cart){ double price=0; for (Flower f:cart.keySet() ) { price = PriceUtil.add(price,Double.parseDouble(f.getPrice())*cart.get(f)); } return price; } // 购物车初始化 public static Map getUserCart(User user) throws SQLException { // 创建map对象 Map cart=new HashMap (); // 获取flower对象 List flowers= CartDao.getUserFlowers(user.getId()); for (Flower f:flowers ) { cart.put(f,CartDao.getFlowerCount(user.getId(),f.getId())); } return cart; } } servlet包
CartAddservlet:
package servlet; import dao.FlowerDao; import model.Flower; import model.User; import service.CartService; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; import java.sql.SQLException; import java.util.Map; @WebServlet("/CartAddServlet") public class CartAddServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 从session域中获取cart对象 HttpSession session = req.getSession(); User user = (User) session.getAttribute("user"); Map
cart = (Map ) session.getAttribute("cart"); // 获取传入id int id = Integer.parseInt(req.getParameter("id")); Flower flower = null; // 通过id查看是否存在该商品 try { flower = FlowerDao.getFlowerById(id); if(flower!=null){ CartService.addGoods(user,flower); } // 重定向到cart页面 resp.sendRedirect("goods_cart"); } catch (SQLException throwables) { throwables.printStackTrace(); } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req,resp); } } CartCutServlet:
package servlet; import dao.FlowerDao; import model.Flower; import model.User; import service.CartService; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; import java.sql.SQLException; import java.util.Map; @WebServlet("/CartCutServlet") public class CartCutServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 从session域中获取cart对象 HttpSession session = req.getSession(); User user = (User) session.getAttribute("user"); Map
cart = (Map ) session.getAttribute("cart"); // 获取传入id int id = Integer.parseInt(req.getParameter("id")); Flower flower = null; // 通过id查看是否存在该商品 try { flower = FlowerDao.getFlowerById(id); if(flower!=null){ CartService.cutGoods(user,flower); } // 重定向到cart页面 resp.sendRedirect("goods_cart"); } catch (SQLException throwables) { throwables.printStackTrace(); } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req,resp); } } CartDeleteServlet:
package servlet; import dao.FlowerDao; import model.Flower; import model.User; import service.CartService; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; import java.sql.SQLException; import java.util.HashMap; import java.util.Map; @WebServlet("/CartDeleteServlet") public class CartDeleteServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 从session域中取出 cart对象 HttpSession session = req.getSession(); User user = (User) session.getAttribute("user"); Map
cart = (HashMap ) session.getAttribute("cart"); // 从req域中获取 flower Flower flower = null; // 通过id查看是否有该商品 int id = Integer.parseInt(req.getParameter("id")); try { flower = FlowerDao.getFlowerById(id); if(flower != null){ // 如果不为空,调用函数 CartService.deletGoods(user,flower); } // 重定向到购物车页面 resp.sendRedirect("goods_cart"); } catch (SQLException throwables) { throwables.printStackTrace(); } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req,resp); } } CartServlet:
package servlet; import model.Flower; import model.User; import service.CartService; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; import java.sql.SQLException; import java.util.HashMap; import java.util.Map; @WebServlet(name = "CartServlet",urlPatterns = "https://blog.csdn.net/goods_cart") public class CartServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 从session域中取出 cart 对象 HttpSession session = req.getSession(); User user = (User) session.getAttribute("user"); // 用户为空,重定向到登录页面 if(user == null){ resp.sendRedirect("https://blog.csdn.net/Wksycxy/article/details/user_login.jsp"); return; } try { session.setAttribute("cart", CartService.getUserCart(user)); } catch (SQLException throwables) { throwables.printStackTrace(); } Map
cart = (HashMap ) session.getAttribute("cart"); // 判断购物车是否为空 if(cart.isEmpty()){ // // 为空,重定向到蛋糕展示列表 resp.sendRedirect("https://blog.csdn.net/Wksycxy/article/details/goods_detail.jsp"); }else{ // 不为空,转发到goods_cart.jsp req.getRequestDispatcher("goods_cart.jsp").forward(req,resp); } } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } } GoodsServlet:
package servlet; import service.FlowerService; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet(name = "GoodsServlet",urlPatterns = "/https://blog.csdn.net/Wksycxy/article/details/goods_detail") public class GoodsServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 在请求域中添加goods req.setAttribute("goods", FlowerService.getGoodsList()); // 跳转 req.getRequestDispatcher("/https://blog.csdn.net/Wksycxy/article/details/goods_detail.jsp").forward(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
ManagerActionServlet:
package servlet; import dao.FlowerDao; import dao.UserDao; import service.FlowerService; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.sql.SQLException; @WebServlet("/manager_action") public class ManagerActionServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 获取请求域对象 int id = Integer.parseInt(req.getParameter("id")); // 设置请求域 try { req.setAttribute("flower", FlowerDao.getFlowerById(id)); } catch (SQLException throwables) { throwables.printStackTrace(); } // 转发 req.getRequestDispatcher("update_flower.jsp").forward(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
ManagerAddFlowerServlet:
package servlet; import model.Flower; import org.apache.commons.beanutils.BeanUtils; import service.FlowerService; import service.ManagerService; import utils.UploadUtils; import javax.servlet.ServletException; import javax.servlet.annotation.MultipartConfig; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.Part; import java.io.IOException; import java.lang.reflect.InvocationTargetException; @WebServlet("/add_flower") @MultipartConfig public class ManagerAddFlowerServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { Part part = req.getPart("photo"); // 获取文件名 String path = UploadUtils.upload(part,req,resp);//调用工具类中的方法 if(path==""){ return; } Flower flower = new Flower(); try { BeanUtils.copyProperties(flower,req.getParameterMap()); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } flower.setPath(path); // 添加 FlowerService.addFlower(flower); req.setAttribute("msg","添加成功"); req.getRequestDispatcher("https://blog.csdn.net/Wksycxy/article/details/add_flower.jsp").forward(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
ManagerDeleteFlowerServlet:
package servlet; import service.FlowerService; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet("/delete_flower") public class ManagerDeleteFlowerServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 获取前台数据 int id = Integer.parseInt(req.getParameter("id")); FlowerService.deleteFlower(id); // 重定向 resp.sendRedirect("/https://blog.csdn.net/Wksycxy/article/details/manager_detail"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req,resp); } }
ManagerDeleteUserServlet:
package servlet; import service.UserService; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet("/delete_user") public class ManagerDeleteUserServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 获取请求域参数 int id = Integer.parseInt(req.getParameter("id")); // 删除 UserService.deleteUser(id); // 重定向 resp.sendRedirect("/https://blog.csdn.net/Wksycxy/article/details/manager_detail"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req,resp); } }
ManagerDetailServlet:
package servlet; import service.FlowerService; import service.UserService; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet("/https://blog.csdn.net/Wksycxy/article/details/manager_detail") public class ManagerDetailServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 在请求域中添加flowes users req.setAttribute("flowers", FlowerService.getGoodsList()); req.setAttribute("users", UserService.getAllUser()); // 跳转 req.getRequestDispatcher("/https://blog.csdn.net/Wksycxy/article/details/manager_detail.jsp").forward(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
ManagerLoginServlet:
package servlet; import model.Manager; import service.ManagerService; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; import java.sql.SQLException; @WebServlet("/manager_login") public class ManagerLoginServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 接受请求 接受参数 username password String name = req.getParameter("name"); String password = req.getParameter("password"); Manager manager = null; try { manager = ManagerService.login(name, password); } catch (SQLException throwables) { throwables.printStackTrace(); } if (manager == null) { // 失败给出响应 req.setAttribute("msg", "用户名或者密码错误,请重新登录"); req.getRequestDispatcher("/https://blog.csdn.net/Wksycxy/article/details/user_login.jsp").forward(req, resp); } else { // 成功给出响应 // 将manager ,存在会话中 req.getSession().setAttribute("manager", manager); req.getRequestDispatcher("/https://blog.csdn.net/Wksycxy/article/details/manager_detail").forward(req, resp); } } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } }
ManagerUpdateFlowerServlet:
package servlet; import model.Flower; import org.apache.commons.beanutils.BeanUtils; import service.FlowerService; import utils.UploadUtils; import javax.servlet.ServletException; import javax.servlet.annotation.MultipartConfig; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.Part; import java.io.IOException; import java.lang.reflect.InvocationTargetException; @WebServlet("/update_flower") @MultipartConfig public class ManagerUpdateFlowerServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { Part part = req.getPart("photo"); // 获取文件名 String path = UploadUtils.upload(part, req, resp);//调用工具类中的方法 // 封装 Flower flower = new Flower(); try { BeanUtils.copyProperties(flower,req.getParameterMap()); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } if(path!=null){ flower.setPath(path); } // 执行更新业务 FlowerService.updateFlower(flower); // 跳转页面 resp.sendRedirect("/https://blog.csdn.net/Wksycxy/article/details/manager_detail"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
PurchaseServlet:
package servlet; import dao.FlowerDao; import model.Flower; import model.User; import service.CartService; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.*; import java.io.IOException; import java.sql.SQLException; import java.util.HashMap; import java.util.Map; @WebServlet(name = "PurchaseServlet",urlPatterns = "/PurchaseServlet") public class PurchaseServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 1、用户未登录,跳转登录页面 User user = (User) req.getSession().getAttribute("user"); if (user == null) { // 跳转 req.setAttribute("msg", "请登录"); req.getRequestDispatcher("/https://blog.csdn.net/Wksycxy/article/details/user_login.jsp").forward(req, resp); } else { Flower flower = null; // 通过id查看是否有该商品 int id = Integer.parseInt(req.getParameter("id")); // 没有,则重定向到商品展示列表 try { flower = FlowerDao.getFlowerById(id); if(flower == null){ resp.sendRedirect("https://blog.csdn.net/Wksycxy/article/details/goods_detail.jsp"); return; } } catch (SQLException throwables) { throwables.printStackTrace(); } // 有,加入购物车 // 从session域中获取购物车 HttpSession session = req.getSession(); // 购物车 就是一个Map集合 // key:flower value: count Map
cart=(Map) session.getAttribute("cart"); // cart是否存在 // 不存在,创建购物车 if(cart == null){ cart = new HashMap (); // 存在session域中 session.setAttribute("cart",cart); } // 无论存在与否,都要往购物车中,添加商品 try { CartService.addGoods(user,flower); } catch (SQLException throwables) { throwables.printStackTrace(); } Cookie cookie = new Cookie("JSESSIONID",session.getId()); // 将session对应的ID持久化30分钟,目的和session域中存储的对象持久的时间一致 cookie.setMaxAge(60*30); cookie.setPath("/"); resp.addCookie(cookie); // 重定向到购物车页面中 resp.sendRedirect("goods_cart"); } } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } } https://blog.csdn.net/Wksycxy/article/details/SignOutServlet:
package servlet; import model.User; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; @WebServlet("/https://blog.csdn.net/Wksycxy/article/details/SignOut") public class https://blog.csdn.net/Wksycxy/article/details/SignOutServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 删除session域中的user,和manager req.getSession().removeAttribute("user"); req.getSession().removeAttribute("manager"); // 重定向到首页 resp.sendRedirect("https://blog.csdn.net/Wksycxy/article/details/goods_detail"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req,resp); } }
UserLoginServlet:
package servlet; import dao.CartDao; import dao.UserDao; import model.User; import service.CartService; import service.UserService; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; import java.sql.SQLException; @WebServlet(name = "UserLoginServlet", urlPatterns = "/user_login") public class UserLoginServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 判断用户是否已经登录 HttpSession session = req.getSession(); // 接受请求 接受参数 username password String username = req.getParameter("username"); String password = req.getParameter("password"); // 调用业务层 login =>user User user = UserService.login(username, password); if (user == null) { // 失败给出响应 req.setAttribute("msg", "用户名或者密码错误,请重新登录"); req.getRequestDispatcher("/https://blog.csdn.net/Wksycxy/article/details/user_login.jsp").forward(req, resp); } else { // 成功给出响应 // 将账号密码存在会话中 req.getSession().setAttribute("user", user); req.getRequestDispatcher("/https://blog.csdn.net/Wksycxy/article/details/goods_detail").forward(req, resp); } } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } }
UserRegisterServlet:
package servlet; import model.User; import org.apache.commons.beanutils.BeanUtils; import service.UserService; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.lang.reflect.InvocationTargetException; @WebServlet(name = "UserRegisterServlet",urlPatterns = "/user_register") public class UserRegisterServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 拿到前台数据 封装 User user = new User(); try { BeanUtils.copyProperties(user,req.getParameterMap()); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } UserService uService = new UserService(); // 调用业务层 判断 if(uService.register(user)){ req.setAttribute("msg","注册成功,请登录"); req.getRequestDispatcher("https://blog.csdn.net/Wksycxy/article/details/user_login.jsp").forward(req,resp); }else{ req.setAttribute("msg","用户名存在"); req.getRequestDispatcher("https://blog.csdn.net/Wksycxy/article/details/user_register.jsp").forward(req,resp); } } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req,resp); } }
UserSearchServlet:
package servlet; import model.Manager; import service.FlowerService; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.sql.SQLException; @WebServlet("/user_search") public class UserSearchServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 获取req域中flowerName String flowerName= req.getParameter("flowerName"); try { // 判断flowerName是否为空 if(flowerName.trim().equals("")||flowerName==null){ resp.sendRedirect("/https://blog.csdn.net/Wksycxy/article/details/goods_detail"); return; }else{ req.setAttribute("goods" , FlowerService.getSearchFlower(flowerName)); req.getRequestDispatcher("/https://blog.csdn.net/Wksycxy/article/details/goods_detail.jsp").forward(req,resp); } } catch (SQLException throwables) { throwables.printStackTrace(); } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
UserUpdateServlet:
package servlet; import dao.UserDao; import model.User; import org.apache.commons.beanutils.BeanUtils; import service.UserService; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.lang.reflect.InvocationTargetException; @WebServlet("/user_update") public class UserUpdateServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { User user = new User(); try { BeanUtils.copyProperties(user,req.getParameterMap()); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } // 更新 UserService.updateUser(user); user=UserService.login(user.getUsername(),user.getPassword()); req.getSession().setAttribute("user",user); // 重定向 resp.sendRedirect("/https://blog.csdn.net/Wksycxy/article/details/user_update.jsp"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
2.web
jsp
https://blog.csdn.net/Wksycxy/article/details/add_flower.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
增加花品 增加花品${msg }goods_cart.jsp
<%@ page import="java.util.Map" %> <%@ page import="model.Flower" %> <%@ page import="java.util.HashMap" %> <%@ page import="service.FlowerService" %> <%@ page import="service.CartService" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
我的购物车 https://blog.csdn.net/Wksycxy/article/details/goods_detail.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
首页 ¥${g.price} ${g.name}加入购物车header.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
首页 https://blog.csdn.net/Wksycxy/article/details/manager_detail.jsp
<%-- Created by IntelliJ IDEA. User: 666 Date: 2022/12/27 Time: 17:14 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
用户货物管理 https://blog.csdn.net/Wksycxy/article/details/manager_login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
管理员登录 <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>小熙花店 ${msg }管理员登录update_flower.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
修改花朵信息 信息修改https://blog.csdn.net/Wksycxy/article/details/user_login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
小熙花店 ${msg }用户登录https://blog.csdn.net/Wksycxy/article/details/user_register.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
小熙花店 ${msg }注册新用户https://blog.csdn.net/Wksycxy/article/details/user_update.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
个人中心 ${msg }个人中心css样式
bootstrap样式自行导入,文章太长了,敲个字已经开始卡了,溜了
三、源码
JavaWeb+MySql实现简易商城系统源码
https://github.com/muyixiaoxi/florist.git
还没有评论,来说两句吧...