当前位置:   article > 正文

JavaWeb+MySql实现简易商城系统_html+servlet+mysql简单java系统

html+servlet+mysql简单java系统

JavaWeb+MySql实现简易商城系统



前言

该系统使用MVC架构实现了商城的购物车功能,除此之外,基本上都是不同实体之间的增删改查。在一些地方还需要优化(但是我懒癌犯了,不想改了)例如在管理员和用户登录时,应该是由同一个窗口登录,通过数据库存储的字段判断跳转页面。除此之外还有一个问题,当管理员给商品更换图片后,在idea中图片加载成功,在数据库中存储地址也没有问题,但是在页面中有时候不能正常刷新出来,需要再次刷新。


一、系统结构

E-R图展示:

工具是为了解决数据分析任务而创建的。

项目结构:

在这里插入图片描述

效果展示:

首页

在这里插入图片描述

购物车

在这里插入图片描述

管理员

在这里插入图片描述

修改商品

在这里插入图片描述

二、具体实现

1.src

配置c3p0文件:

<?xml version="1.0" encoding="UTF-8" ?>
<c3p0-config>

    <default-config>
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/florist</property>
        <property name="user">root</property>
        <property name="password">15194538986a</property>
    </default-config>
</c3p0-config>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

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 {

    }

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

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();
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

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();
    }


}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

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;//返回图片的名字
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47

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;
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64

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;
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

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;
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71

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>(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>(User.class),username,password);
    }

//    获取全部用户
    public static List<User> getAllUser() throws SQLException {
//        拿到执行者对象
        QueryRunner r =new QueryRunner(DBUtil.getDataSource());
//        sql
        String sql = "select * from user";
//        执行sql
        return r.query(sql,new BeanListHandler<User>(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>(User.class),id);
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90

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<Flower> getAll() throws SQLException {
//        拿到执行者对象
        QueryRunner r = new QueryRunner(DBUtil.getDataSource());
//        sql
        String sql = "select * from flowers";
//        执行
        return r.query(sql,new BeanListHandler<Flower>(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>(Flower.class),id);
    }

//    模糊查询
    public static List<Flower> 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>(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());
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83

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>(Manager.class),name,password);
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

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<Integer> 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<Integer>) 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<Flower> 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>(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>(Flower.class),user_id,flower_id);
        if (flower==null){
            return false;
        }else{
            return true;
        }
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95

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();
        }
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74

FlowerService:

package service;

import dao.FlowerDao;
import model.Flower;

import java.sql.SQLException;
import java.util.List;

public class FlowerService {

//    加载页面时,获取全部商品列表展示
    public static List<Flower> getGoodsList(){
        List <Flower> list = null;
        try {
            list= FlowerDao.getAll();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return list;
    }

    //    查询业务
    public static List<Flower> 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();
        }
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60

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;
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

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<Flower,Integer> 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<Flower,Integer> getUserCart(User user) throws SQLException {
//        创建map对象
        Map<Flower,Integer>cart=new HashMap<Flower,Integer>();
//        获取flower对象
        List<Flower> flowers= CartDao.getUserFlowers(user.getId());

        for (Flower f:flowers
             ) {
            cart.put(f,CartDao.getFlowerCount(user.getId(),f.getId()));
        }

        return cart;
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65

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<Flower,Integer> cart = (Map<Flower, Integer>) 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);
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

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<Flower,Integer> cart = (Map<Flower, Integer>) 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);
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

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<Flower,Integer> cart = (HashMap<Flower,Integer>) 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);
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53

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 = "/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("user_login.jsp");
            return;
        }

        try {
            session.setAttribute("cart", CartService.getUserCart(user));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        Map<Flower,Integer> cart = (HashMap<Flower,Integer>) session.getAttribute("cart");

//        判断购物车是否为空
        if(cart.isEmpty()){
//
//            为空,重定向到蛋糕展示列表
            resp.sendRedirect("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);
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57

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 = "/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("/goods_detail.jsp").forward(req,resp);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

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);
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

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("add_flower.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56

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("/manager_detail");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

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("/manager_detail");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

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("/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("/manager_detail.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

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("/user_login.jsp").forward(req, resp);
        } else {
//            成功给出响应
//            将manager ,存在会话中
            req.getSession().setAttribute("manager", manager);
            req.getRequestDispatcher("/manager_detail").forward(req, resp);
        }
    }


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

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("/manager_detail");
    }



    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57

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("/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("goods_detail.jsp");
                    return;
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

//            有,加入购物车
//            从session域中获取购物车
            HttpSession session = req.getSession();
//            购物车 就是一个Map集合
//            key:flower    value: count
            Map<Flower,Integer> cart=(Map) session.getAttribute("cart");
//            cart是否存在
//            不存在,创建购物车
            if(cart == null){
                cart = new HashMap<Flower,Integer>();
//                存在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);
    }

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77

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("/SignOut")
public class 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("goods_detail");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

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("/user_login.jsp").forward(req, resp);
        } else {
//            成功给出响应
//            将账号密码存在会话中
            req.getSession().setAttribute("user", user);
            req.getRequestDispatcher("/goods_detail").forward(req, resp);
        }

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51

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("user_login.jsp").forward(req,resp);
        }else{
            req.setAttribute("msg","用户名存在");
            req.getRequestDispatcher("user_register.jsp").forward(req,resp);
        }

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

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("/goods_detail");
                    return;
                }else{
                    req.setAttribute("goods" , FlowerService.getSearchFlower(flowerName));
                    req.getRequestDispatcher("/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);
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

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("/user_update.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

2.web

jsp

add_flower.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>增加花品</title>
    <link rel="stylesheet" type="text/css" href="css/bootstrap.css">
    <link rel="stylesheet" type="text/css" href="css/florist.css">
    <script src="js/florist.js"></script>
</head>
<body>
<div class="login">
    <jsp:include page="header.jsp"></jsp:include>
    <div class="title">增加花品</div>
    <c:if test="${!empty msg }">
        <div class="tips">${msg }</div>
    </c:if>
    <form action="/add_flower" method="post" enctype="multipart/form-data">
        <div class="input">
            <div class="img">
                <img src="img/" class="img-thumbnail" id="img">
            </div>
            <div class="mb-3">
                <label for="file" class="form-label">图片</label>
                <input class="form-control" name="photo" type="file" id="file" required="required">
            </div>
        </div>

        <div class="input">
            <span>花名</span>
            <input type="text" name="name" required="required">
        </div>
        <div class="input">
            <span>价格</span>
            <input type="text" name="price"  required="required">
        </div>
        <div class="input">
            <span>种类</span>
            <input type="text" name="kind" required="required">
        </div>
        <div class="sub">
            <input type="submit" value="提交">
        </div>
    </form>
</div>
</body>
</html>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46

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" %>
<html>
<head>
    <title>我的购物车</title>
    <link rel="stylesheet" type="text/css" href="css/bootstrap.css">
    <link rel="stylesheet" type="text/css" href="css/florist.css">
</head>
<body>
<jsp:include page="header.jsp"></jsp:include>
<div class="container">
    <table class="table">
        <thead>
        <tr>
            <th scope="col">商品</th>
            <th scope="col">单价</th>
            <th scope="col">数量</th>
            <th scope="col" style="width: 25%">操作</th>
        </tr>
        </thead>
        <tbody>
        <%--        遍历输出--%>
        <c:forEach items="${cart.keySet()}" var="c">
            <tr>
                <td>
                  <div class="td1">
                      <div class="cart_img">
                          <img src="img/${c.path}">
                      </div>
                      <div class="cart_name">
                              ${c.name}
                      </div>
                  </div>
                </td>
                <td>
                    <div class="cart_price">
                            ${c.price}
                    </div>
                </td>
                <td>
                    <div class="cart_count">
                            ${cart.get(c)}
                    </div>
                </td>
                <td>
                    <div class="cart_update">
                        <a href="CartAddServlet?id=${c.id}" class="btn btn-primary">增加</a>
                        <a href="CartCutServlet?id=${c.id}" class="btn btn-success">减少</a>
                        <a href="CartDeleteServlet?id=${c.id}" class="btn btn-danger">删除</a>
                    </div>
                </td>
            </tr>
        </c:forEach>
        <%--总价--%>
        </tbody>
    </table>
    <div class="price">
        <%
            Map<Flower,Integer> cart =(HashMap<Flower,Integer>)request.getSession().getAttribute("cart");
            out.print(CartService.priceGoods(cart));
        %>
    </div>
</div>
</body>
</html>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71

goods_detail.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>首页</title>
    <link rel="stylesheet" type="text/css" href="css/bootstrap.css">
    <link rel="stylesheet" type="text/css" href="css/florist.css">
</head>
<body>
<jsp:include page="header.jsp"></jsp:include>
<div class="container">
    <div class="bg"><img src="img/bg.jpeg"></div>
    <div class="flo_div">
        <c:forEach items="${goods}" var="g">
            <div>
                <img src="img/${g.path}">
                <div class="price">¥${g.price} <span class="goodName">${g.name}</span></div>
                <a href="PurchaseServlet?id=${g.id}" class="btn btn-danger">加入购物车</a>
            </div>
        </c:forEach>
    </div>

</div>

</body>
</html>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

header.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>首页</title>
    <link rel="stylesheet" type="text/css" href="css/bootstrap.css">
</head>
<body>
<div class="container">
    <nav class="navbar navbar-expand-lg bg-light">
        <div class="container-fluid">
            <a class="navbar-brand" href="#">小熙花店</a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse"
                    data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent"
                    aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarSupportedContent">
                <ul class="navbar-nav me-auto mb-2 mb-lg-0">
                    <li class="nav-item">
                        <c:if test="${!empty manager}">
                            <a class="nav-link active" aria-current="page" href="manager_detail">首页</a>
                        </c:if>
                        <c:if test="${empty manager}">
                            <a class="nav-link active" aria-current="page" href="goods_detail">首页</a>
                        </c:if>
                    </li>
                    <c:if test="${!empty user}">
                        <li class="nav-item">
                            <a class="nav-link" href="user_update.jsp">个人中心</a>
                        </li>
                    </c:if>
                    <c:if test="${empty user && empty manager}">
                        <li class="nav-item">
                            <a class="nav-link" href="user_register.jsp">注册</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="user_login.jsp">登录</a>
                        <li class="nav-item">
                            <a class="nav-link" href="manager_login.jsp">管理员</a>
                        </li>
                    </c:if>
                    <c:if test="${!empty manager}">
                        <li class="nav-item">
                            <a class="nav-link" href="add_flower.jsp">增加花品</a>
                        </li>
                    </c:if>
                    <c:if test="${!empty user||!empty manager}">
                        <li class="nav-item">
                            <a class="nav-link" href="SignOut">退出登录</a>
                        </li>
                    </c:if>
                </ul>
            </div>
            <div class="d-flex">
                <c:if test="${empty manager}">
                    <form class="d-flex" role="search" action="user_search" method="get">
                        <input class="form-control me-2" type="search" placeholder="Search" aria-label="Search"
                               name="flowerName">
                        <button class="btn btn-outline-info" type="submit">
                            <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor"
                                 class="bi bi-search" viewBox="0 0 16 16">
                                <path d="M11.742 10.344a6.5 6.5 0 1 0-1.397 1.398h-.001c.03.04.062.078.098.115l3.85 3.85a1 1 0 0 0 1.415-1.414l-3.85-3.85a1.007 1.007 0 0 0-.115-.1zM12 6.5a5.5 5.5 0 1 1-11 0 5.5 5.5 0 0 1 11 0z"/>
                            </svg>
                        </button>
                    </form>
                    <div class="header-right">
                        <a class="nav-link" href="/goods_cart">
                            <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor"
                                 class="bi bi-cart-dash-fill" viewBox="0 0 16 16">
                                <path d="M.5 1a.5.5 0 0 0 0 1h1.11l.401 1.607 1.498 7.985A.5.5 0 0 0 4 12h1a2 2 0 1 0 0 4 2 2 0 0 0 0-4h7a2 2 0 1 0 0 4 2 2 0 0 0 0-4h1a.5.5 0 0 0 .491-.408l1.5-8A.5.5 0 0 0 14.5 3H2.89l-.405-1.621A.5.5 0 0 0 2 1H.5zM6 14a1 1 0 1 1-2 0 1 1 0 0 1 2 0zm7 0a1 1 0 1 1-2 0 1 1 0 0 1 2 0zM6.5 7h4a.5.5 0 0 1 0 1h-4a.5.5 0 0 1 0-1z"/>
                            </svg>
                        </a>
                    </div>
            </div>
        </c:if>
        </div>
    </nav>

</div>
</body>
</html>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83

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" %>
<html>
<head>
    <title>用户货物管理</title>
    <link rel="stylesheet" type="text/css" href="css/bootstrap.css">
    <link rel="stylesheet" type="text/css" href="css/florist.css">
</head>
<body>
<jsp:include page="header.jsp"></jsp:include>
<div class="container">
    <p class="lead">
        商品信息
    </p>
    <table class="table">
        <thead>
        <tr>
            <th scope="col">商品</th>
            <th scope="col">单价</th>
            <th scope="col">种类</th>
            <th scope="col" style="width: 15%">操作</th>
        </tr>
        </thead>
        <tbody>
        <%--        遍历输出--%>
        <c:forEach items="${flowers}" var="f">
            <tr>
                <td>
                    <div class="td1">
                        <div class="cart_img">
                            <img src="img/${f.path}">
                        </div>
                        <div class="cart_name">
                                ${f.name}
                        </div>
                    </div>
                </td>
                <td>
                    <div class="cart_price">
                            ${f.price}
                    </div>
                </td>
                <td>
                    <div class="cart_count">
                            ${f.kind}
                    </div>
                </td>
                <td>
                    <div class="cart_update">
                        <a href="manager_action?id=${f.id}" class="btn btn-success">修改</a>
                        <a href="delete_flower?id=${f.id}" class="btn btn-danger">删除</a>
                    </div>
                </td>
            </tr>
        </c:forEach>
        </tbody>
    </table>
    <p class="lead">
        用户信息
    </p>
    <table class="table">
        <thead>
        <tr>
            <th scope="col">用户名</th>
            <th scope="col">密码</th>
            <th scope="col">姓名</th>
            <th scope="col">电话</th>
            <th scope="col">地址</th>
            <th scope="col" style="width: 15%">操作</th>
        </tr>
        </thead>
        <tbody>
        <%--        遍历输出--%>
        <c:forEach items="${users}" var="u">
            <tr>
                <td>
                        ${u.username}
                </td>
                <td>
                        ${u.password}
                </td>
                <td>
                        ${u.name}
                </td>
                <td>
                        ${u.phone}
                </td>
                <td>
                        ${u.address}
                </td>
                <td>
                    <div class="cart_update">
                        <a href="delete_user?id=${u.id}" class="btn btn-danger">删除</a>
                    </div>
                </td>
            </tr>
        </c:forEach>
        </tbody>
    </table>
</div>
</body>
</html>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110

manager_login.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>管理员登录</title>
</head>
<body>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>小熙花店</title>
    <link rel="stylesheet" type="text/css" href="css/florist.css">
</head>
<body>
<jsp:include page="header.jsp"></jsp:include>
<div class="login">
    <c:if test="${!empty msg }">
        <div class="tips">${msg }</div>
    </c:if>
    <div class="title">管理员登录</div>
    <form action="/manager_login" method="post">
        <div class="input">
            <span>管理员 <label style="color: #ff0000;">*</label></span>
            <input type="text" name="name" placeholder="请输入管理员账号" required="required">
        </div>
        <div class="input">
            <span>密码 <label style="color: red;">*</label></span>
            <input type="password" name="password" placeholder="请输入密码" required="required">
        </div>
        <div class="sub">
            <input type="submit" value="提交">
        </div>
    </form>
</div>
</body>
</html>

</body>
</html>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

update_flower.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>修改花朵信息</title>
    <link rel="stylesheet" type="text/css" href="css/bootstrap.css">
    <link rel="stylesheet" type="text/css" href="css/florist.css">
    <script type="text/javascript" src="js/florist.js"></script>
</head>
<body>
<jsp:include page="header.jsp"></jsp:include>
<div class="login">
    <div class="title">信息修改</div>
    <form action="/update_flower" method="post" enctype="multipart/form-data">
        <div class="input" style="display: none">
            <span>id</span>
            <input type="text"  name="id" value=${flower.id} readonly>
        </div>

        <div class="input">
            <div class="img">
                <img src="img/${flower.path}" class="img-thumbnail" id="img">
            </div>
            <div class="mb-3">
                <label for="file" class="form-label">图片</label>
                <input class="form-control" name="photo" type="file" id="file">
            </div>
        </div>

        <div class="input">
            <span>花名</span>
            <input type="text" name="name" value=${flower.name} readonly>
        </div>
        <div class="input">
            <span>价格</span>
            <input type="text" name="price" value="${flower.price}" required="required">
        </div>
        <div class="input">
            <span>种类</span>
            <input type="text" name="kind" value="${flower.kind}" required="required" >
        </div>
        <div class="sub">
            <input type="submit" value="提交">
        </div>
    </form>
</div>
</body>
</html>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

user_login.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>小熙花店</title>
    <link rel="stylesheet" type="text/css" href="css/florist.css">
</head>
<body>
<jsp:include page="header.jsp"></jsp:include>
<div class="login">
    <c:if test="${!empty msg }">
        <div class="tips">${msg }</div>
    </c:if>
    <div class="title">用户登录</div>
    <form action="/user_login" method="post">
        <div class="input">
            <span>用户名 <label style="color: #ff0000;">*</label></span>
            <input type="text" name="username" placeholder="请输入用户名" required="required">
        </div>
        <div class="input">
            <span>密码 <label style="color: red;">*</label></span>
            <input type="password" name="password" placeholder="请输入密码" required="required">
        </div>
        <div class="sub">
            <input type="submit" value="提交">
        </div>
    </form>
</div>
</body>
</html>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

user_register.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>小熙花店</title>
    <link rel="stylesheet" type="text/css" href="css/florist.css">
</head>
<body>
<jsp:include page="header.jsp"></jsp:include>
    <div class="login">
        <c:if test="${!empty msg }">
            <div class="tips">${msg }</div>
        </c:if>
        <div class="title">注册新用户</div>
        <form action="/user_register" method="post">
            <div class="input">
                <span>用户名 <label style="color: #ff0000;">*</label></span>
                <input type="text" name="username" placeholder="请输入用户名" required="required">
            </div>
            <div class="input">
                <span>密码 <label style="color: red;">*</label></span>
                <input type="password" name="password" placeholder="请输入密码" required="required">
            </div>
            <div class="input">
                <span>收货人 <label></label></span>
                <input type="text" name="name"placeholder="请输入收货">
            </div>
            <div class="input">
                <span>收货电话 <label></label></span>
                <input type="text" name="phone" placeholder="请输入电话">
            </div>
            <div class="input">
                <span>收货地址 <label></label></span>
                <input type="text" name="address" placeholder="请输入地址">
            </div>
            <div class="sub">
                <input type="submit" value="提交">
            </div>

        </form>
    </div>
</body>
</html>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

user_update.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>个人中心</title>
    <link rel="stylesheet" type="text/css" href="css/bootstrap.css">
    <link rel="stylesheet" type="text/css" href="css/florist.css">
</head>
<body>
<jsp:include page="header.jsp"></jsp:include>
<div class="login">
    <c:if test="${!empty msg }">
        <div class="tips">${msg }</div>
    </c:if>
    <div class="title">个人中心</div>
    <form action="/user_update" method="post">
        <div class="input">
            <span>用户名</span>
            <input type="text" name="username"value="${user.username}" readonly>
        </div>
        <div class="input">
            <span>密码</span>
            <input type="text" name="password" value="${user.password}" required="required">
        </div>
        <div class="input">
            <span>姓名</span>
            <input type="text" name="name"value="${user.name}"required="required" >
        </div>
        <div class="input">
            <span>电话</span>
            <input type="text" name="phone"value="${user.phone}"required="required">
        </div>
        <div class="input">
            <span>地址</span>
            <input type="text" name="address"value="${user.address}"required="required">
        </div>
        <div class="sub">
            <input type="submit" value="修改">
        </div>
    </form>
</div>
</body>
</html>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

css样式

bootstrap样式自行导入,文章太长了,敲个字已经开始卡了,溜了

三、源码

JavaWeb+MySql实现简易商城系统源码
https://github.com/muyixiaoxi/florist.git

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号