jsp+servlet+tomcat实现网上商城

Java Web 专栏收录该内容
5 篇文章 0 订阅

项目总览

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

api层

entity.Good

package org.example.api.entity;

public class Good {
     String goodName;
     String goodValue;
     String goodId;
     String goodNum;
    public Good(String name, String f, String Id , String Num){
        this.goodName = name;
        this.goodValue= f;
        this.goodId = Id;
        this.goodNum = Num;

    }
    public Good() {

    }

    public String getGoodName() {
        return goodName;
    }

    public void setGoodName(String goodName) {
        this.goodName = goodName;
    }

    public String getGoodValue() {
        return goodValue;
    }

    public void setGoodValue(String goodValue) {
        this.goodValue = goodValue;
    }

    public String getGoodId() {
        return goodId;
    }

    public void setGoodId(String goodId) {
        this.goodId = goodId;
    }

    public String getGoodNum() {
        return goodNum;
    }

    public void setGoodNum(String goodNum) {
        this.goodNum = goodNum;
    }
}

entity.User

package org.example.api.entity;

public class User {


    private int uid;
    public  String userName;
    public  String userPassword;
    private String sex;
    private int age;
    private String telephone;

    public User(String userName, String userPassword) {
        this.userName=userName;
        this.userPassword=userPassword;
    }

    public int getUid() {
        return uid;
    }

    public void setUid(int uid) {
        this.uid = uid;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserPassword() {
        return userPassword;
    }

    public void setUserPassword(String userPassword) {
        this.userPassword = userPassword;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

    public User(int uid, String uname, String pwd, String sex, int age,
                String telephone)
    {
        super();
        this.uid = uid;
        this.userName = uname;
        this.userPassword = pwd;
        this.sex = sex;
        this.age = age;
        this.telephone = telephone;
    }
    public User() {

        super();

    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        User other = (User) obj;
        if (age != other.age)
            return false;
        if (telephone == null) {
            if (other.telephone != null)
                return false;
        } else if (!telephone.equals(other.telephone))
            return false;
        if (userPassword == null) {
            if (other.userPassword != null)
                return false;
        } else if (!userPassword.equals(other.userPassword))
            return false;
        if (sex == null) {
            if (other.sex != null)
                return false;
        } else if (!sex.equals(other.sex))
            return false;
        if (uid != other.uid)
            return false;
        if (userName == null) {
            if (other.userName != null)
                return false;
        } else if (!userName.equals(other.userName))
            return false;
        return true;
    }

    @Override
    public String toString() {
        return "User{" +
                "uid=" + uid +
                ", userName='" + userName + '\'' +
                ", userPassword='" + userPassword + '\'' +
                ", sex='" + sex + '\'' +
                ", age=" + age +
                ", telephone='" + telephone + '\'' +
                '}';
    }
}
##  entity.GoodList

```clike
package org.example.api.entity;

import java.util.ArrayList;
import java.util.stream.Stream;

public class GoodList {
    ArrayList<Good> goodsList = new ArrayList<Good>();



    public ArrayList<Good> getGoodList() {
        return this.goodsList;
    }

    public Good findGood(int i) {
        return goodsList.get(i);
    }

    public void addGoods(Good g) {
        this.goodsList.add(g);
    }

    public void removeGoods(Good g) {
        int index = goodsList.indexOf(g);
        this.goodsList.remove(index);
    }

    public void clearGoods() {
        this.goodsList.clear();
    }
}



entity.OrderList

package org.example.api.entity;

import org.example.api.utils.MyTools;

import java.util.ArrayList;

public class OrderList extends ArrayList {
    private ArrayList<Good> OrderList = new ArrayList<Good>();
    public ArrayList<Good> getOrders(){
        return this.OrderList;
    }
    public Good findOrder(int i) {
        return OrderList.get(i);
    }
    public void addOrder(Good g) {
        if(OrderList.size() == 0) {
            OrderList.add(g);
        }else {
            int i = 0;
            for(; i < OrderList.size(); i++) {
                if(g.getGoodName().equals(OrderList.get(i).getGoodName())) {
                   OrderList.get(i).setGoodId(OrderList.get(i).getGoodId()+1);
                    break;
                }
            }
            if(i >= OrderList.size()) {
                OrderList.add(g);
            }
        }
    }
    public void reduceOrder(Good g) {
        if(OrderList.size() == 0) {
            System.out.println("no item to reduce!");
        }else {
            int i = 0;
            for(; i < OrderList.size(); i++) {
                if(g.getGoodName().equals(OrderList.get(i).getGoodName())) {
                    if(Integer.getInteger(OrderList.get(i).getGoodId()) > 1) {
                        OrderList.get(i).setGoodId(""+(Integer.getInteger(OrderList.get(i).getGoodId())-1));
                        break;
                    }else if(OrderList.get(i).getGoodId() == "1") {
                        OrderList.remove(OrderList.get(i));
                        break;
                    }
                }
            }
        }
    }
    public void removeOrder(Good g) {
        if(OrderList.size() == 0) {
            System.out.println("没有可以删除的选项!");
        }else {
            OrderList.remove(g);
        }
    }
    public void clearItem(String name) {
        if(OrderList.size() == 0) {
        }else {
            int i = 0;
            for(; i < OrderList.size(); i++) {
                if(MyTools.toChinese(name).equals(OrderList.get(i).getGoodName())) {
                   OrderList.remove(i);
                }
            }
        }
    }
    public void clearCar() {
       OrderList.clear();
    }

}


controllor层

AddGoodServlet

package org.example.controllor;

import org.example.api.IOrderService;
import org.example.api.utils.JavaDateConnectionUtils;
import org.example.service.impl.OrderServiceImpl;

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.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

@WebServlet("/AddGood")
public class AddGoodServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 防止乱码
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        // 获取参数内容
        String id = request.getParameter("id");
        String userName = request.getParameter("userName");

        IOrderService iOrderService = new OrderServiceImpl();
        boolean judgment = false;
        try {
            judgment = iOrderService.selectOrder(id,userName);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        if (judgment == true) {
            JavaDateConnectionUtils javaDateConnection = new JavaDateConnectionUtils();
            PreparedStatement preparedStatement = null;
            Connection connection = null;
            try {
                connection = javaDateConnection.getConn();
                String sql = "update t_order set orderNum=orderNum+1 where orderId=? and userName=?";
                preparedStatement = connection.prepareStatement(sql);
                preparedStatement.setString(1, id);
                preparedStatement.setString(2,userName);
                int count = preparedStatement.executeUpdate();
                if (count == 1) {
                    request.getRequestDispatcher("showcar.jsp").forward(request, response);
                }
                else {
                    request.getRequestDispatcher("showcar.jsp").forward(request, response);
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                try {
                    connection.close();
                    preparedStatement.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }

            }
        }
        else {
            request.getRequestDispatcher("showcar.jsp").forward(request, response);
        }
    }
}

carfilter

package org.example.controllor;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebFilter("/showcar.jsp")
public class carFilter implements Filter {
    @Override
    public void destroy() {
        System.out.println("销毁了filter对象");

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        String uri = request.getRequestURI();
        if (uri.contains("/login.jsp") || uri.contains("/loginUser") || uri.contains("/register.jsp") || uri.contains("/delete.jsp") || uri.contains("/insertUser") || uri.contains("/deleteUser") || uri.contains("/css/")){
            filterChain.doFilter(servletRequest,servletResponse);
        }
        else{
            String userName = (String) request.getSession().getAttribute("userName");
            if (userName==null||"".equals(userName)){
                request.setAttribute("msg","登录以查看更多信息");
                request.getRequestDispatcher("/login.jsp").forward(request,servletResponse);

            }else {
                filterChain.doFilter(servletRequest,servletResponse);
            }

        }
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("创建了filter对象");

    }
    }

DeleteUserServlet

package org.example.controllor;
import org.example.api.IUserService;
import org.example.service.impl.UserServiceImpl;
import org.example.api.utils.JavaDateConnectionUtils;

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.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;


@WebServlet("/DeleteUser")
public class DeleteUserServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 防止乱码
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        // 获取表单内容
        String userName = request.getParameter("userName");
        String userPassword = request.getParameter("userPassword");
        String telephone = request.getParameter("telephone");
        System.out.println(userName + userPassword + telephone);
        // 判断表单内容是否为空
        if (userName == null || userName.trim() == "" || userPassword == null || userPassword.trim() == "" || telephone == null || telephone.trim() == "" ) {
            request.setAttribute("msg", "用户信息填写不完整!!!请重新进行注销");
            request.getRequestDispatcher("delete.jsp").forward(request, response);
        } else {
            IUserService iUserService = new UserServiceImpl();
            boolean judgment = true;
            try {
                judgment = iUserService.insertUser(userName, userPassword);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            if (judgment == true) {
                JavaDateConnectionUtils javaDateConnection = new JavaDateConnectionUtils();
                PreparedStatement preparedStatement = null;

                Connection connection = null;

                try {
                    connection = javaDateConnection.getConn();
                    String sql = "delete from t_user where userName=? and userPassword=? and telephone=?";
                    preparedStatement = connection.prepareStatement(sql);
                    preparedStatement.setString(1, userName);
                    preparedStatement.setString(2, userPassword);
                    preparedStatement.setString(3, telephone);
                    int count = preparedStatement.executeUpdate();

                    if (count != 0) {
                        request.setAttribute("msg", "用户"+ userName+"注销成功!!!");
                        request.getRequestDispatcher("delete.jsp").forward(request, response);
                    }
                    else {

                        request.setAttribute("msg","用户电话填写有误,请确认用户信息后重试");
                        request.getRequestDispatcher("delete.jsp").forward(request, response);
                    }
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                } finally {
                    try {
                        connection.close();
                        preparedStatement.close();
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }

                }
            }
            else {
                request.setAttribute("msg", "注销失败,请确认账号密码无误后重试");
                request.getRequestDispatcher("delete.jsp").forward(request, response);
            }
        }
    }
}

goodFilter

package org.example.controllor;



import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebFilter("/showgoods.jsp")
public class goodFilter implements Filter {
    @Override
    public void destroy() {
        System.out.println("销毁了filter对象");

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
        String uri = request.getRequestURI();
        if (uri.contains("/login.jsp") || uri.contains("/loginUser") || uri.contains("/register.jsp") || uri.contains("/delete.jsp") || uri.contains("/insertUser") || uri.contains("/deleteUser") || uri.contains("/css/")){
            filterChain.doFilter(servletRequest,servletResponse);
        }
        else{
            String userName = (String) request.getSession().getAttribute("userName");
            if (userName==null||"".equals(userName)){
                request.setAttribute("msg","登录以查看更多信息");
                request.getRequestDispatcher("/login.jsp").forward(request,servletResponse);

            }else {
                filterChain.doFilter(servletRequest,servletResponse);
            }

        }
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("创建了filter对象");

    }
}

InsertGoodServlet

package org.example.controllor;

import org.example.api.IOrderService;
import org.example.api.utils.JavaDateConnectionUtils;
import org.example.service.impl.OrderServiceImpl;

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.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

@WebServlet("/InsertGood")
public class InsertGoodServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 防止乱码
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");

        // 获取参数内容
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String value = request.getParameter("value");
        String userName = request.getParameter("userName");


        IOrderService iOrderService = new OrderServiceImpl();
        boolean judgment = true;
        try {
            judgment = iOrderService.selectOrder(id,userName);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        if (judgment == false) {
            JavaDateConnectionUtils javaDateConnection = new JavaDateConnectionUtils();
            PreparedStatement preparedStatement = null;
            Connection connection = null;
            try {
                connection = javaDateConnection.getConn();
                String sql = "insert into t_order(orderId,orderName,orderValue,orderNum,userName) values (?,?,?,?,?)";
                preparedStatement = connection.prepareStatement(sql);
                preparedStatement.setString(1, id);
                preparedStatement.setString(2, name);
                preparedStatement.setString(3, value);
                preparedStatement.setString(4,"1");
                preparedStatement.setString(5,userName);
                int count = preparedStatement.executeUpdate();
                if (count == 1) {
                    HttpSession httpSession = request.getSession();
                    httpSession.setAttribute("userName",userName);
                    request.getRequestDispatcher("showcar.jsp").forward(request, response);
                }
                else {
                    request.getRequestDispatcher("showcar.jsp").forward(request, response);
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                try {
                    connection.close();
                    preparedStatement.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }

            }
        }
        else {
            request.getRequestDispatcher("showcar.jsp").forward(request, response);
        }
    }
}

InsertUserServlet

package org.example.controllor;
import org.example.api.IUserService;
import org.example.service.impl.UserServiceImpl;
import org.example.api.utils.JavaDateConnectionUtils;

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.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;


@WebServlet("/InsertUser")
public class InsertUserServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 防止乱码
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        // 获取表单内容
        String userName = request.getParameter("userName");
        String userPassword = request.getParameter("userPassword");
        String sex = request.getParameter("sex");
        String age = request.getParameter("age");
        String telephone = request.getParameter("telephone");
        System.out.println(userName + userPassword + sex + age + telephone);
        // 判断表单内容是否为空
        if (userName == null || userName.trim() == "" || userPassword == null || userPassword.trim() == "" || sex == null || sex.trim() == "" || telephone == null || telephone.trim() == "" || age == null || age.trim() == "") {
            request.setAttribute("msg", "用户信息填写不完整!!!请重新进行注册");
            request.getRequestDispatcher("register.jsp").forward(request, response);
        } else {
            IUserService iUserService = new UserServiceImpl();
            boolean judgment = true;
            try {
                judgment = iUserService.loginUser(userName);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            if (judgment == false) {
                JavaDateConnectionUtils javaDateConnection = new JavaDateConnectionUtils();
                PreparedStatement preparedStatement = null;
                Connection connection = null;
                try {
                    connection = javaDateConnection.getConn();
                    String sql = "insert into t_user(userName,userPassword,sex,age,telephone) value(?,?,?,?,?)";
                    preparedStatement = connection.prepareStatement(sql);
                    preparedStatement.setString(1, userName);
                    preparedStatement.setString(2, userPassword);
                    preparedStatement.setString(3, sex);
                    preparedStatement.setString(4, age);
                    preparedStatement.setString(5, telephone);
                    int count = preparedStatement.executeUpdate();
                    if (count == 1) {
                        request.setAttribute("msg", "注册成功!!!登录以查看更多内容");
                        request.getRequestDispatcher("login.jsp").forward(request, response);
                    }
                    else {
                        request.getRequestDispatcher("register.jsp").forward(request, response);
                    }
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                } finally {
                    try {
                        connection.close();
                        preparedStatement.close();
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }

                }
            }
            else {
                    request.setAttribute("msg", "注册失败,该用户已存在,请确认账号密码无误后重试");
                    request.getRequestDispatcher("register.jsp").forward(request, response);
            }
        }
    }
}

loginUserServlet

package org.example.controllor;

import org.example.api.IUserService;
import org.example.service.impl.UserServiceImpl;
import org.example.api.utils.JavaDateConnectionUtils;

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.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet("/loginUser")
public class loginUserServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 防止乱码
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        // 获取表单内容
        String userName = request.getParameter("userName");
        String userPassword = request.getParameter("userPassword");
        // 判断表单内容是否为空
        if (userName == null || userName.trim() == "" || userPassword == null || userPassword.trim() == "" ) {
            request.setAttribute("msg", "用户信息填写不完整!!!请重新进行登录");
            request.getRequestDispatcher("login.jsp").forward(request, response);
        } else {
            IUserService iUserService = new UserServiceImpl();
            boolean judgment = true;
            try {
                judgment = iUserService.insertUser(userName, userPassword);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            if (judgment == true) {
                JavaDateConnectionUtils javaDateConnection = new JavaDateConnectionUtils();
                PreparedStatement preparedStatement = null;
                Connection connection = null;
                try {
                    connection = javaDateConnection.getConn();
                    String sql = "select * from t_user where userName=? and userPassword=?";
                    preparedStatement = connection.prepareStatement(sql);
                    preparedStatement.setString(1, userName);
                    preparedStatement.setString(2, userPassword);
                    ResultSet rs = preparedStatement.executeQuery();
                    while (rs.next()){
                        System.out.println(rs.getString("userName")+rs.getString("userPassword")+rs.getString("sex")+rs.getString("age")+rs.getString("telephone"));
                    }

                        request.setAttribute("msg", "登录成功!!!");
                    HttpSession httpSession = request.getSession();
                    httpSession.setAttribute("userName",userName);
                        request.getRequestDispatcher("showgoods.jsp").forward(request, response);

                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                } finally {
                    try {
                        connection.close();
                        preparedStatement.close();
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }

                }

            }
            else {
                request.setAttribute("msg", "登录失败,请确认账号密码无误后重试");
                request.getRequestDispatcher("login.jsp").forward(request, response);

            }
        }
    }
}

RemoveAllGoodServlet

package org.example.controllor;
import org.example.api.IOrderService;
import org.example.api.utils.JavaDateConnectionUtils;
import org.example.service.impl.OrderServiceImpl;
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.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

@WebServlet("/RemoveAllGood")
public class RemoveAllGoodServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 防止乱码
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        String userName = request.getParameter("userName");
        IOrderService iOrderService = new OrderServiceImpl();
        boolean judgment = false;
        try {
            judgment = iOrderService.select();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        if (judgment == true) {
            JavaDateConnectionUtils javaDateConnection = new JavaDateConnectionUtils();
            PreparedStatement preparedStatement = null;
            Connection connection = null;
            try {
                connection = javaDateConnection.getConn();
                String sql = "delete from t_order where userName=?";
                preparedStatement = connection.prepareStatement(sql);
                preparedStatement.setString(1,userName);
                int count = preparedStatement.executeUpdate();
                if (count != 0) {
                    request.getRequestDispatcher("showcar.jsp").forward(request, response);
                }
                else {
                    request.getRequestDispatcher("showcar.jsp").forward(request, response);
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                try {
                    connection.close();
                    preparedStatement.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }

            }
        }
        else {
            request.getRequestDispatcher("showcar.jsp").forward(request, response);
        }
    }
}

RemoveGoodServlet

package org.example.controllor;

import org.example.api.IOrderService;
import org.example.api.utils.JavaDateConnectionUtils;
import org.example.service.impl.OrderServiceImpl;
import org.example.api.utils.MyTools;
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.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

@WebServlet("/RemoveGood")
public class RemoveGoodServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 防止乱码
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        // 获取参数内容
        String id = request.getParameter("id");
        String userName = request.getParameter("userName");
     //   MyTools myTools = new MyTools();
        IOrderService iOrderService = new OrderServiceImpl();
        boolean judgment = false;
        try {
            judgment = iOrderService.selectOrder(id,userName);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        if (judgment == true) {
            JavaDateConnectionUtils javaDateConnection = new JavaDateConnectionUtils();
            PreparedStatement preparedStatement = null;
            Connection connection = null;
            try {
                connection = javaDateConnection.getConn();
                String sql = "update t_order set orderNum=orderNum-1 where orderId=? and orderNum!=? and userName=?";
                preparedStatement = connection.prepareStatement(sql);
                preparedStatement.setString(1, id);
                preparedStatement.setString(2, "1");
                preparedStatement.setString(3,userName);
                int count = preparedStatement.executeUpdate();
                if (count == 1) {
                    request.getRequestDispatcher("showcar.jsp").forward(request, response);
                }
                else {
                    request.getRequestDispatcher("/RemoveOrder?id="+id+"&userName="+userName).forward(request,response);
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                try {
                    connection.close();
                    preparedStatement.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }

            }
        }
        else {
            request.getRequestDispatcher("showcar.jsp").forward(request,response);
        }
    }
}

RemoveOrderServlet

package org.example.controllor;

import org.example.api.IOrderService;
import org.example.api.utils.JavaDateConnectionUtils;
import org.example.service.impl.OrderServiceImpl;
import org.example.api.utils.MyTools;
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.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import org.example.api.utils.MyTools;

@WebServlet("/RemoveOrder")
public class RemoveOrderServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 防止乱码
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        // 获取参数内容
        String id = request.getParameter("id");
        String userName = request.getParameter("userName");

        IOrderService iOrderService = new OrderServiceImpl();
        boolean judgment = false;
        try {
            judgment = iOrderService.selectOrder(id,userName);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        if (judgment == true) {
            JavaDateConnectionUtils javaDateConnection = new JavaDateConnectionUtils();
            PreparedStatement preparedStatement = null;
            Connection connection = null;
            try {
                connection = javaDateConnection.getConn();
                String sql = " delete from t_order where orderId=? and userName=? ";
                preparedStatement = connection.prepareStatement(sql);
                preparedStatement.setString(1, id);
                preparedStatement.setString(2,userName);
                int count = preparedStatement.executeUpdate();
                if (count == 1) {
                    request.getRequestDispatcher("showcar.jsp").forward(request, response);
                }
                else {
                    request.getRequestDispatcher("showcar.jsp").forward(request, response);
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                try {
                    connection.close();
                    preparedStatement.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }

            }
        }
        else {
            request.getRequestDispatcher("showcar.jsp").forward(request, response);
        }
    }
}

SAddGoodServlet

package org.example.controllor;

import org.apache.catalina.session.StandardSession;
import org.example.api.IGoodService;
import org.example.api.IUserService;
import org.example.service.impl.GoodServiceImpl;
import org.example.api.entity.Good;
import org.example.api.entity.GoodList;
import org.example.api.utils.JavaDateConnectionUtils;
import org.example.service.impl.GoodServiceImpl;
import org.example.service.impl.UserServiceImpl;

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.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet("/SAddGood")
public class SAddGoodServlet extends HttpServlet {

    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 防止乱码
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        // 获取表单内容
       String userName = request.getParameter("userName");
        String goodName = request.getParameter("goodName");
        String goodValue = request.getParameter("goodValue");
        String goodId = request.getParameter("goodId");
        System.out.println(userName);
        // 判断表单内容是否为空
        if (goodId == null || goodName.trim() == "" || goodValue == null || goodValue.trim() == "" || goodId == null || goodId.trim() == "" ) {
            request.setAttribute("msg", "商品信息填写不完整!!!请重新进行填写");
            request.getRequestDispatcher("SAddGood.jsp").forward(request, response);
        } else {
            IGoodService iGoodService = new GoodServiceImpl();
            boolean judgment = true;
            try {
                judgment = iGoodService.select(goodId);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            if (judgment == false && userName.equals("侯凯")) {
                JavaDateConnectionUtils javaDateConnection = new JavaDateConnectionUtils();
                PreparedStatement preparedStatement = null;
                Connection connection = null;
                try {
                    connection = javaDateConnection.getConn();
                    String sql = "insert into t_good(goodId,goodName,goodValue) value(?,?,?)";
                    preparedStatement = connection.prepareStatement(sql);
                    preparedStatement.setString(1, goodId);
                    preparedStatement.setString(2, goodName);
                    preparedStatement.setString(3, goodValue);
                    int count = preparedStatement.executeUpdate();
                    if (count == 1) {
                        request.setAttribute("msg", "添加成功!!!");
                        request.getRequestDispatcher("SAddGood.jsp").forward(request, response);
                    }
                    else {
                            request.setAttribute("msg", "添加失败,该商品已存在或您无增加商品的权限");
                            request.getRequestDispatcher("SAddGood.jsp").forward(request, response);
                    }
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                } finally {
                    try {
                        connection.close();
                        preparedStatement.close();
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }

                }
            }
            else {
                request.setAttribute("msg", "添加失败,该商品已存在或您无增加商品的权限");
                request.getRequestDispatcher("SAddGood.jsp").forward(request, response);
            }
        }
    }
}

SelectGoodServlet

package org.example.controllor;

import org.apache.catalina.session.StandardSession;
import org.example.api.IGoodService;
import org.example.service.impl.GoodServiceImpl;
import org.example.api.entity.Good;
import org.example.api.entity.GoodList;
import org.example.api.utils.JavaDateConnectionUtils;
import org.example.service.impl.GoodServiceImpl;

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.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet("/SelectGood")
 public class SelectGoodServlet extends HttpServlet {

    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 防止乱码
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        IGoodService iGoodService = new GoodServiceImpl();
        GoodList goodList= new GoodList();
        boolean judgment = false;
        try {
            judgment = iGoodService.selectGood();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        if (judgment == true) {
            JavaDateConnectionUtils javaDateConnection = new JavaDateConnectionUtils();
            PreparedStatement preparedStatement = null;
            Connection connection = null;
            try {
                connection = javaDateConnection.getConn();
                String sql = "select * from t_good";
                preparedStatement = connection.prepareStatement(sql);
                ResultSet rs = preparedStatement.executeQuery();
                while (rs.next()){
                    Good p = new Good();
                    p.setGoodId(rs.getString("goodId"));
                    p.setGoodName(rs.getString("goodName"));
                    p.setGoodValue(rs.getString("goodValue"));
                    goodList.addGoods(p);
                }
                HttpSession httpSession = request.getSession();
                httpSession.setAttribute("Array",goodList.getGoodList());
                request.getRequestDispatcher("showgoods.jsp").forward(request, response);

            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                try {
                    connection.close();
                    preparedStatement.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }

        }
    }
}

SelectOrderServlet

package org.example.controllor;

import org.example.api.IGoodService;
import org.example.api.IOrderService;
import org.example.service.impl.GoodServiceImpl;
import org.example.api.entity.Good;
import org.example.api.entity.GoodList;
import org.example.api.utils.JavaDateConnectionUtils;
import org.example.service.impl.OrderServiceImpl;

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.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet("/SelectOrder")
public class SelectOrderServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 防止乱码
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        String userName = request.getParameter("userName");
        IOrderService iOrderService = new OrderServiceImpl();
        GoodList goodList= new GoodList();
        boolean judgment = false;
        try {
            judgment = iOrderService.select();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        if (judgment == true) {
            JavaDateConnectionUtils javaDateConnection = new JavaDateConnectionUtils();
            PreparedStatement preparedStatement = null;
            Connection connection = null;
            try {
                connection = javaDateConnection.getConn();
                String sql = "select * from t_order where userName=?";
                preparedStatement = connection.prepareStatement(sql);
                preparedStatement.setString(1, userName);
                ResultSet rs = preparedStatement.executeQuery();
                while (rs.next()){
                    System.out.println(rs.getString("orderName")+rs.getString("orderValue"));
                    Good p = new Good();
                    p.setGoodId(rs.getString("orderId"));
                    p.setGoodName(rs.getString("orderName"));
                    p.setGoodValue(rs.getString("orderValue"));
                    p.setGoodNum(rs.getString("orderNum"));
                    goodList.addGoods(p);
                }
                HttpSession httpSession = request.getSession();
                request.setAttribute("Array1",goodList.getGoodList());
                request.getRequestDispatcher("showcar.jsp").forward(request, response);

            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                try {
                    connection.close();
                    preparedStatement.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }

        }
        else {
            HttpSession httpSession = request.getSession();
            httpSession.setAttribute("Array1",goodList.getGoodList());
            request.getRequestDispatcher("showcar.jsp").forward(request,response);
        }
    }
}

SRemoveGoodServlet

package org.example.controllor;

import org.apache.catalina.session.StandardSession;
import org.example.api.IGoodService;
import org.example.service.impl.GoodServiceImpl;
import org.example.api.entity.Good;
import org.example.api.entity.GoodList;
import org.example.api.utils.JavaDateConnectionUtils;
import org.example.service.impl.GoodServiceImpl;

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.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet("/SRemoveGood")
public class SRemoveGoodServlet extends HttpServlet {

    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 防止乱码
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        String id = request.getParameter("id");
        String userName = request.getParameter("userName");
        IGoodService iGoodService = new GoodServiceImpl();
        GoodList goodList = new GoodList();
        boolean judgment = false;
        try {
            judgment = iGoodService.select(id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        if (judgment == true && userName.equals("侯凯")) {

            JavaDateConnectionUtils javaDateConnection = new JavaDateConnectionUtils();
            PreparedStatement preparedStatement = null;
            Connection connection = null;
            try {
                connection = javaDateConnection.getConn();
                String sql = "delete from t_good where goodId=?";
                preparedStatement = connection.prepareStatement(sql);
                preparedStatement.setString(1, id);
                int rs = preparedStatement.executeUpdate();

                if (rs == 1) {
                    HttpSession httpSession = request.getSession();
                    request.setAttribute("ji","商品删除成功!!!");
                    request.getRequestDispatcher("showgoods.jsp").forward(request, response);

                }
                else {
                    request.getRequestDispatcher("showgoods.jsp").forward(request,response);
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                try {
                    connection.close();
                    preparedStatement.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }

            }
        }
        else {
            HttpSession httpSession = request.getSession();
            request.setAttribute("ji","商品删除失败!!!您无删除商品的权限,");
            request.getRequestDispatcher("showgoods.jsp").forward(request,response);
        }
    }
}
#  web层

```java

```java

```java
```clike
<%@ page language="java" contentType="text/html;charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD
HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>账号注册</title>
</head>
<style>
    #a {
        width:50%;
        height:50%;
        border: 1px dashed ;
        background-color: #90eee3;
        text-align:center;
    }

    body{
        background-color: #e0f9ff;
    }
</style>
<body>

<div id="a">
    <h1>注销账号</h1>

    <form action="/DeleteUser"  method="post">
        用&nbsp;户&nbsp;名:
        <input type="text" name="userName">
        <br>

        密&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;码:
        <input type="password" name="userPassword">
        <br>

        手&nbsp;机&nbsp;号:
        <input type="text" name="telephone">
        <br>

        <input type="submit" value="注销">

    </form>
</div>
<h3 align="center" ><%=request.getAttribute("msg")%></h3>
<h2 align="center"><a href="register.jsp">点击重新注册账号</a></h2>

</body>
</html>


<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html" charset="UTF-8">
    <title>黑市商城首页</title>
</head>
<%response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");%>
<body bgcolor="#add8e6">

<table width="100%">

    <tr>
        <td>
            <table width="100%">
                <tr>

                    <td>
                        <a href="register.jsp">注册</a>
                        <a href="login.jsp">登录</a>
                        <a href="delete.jsp">注销</a>
                        <a href="showcar.jsp">订单</a>
                        <a href="#">帮助</a>
                    </td>
                </tr>
            </table>
        </td>
    </tr>
    <!--第二部分-->
    <tr bgcolor="black">
        <td height="50px">
            <a href="#"><font color="white">首页</font></a>
            <a href="#"><font color="white">碳酸饮料</font></a>
            <a href="#"><font color="white">乳饮</font></a>
            <a href="#"><font color="white">果茶</font></a>
            <a href="#"><font color="white">其他饮料</font></a>
        </td>
    </tr>


    <tr>
        <td>
            <img src="https://n.sinaimg.cn/spider2020420/133/w600h333/20200420/4c7d-iskepxt7981236.png" height="300px" width="100%" />
        </td>
    </tr>

    <tr>
        <td>

            <table>
                <tr>
                    <td>
                        <img src="https://th.bing.com/th/id/Rdb7c7cba11e8e880930a480ff0db0c87?rik=cTjPOJPJyoleSA&riu=http%3a%2f%2fpic109.nipic.com%2ffile%2f20160916%2f6062299_163706413000_2.jpg&ehk=1nPt6VryGF2MlNMFi8ChxJ1PhlVp1k1%2fKnz2bLkL6VQ%3d&risl=&pid=ImgRaw" height="200px" width="200px"/>
                        <p><a>可口可乐</a></p>
                        <p><font color="red">$3</font></p>
                    </td>
                    <td>
                        <img src="https://th.bing.com/th/id/Rdb7c7cba11e8e880930a480ff0db0c87?rik=cTjPOJPJyoleSA&riu=http%3a%2f%2fpic109.nipic.com%2ffile%2f20160916%2f6062299_163706413000_2.jpg&ehk=1nPt6VryGF2MlNMFi8ChxJ1PhlVp1k1%2fKnz2bLkL6VQ%3d&risl=&pid=ImgRaw" height="200px" width="200px"/>
                        <p><a>可口可乐</a></p>
                        <p><font color="red">$3</font></p>
                    </td>
                    <td>
                        <img src="https://th.bing.com/th/id/Rdb7c7cba11e8e880930a480ff0db0c87?rik=cTjPOJPJyoleSA&riu=http%3a%2f%2fpic109.nipic.com%2ffile%2f20160916%2f6062299_163706413000_2.jpg&ehk=1nPt6VryGF2MlNMFi8ChxJ1PhlVp1k1%2fKnz2bLkL6VQ%3d&risl=&pid=ImgRaw" height="200px" width="200px"/>
                        <p><a>可口可乐</a></p>
                        <p><font color="red">$3</font></p>
                    </td>
                    <td>
                        <img src="https://th.bing.com/th/id/Rdb7c7cba11e8e880930a480ff0db0c87?rik=cTjPOJPJyoleSA&riu=http%3a%2f%2fpic109.nipic.com%2ffile%2f20160916%2f6062299_163706413000_2.jpg&ehk=1nPt6VryGF2MlNMFi8ChxJ1PhlVp1k1%2fKnz2bLkL6VQ%3d&risl=&pid=ImgRaw" height="200px" width="200px"/>
                        <p><a>可口可乐</a></p>
                        <p><font color="red">$3</font></p>
                    </td>
                    <td>
                        <img src="https://th.bing.com/th/id/Rdb7c7cba11e8e880930a480ff0db0c87?rik=cTjPOJPJyoleSA&riu=http%3a%2f%2fpic109.nipic.com%2ffile%2f20160916%2f6062299_163706413000_2.jpg&ehk=1nPt6VryGF2MlNMFi8ChxJ1PhlVp1k1%2fKnz2bLkL6VQ%3d&risl=&pid=ImgRaw" height="200px" width="200px"/>
                        <p><a>可口可乐</a></p>
                        <p><font color="red">$3</font></p>
                    </td>
                    <td>
                        <img src="https://th.bing.com/th/id/Rdb7c7cba11e8e880930a480ff0db0c87?rik=cTjPOJPJyoleSA&riu=http%3a%2f%2fpic109.nipic.com%2ffile%2f20160916%2f6062299_163706413000_2.jpg&ehk=1nPt6VryGF2MlNMFi8ChxJ1PhlVp1k1%2fKnz2bLkL6VQ%3d&risl=&pid=ImgRaw" height="200px" width="200px"/>
                        <p><a>可口可乐</a></p>
                        <p><font color="red">$3</font></p>
                    </td>

                </tr>
                <tr>
                    <td>
                        <img src="https://cbu01.alicdn.com/img/ibank/2019/935/265/11703562539_780901980.jpg" height="200px" width="200px"/>
                        <p><a>旺仔牛奶(一排)</a></p>
                        <p><font color="red">$20</font></p>
                    </td>
                    <td>
                        <img src="https://tse3-mm.cn.bing.net/th/id/OIP.kz-2OVuNiFTQrYHAD7jP2AAAAA?pid=ImgDet&rs=1" height="200px" width="200px"/>
                        <p><a>脉动</a></p>
                        <p><font color="red">$5</font></p>
                    </td>
                    <td>
                    <img src="https://th.bing.com/th/id/R48187afbfb548050648990f0dd7a3fe0?rik=cXl%2bo9kjuPe5YA&riu=http%3a%2f%2fwww.shikefood.com%2fimages%2f201706%2fsource_img%2f10502_P_1497391133066.jpg&ehk=4xKqaMC%2fueYJILKz3HynqZJ25tDpC2%2bZf5YKEAkPfTA%3d&risl=&pid=ImgRaw" height="200px" width="200px"/>
                    <p><a>乌龙茶</a></p>
                    <p><font color="red">$1</font></p>
                    </td>
                    <td>
                    <img src="https://th.bing.com/th/id/Rdb7c7cba11e8e880930a480ff0db0c87?rik=cTjPOJPJyoleSA&riu=http%3a%2f%2fpic109.nipic.com%2ffile%2f20160916%2f6062299_163706413000_2.jpg&ehk=1nPt6VryGF2MlNMFi8ChxJ1PhlVp1k1%2fKnz2bLkL6VQ%3d&risl=&pid=ImgRaw" height="200px" width="200px"/>
                    <p><a>可口可乐</a></p>
                    <p><font color="red">$3</font></p>
                </td>
                    <td>
                        <img src="https://th.bing.com/th/id/Rdb7c7cba11e8e880930a480ff0db0c87?rik=cTjPOJPJyoleSA&riu=http%3a%2f%2fpic109.nipic.com%2ffile%2f20160916%2f6062299_163706413000_2.jpg&ehk=1nPt6VryGF2MlNMFi8ChxJ1PhlVp1k1%2fKnz2bLkL6VQ%3d&risl=&pid=ImgRaw" height="200px" width="200px"/>
                        <p><a>可口可乐</a></p>
                        <p><font color="red">$3</font></p>
                    </td>
                    <td>
                        <img src="https://th.bing.com/th/id/Rdb7c7cba11e8e880930a480ff0db0c87?rik=cTjPOJPJyoleSA&riu=http%3a%2f%2fpic109.nipic.com%2ffile%2f20160916%2f6062299_163706413000_2.jpg&ehk=1nPt6VryGF2MlNMFi8ChxJ1PhlVp1k1%2fKnz2bLkL6VQ%3d&risl=&pid=ImgRaw" height="200px" width="200px"/>
                        <p><a>可口可乐</a></p>
                        <p><font color="red">$3</font></p>
                    </td>
                </tr>
                <tr>
                    <td>
                        <img src="https://th.bing.com/th/id/Rbf660f71a587e9c365139518e7b084b1?rik=O1SdZo5%2f8tmEpQ&riu=http%3a%2f%2fpic299.nipic.com%2ffile%2f20200611%2f19051700_194646446000_2.jpg&ehk=GDt8l%2bRaESjBBHz%2bfP5TVTqjtCGNHypsIubHbRgbsSs%3d&risl=&pid=ImgRaw" height="200px" width="200px"/>
                        <p><a>果茶</a></p>
                        <p><font color="red">$15</font></p>
                    </td>
                    <td>
                        <img src="https://tse1-mm.cn.bing.net/th/id/OIP.WzXja99RFLlHs4LreoO3GQHaGq?pid=ImgDet&rs=1" height="200px" width="200px"/>
                        <p><a>七喜</a></p>
                        <p><font color="red">$7</font></p>
                    </td>
                    <td>
                        <img src="https://imgservice4.suning.cn/uimg1/b2c/image/gs0RyM76yxGFGVefFTHpAg==.jpg_400w_400h_4e" height="200px" width="200px"/>
                        <p><a>水晶葡萄</a></p>
                        <p><font color="red">$4.5</font></p>
                    </td>
                    <td>
                        <img src="https://tse3-mm.cn.bing.net/th/id/OIP.U6N6vZvwHFWmqFq8Pk8tPQHaKE?pid=ImgDet&rs=1" height="200px" width="200px"/>
                        <p><a>美年达</a></p>
                        <p><font color="red">$3</font></p>
                    </td>
                    <td>
                        <img src="https://th.bing.com/th/id/Rc68719c95e68240324bad6459d625675?rik=1SpDWN3ULeZtsQ&riu=http%3a%2f%2fimages.zon100.com%2fimg%2f10000518%2f1100141218%2f1100206232_1000.jpg&ehk=DcWYen6bIosj%2f4LJlX0g1xbitDegc7ga78tDMlubvo4%3d&risl=&pid=ImgRaw" height="200px" width="200px"/>
                        <p><a>绿茶</a></p>
                        <p><font color="red">$5</font></p>
                    </td>
                    <td>
                        <img src="https://th.bing.com/th/id/Rbc672bbf2693119837a8d3f5df6e2fbd?rik=KpfIwTaSBYPgdw&riu=http%3a%2f%2fwx1.sinaimg.cn%2flarge%2f006APoFYly1ga7ynozy6eg308c069dg7.gif&ehk=raDwaMUtQHU1LIZCkiu2tn91MXGEsd1k5uqrK7hXh7I%3d&risl=&pid=ImgRaw" height="200px" width="220px"/>
                        <p><a>营养快线</a></p>
                        <p><font color="red">$998</font></p>
                    </td>
                </tr>
            </table>
        </td>
    </tr>

    <tr>
        <td>
            <img src="picture/banner.jpg" height="5%" width="100%"/>
        </td>
    </tr>

    <tr>
        <td align="center">
            <a href="#">服务保障</a>
            <a href="#">联系我们</a>
            <a href="#">关于我们</a>
            <a href="#">投诉建议</a>
            <br />
            <p>HK黑市商城&copy;2021-4.18火爆开启</p>
        </td>
    </tr>
</table>
</body>
</html>
<%@ page language="java" contentType="text/html;
charset=utf-8"
         pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD
HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type"
          content="text/html; charset=utf-8">
    <title>用户登录</title>
    <style>
        #a {
            width:50%;
            height:200px;
            border: 1px dashed ;
            background-color: #e0fff4;
            text-align:center;
        }
        body{
            background-color:lightblue;
        }
    </style>
</head>
<body>
<div id="a">
    <h1>登录界面</h1>
    <form action="/loginUser" method="post">
        账号:<input type="text" name="userName"/>
        <br>
        密码:<input type="password" name="userPassword"/>
        <br>

        <input type="submit" value="登录"/>
        没有账号?<a href ="register.jsp">注册账号</a><br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;多余账号?<a href="delete.jsp">注销账号</a>
    </form>
</div>
<h3 align="center" ><%=request.getAttribute("msg")%></h3>
<h2 align="center"><a href="showgoods.jsp">点击进入商品界面</a></h2>
</body>
</html>
<%@ page language="java" contentType="text/html;charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD
HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>账号注册</title>
</head>
<style>
    #a {
        width:50%;
        height:50%;
        border: 1px dashed ;
        background-color: #90eee6;
        text-align:center;
    }

    body{
        background-color: #e0f9ff;
    }
</style>
<body>

<div id="a">
    <h1>注册账号</h1>

    <form action="/InsertUser"  method="post">
        用&nbsp;户&nbsp;名:
        <input type="text" name="userName">
        <br>

        密&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;码:
        <input type="password" name="userPassword">
        <br>

        性&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;别:
        <input type="text" name="sex">
        <br>


        年&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;龄:
        <input type="text" name="age">
        <br>

        手&nbsp;机&nbsp;号:
        <input type="text" name="telephone">
        <br>

        <input type="submit" value="注册">
        已有账号?<a href ="login.jsp">点击登录</a><br>
    </form>
</div>
<h3 align="center" ><%=request.getAttribute("msg")%></h3>
</body>
</html>


<%@ page language="java" contentType="text/html;
charset=utf-8"
         pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD
HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type"
          content="text/html; charset=utf-8">
    <title>添加商品</title>
    <style>
        #a {

            width:50%;
            height:200px;
            border: 1px dashed ;
            background-color: #e0fff4;
            text-align:center;
        }
        <%
        String userName = request.getParameter("userName");
        response.setContentType("text/html;charset=UTF-8");
    request.setCharacterEncoding("UTF-8");
        %>
        body{
            background-color:lightblue;
        }
    </style>
</head>
<body>
<div id="a">
    <h1>添加商品界面</h1>
    <form action="/SAddGood?userName=<%=userName%>" method="post">
        商品名称:<input type="text" name="goodName"/>
        <br>
        商品价格:<input type="text" name="goodValue"/>
        <br>
        &nbsp;&nbsp;商品ID:<input type="text" name="goodId"/>
        <br>

        <input type="submit" value="添加"/>
    </form>
</div>
<h3 align="center" ><%=request.getAttribute("msg")%></h3>
<h2 align="center"><a href="showgoods.jsp">点击进入商品界面</a></h2>
</body>
</html>

<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="org.example.api.entity.Good" %>
<%@ page import="org.example.api.entity.GoodList" %>
<%@ page import="org.example.api.utils.MyTools" %>
<%@ page import="org.example.api.entity.OrderList" %>
<%
    float total = 0;
%>


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html" charset="UTF-8">
    <link href="styles.css" type="text/css" rel="stylesheet" />
    <title>黑市订单</title>
</head>
<body>

<%
    response.setContentType("text/html;charset=UTF-8");
    request.setCharacterEncoding("UTF-8");
    HttpSession httpSession = request.getSession();
    String userName = (String) httpSession.getAttribute("userName");
%>

<h3 align="left"><a href="/SelectOrder?userName=<%=userName%>">点击刷新订单</a></h3>
<h3 align="right"><a href="index.jsp">返回主页面</a></h3>
<%
    ArrayList <Good> buyList = null;
    buyList = (ArrayList<Good>) request.getAttribute("Array1");
%>
<table>
    <tr height="50">
        <td colspan="3" align="center" ><h1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;订单</h1></td>
    </tr>
    <tr align="center" height="30" bgcolor="lightgrey">
        <td>名称</td>
        <td>价格</td>
        <td>数量</td>
        <td>移除</td>
    </tr>
    <% if(buyList == null || buyList.size() == 0){ %>
    <tr height="100">
        <td colspan="3" align="center">您的订单还未添加任何商品!<br>
        请尝试刷新您的订单!</td>
    </tr>
    <%
    }else{
        for(int i = 0; i < buyList.size(); i++){
            Good single = buyList.get(i);
            float money = MyTools.strToInt(single.getGoodValue())*MyTools.strToInt(single.getGoodNum());
            total += money;
    %>
    <tr align="center" height="50">
        <td><%= single.getGoodName() %></td>
        <td><%= single.getGoodValue() %></td>
        <td>
            <a href="/AddGood?id=<%=single.getGoodId()%>&userName=<%=userName%>">
                <img src="https://img.51miz.com/Element/00/79/78/04/372c7712_E797804_81ee5239.png" width="20" height="20"/>
            </a>
            <strong><%=single.getGoodNum()%></strong>
            <a href="/RemoveGood?id=<%=single.getGoodId()%>&userName=<%=userName%>">
                <img src="https://th.bing.com/th/id/Rcde7b52d6b859e47df4f30a4980b9463?rik=AcAi8g%2bYJfQoDA&riu=http%3a%2f%2fbpic.588ku.com%2felement_pic%2f01%2f35%2f82%2f18573bf7012f19d.jpg&ehk=VxpGi3%2fpJexj4F4DOLo6a5gL1h6XaX%2b%2b51f6TPL7iGI%3d&risl=&pid=ImgRaw" width="20" height="20"/>
            </a>
        </td>
        <td><a href="/RemoveOrder?id=<%=single.getGoodId()%>&userName=<%=userName%>">移除</a></td>
    </tr>
    <%
            }
        }
    %>
    <tr height="50">
        <td colspan="3" align="left" ><h4>应付金额:<%= total %></h4></td>
        <br>

    </tr>
    <tr align="center" height="50">
        <td align="left" colspan="3"><a href="showgoods.jsp">继续购物</a></td>
        <td align="right" colspan="3"><a href="/RemoveAllGood?userName=<%=userName%>">清除订单</a></td>
    </tr>



</table>
</body>
</html>

<%@page import="java.util.ArrayList"%>
<%@page import="org.example.controllor.SelectGoodServlet" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="org.example.api.entity.Good" %>
<%@ page import="org.example.api.entity.GoodList" %>
<%@ page import="java.security.Principal" %>
<jsp:useBean id="goodList" class="org.example.api.entity.GoodList" scope="session"/>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>黑市</title>
    <link href="styles.css" type="text/css" rel="stylesheet" />
</head>
<body>
<%
    response.setContentType("text/html;charset=UTF-8");
    request.setCharacterEncoding("UTF-8");
%>

<%
    HttpSession httpSession = request.getSession();
    ArrayList <Good> goodsList = new ArrayList<Good>();
    String userName = (String) httpSession.getAttribute("userName");
    goodsList = (ArrayList<Good>) httpSession.getAttribute("Array");
    String  ji = request.getAttribute("ji") + "请重新刷新商品页面以查看全部商品";
%>

<a href="/SelectGood">点击刷新商品</a>


<script>alert("<%=ji%>")</script>



<table>
    <tr height="50">
        <td colspan="3" align="center" ><h1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;黑市商城</h1></td>
    </tr>
    <tr align="center" height="30" bgcolor="lightgrey">
        <td></td>
        <td>名称</td>
        <td>价格</td>
        <td>购买</td>
    </tr>
    <% if(goodsList == null || goodsList.size() == 0){ %>
    <tr height="100">
        <td colspan="3" align="center">没有商品可以显示!<br>
        请尝试刷新商品!</td>
    </tr>
    <%
    }else{
        for(int i = 0; i < goodsList.size(); i++){
            Good single =goodsList.get(i);
    %>
    <tr align="center" height="50">
        <td>
            <a href="/SRemoveGood?userName=<%=userName%>&id=<%=single.getGoodId()%>">
            <img src="https://img.51miz.com/Element/00/77/34/97/c78a8ea1_E773497_8e853fd8.png" width="30" height="30"/>
        </a>
        </td>
        <td><%= single.getGoodName() %></td>
        <td><%= single.getGoodValue() %></td>
        <td>
        <a href="/InsertGood?id=<%=single.getGoodId()%>&name=<%=single.getGoodName()%>&value=<%=single.getGoodValue()%>&userName=<%=userName%>">购买</a>
        </td>
    </tr>
    <%
            }
        }
    %>
    <tr align="center" height="50">
        <td align="left" colspan="3">
            <a href="/SAddGood.jsp?userName=<%=userName%>">
                添加商品
            </a>
        </td>
        <td align="right" colspan="3"><a href="showcar.jsp">查看订单</a></td>
    </tr>

</table>

</body>
</html>


@charset "UTF-8";
table{
	border: 1px;
	width: 500px;
	rules: none;
	cellspacing:0px;
	cellpadding:0px;
}
tr{
	height:	50px;
}
td{
	width:100px;
}
a:link {
 text-decoration: none;
}
a:visited {
 text-decoration: none;
}
a:hover {
 text-decoration: none;
}
a:active {
 text-decoration: none;
}

dao层

selectGood

```clike
package org.example.dao;

import java.sql.SQLException;

public interface IGoodDao {

  /**
   * 查询全部商品
   * @return
   * @throws SQLException
   */
  boolean selectGood() throws SQLException;

  /**
   * 根据商品id查询特定商品
   * @param id
   * @return
   * @throws SQLException
   */
  boolean select(String id) throws  SQLException;

}


package org.example.dao;

import java.sql.SQLException;

public interface IOrderDao {
    /**
     * 根据订单id及所传的userName查询订单
     * @param orderId
     * @param userName
     * @return
     * @throws SQLException
     */
       boolean selectOrder(String orderId , String userName) throws SQLException;

    /**
     * 查询全部订单
     * @return
     * @throws SQLException
     */
    boolean select() throws SQLException;
}


package org.example.dao;


import java.sql.SQLException;


public interface IUserDao {


    /**
     * 根据用户账号查询用户
     * @param userName
     * @return
     * @throws SQLException
     */

    boolean loginUser(String userName) throws SQLException;

    /**
     * 根据用户名及密码查询特定用户
     * @param userName
     * @param userPassword
     * @return
     * @throws SQLException
     */
    boolean loginUser1(String userName,String userPassword)throws SQLException;

}

selectGood

package org.example.dao.impl;


import org.example.dao.IUserDao;
import org.example.api.utils.JavaDateConnectionUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class selectGood implements IUserDao {
    int a=0;

    JavaDateConnectionUtils javaDateConnectionUtils = new JavaDateConnectionUtils();

    Connection connection = javaDateConnectionUtils.getConn();


    @Override
    public boolean loginUser(String userName) throws SQLException {

        String iSql = "select * from t_user where userName=?";
        PreparedStatement preparedStatement = null;
        preparedStatement = connection.prepareStatement(iSql);
preparedStatement.setString(1,userName);
         ResultSet re=   preparedStatement.executeQuery();

        while (re.next()){
            a++;
            System.out.println(re.getString("userName")+""+re.getString("userPassword"));
        }if (a!=0){
        System.out.println("查询成功");
        preparedStatement.close();
        connection.close();
        return true;}
        else {preparedStatement.close();
            connection.close();
            System.out.println("查询失败");
            return false;
        }
    }
    @Override
    public boolean loginUser1(String userName, String userPassword) throws SQLException {

        String iSql = "select * from t_user where userName=? and userPassword=?";
        PreparedStatement preparedStatement = null;
        preparedStatement = connection.prepareStatement(iSql);
        preparedStatement.setString(1,userName);
        preparedStatement.setString(2,userPassword);
        ResultSet re=   preparedStatement.executeQuery();

        while (re.next()){
            a++;
            System.out.println(re.getString("userName")+""+re.getString("userPassword"));
        }if (a!=0){
            System.out.println("查询成功");
            preparedStatement.close();
            connection.close();
            return true;}
        else {preparedStatement.close();
            connection.close();
            System.out.println("查询失败");
            return false;
        }
    }
}

impl.OrderDaoImpl

package org.example.dao.impl;

import org.example.dao.IOrderDao;
import org.example.api.utils.JavaDateConnectionUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class OrderDaoImpl implements IOrderDao {
    int a = 0;

    JavaDateConnectionUtils javaDateConnectionUtils = new JavaDateConnectionUtils();

    Connection connection = javaDateConnectionUtils.getConn();


    @Override
    public boolean selectOrder(String orderId , String userName) throws SQLException {

        String iSql = "select * from t_order where orderId=? and userName=?";
        PreparedStatement preparedStatement = null;
        preparedStatement = connection.prepareStatement(iSql);
        preparedStatement.setString(1, orderId);
        preparedStatement.setString(2,userName);
        ResultSet re = preparedStatement.executeQuery();

        while (re.next()) {
            a++;
            System.out.println(re.getString("orderId")+re.getString("userName"));
        }
        if (a != 0) {
            System.out.println("查询成功");
            preparedStatement.close();
            connection.close();
            return true;
        } else {
            preparedStatement.close();
            connection.close();
            System.out.println("查询失败");
            return false;
        }
    }

    public boolean select () throws SQLException {
        String iSql = "select * from t_order ";
        PreparedStatement preparedStatement = null;
        preparedStatement = connection.prepareStatement(iSql);
        ResultSet re = preparedStatement.executeQuery();

        while (re.next()) {
            a++;
            System.out.println(re.getString("orderId"));
        }
        if (a != 0) {
            System.out.println("查询成功");
            preparedStatement.close();
            connection.close();
            return true;
        } else {
            preparedStatement.close();
            connection.close();
            System.out.println("查询失败");
            return false;
        }

    }
}

impl.GoodDaoImpl

package org.example.dao.impl;

import org.example.api.utils.JavaDateConnectionUtils;
import org.example.dao.IGoodDao;
import org.example.api.utils.JavaDateConnectionUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class GoodDaoImpl implements IGoodDao {
    int a=0;

    JavaDateConnectionUtils javaDateConnectionUtils = new JavaDateConnectionUtils();

    Connection connection = javaDateConnectionUtils.getConn();

    @Override
    public boolean selectGood() throws SQLException {

        String iSql = "select * from t_good ";
        PreparedStatement preparedStatement = null;
        preparedStatement = connection.prepareStatement(iSql);
        ResultSet re=   preparedStatement.executeQuery();

        while (re.next()){
            a++;
            System.out.println(re.getString("goodId")+""+re.getString("goodName"));
        }if (a!=0){
            System.out.println("查询成功");
            preparedStatement.close();
            connection.close();
            return true;}
        else {preparedStatement.close();
            connection.close();
            System.out.println("查询失败");
            return false;
        }
    }
    public boolean select(String id) throws SQLException {

        String iSql = "select * from t_good where goodId=?";
        PreparedStatement preparedStatement = null;
        preparedStatement = connection.prepareStatement(iSql);
        preparedStatement.setString(1,id);
        ResultSet re=   preparedStatement.executeQuery();

        while (re.next()){
            a++;
            System.out.println(re.getString("goodId")+""+re.getString("goodName"));
        }if (a!=0){
            System.out.println("查询成功");
            preparedStatement.close();
            connection.close();
            return true;}
        else {preparedStatement.close();
            connection.close();
            System.out.println("查询失败");
            return false;
        }
    }
}

service层

IUserService

```clike
package org.example.api;

import java.sql.SQLException;

public interface IUserService {
    /**
     * 用户增加接口
     * @param
     * @return boolean
     */
    boolean insertUser(String userName,String userPassword) throws SQLException;


    /**
     * 用户登录接口
     * @param userName
     * @return
     */
    boolean loginUser(String userName) throws SQLException;
}

package org.example.api;

import java.sql.SQLException;

public interface IOrderService {
     /**
      *根据订单id 及 登录的userName来查询订单
      * @param orderId
      * @param userName
      * @return
      * @throws SQLException
      */
     boolean selectOrder(String orderId , String userName) throws SQLException;

     /**
      * 查询全部订单
      * @return
      * @throws SQLException
      */
     boolean select() throws SQLException;

}

IGoodService

package org.example.api;

import java.sql.SQLException;

public interface IGoodService {


    /**
     * 查询全部商品
      * @return
     * @throws SQLException
     */
    boolean selectGood () throws SQLException;

    /**
     * 根据商品id查询特定商品
     * @param id
     * @return
     * @throws SQLException
     */
    boolean select(String id) throws SQLException;
}

UserServiceImpl

package org.example.service.impl;


import org.example.api.IUserService;
import org.example.dao.IUserDao;
import org.example.dao.impl.UserDaoImpl;

import java.sql.SQLException;
import java.sql.SQLRecoverableException;

public class UserServiceImpl implements IUserService {
    IUserDao iUserDao = new UserDaoImpl();

    public boolean insertUser(String userName,String userPasswprd) throws SQLException {
        return iUserDao.loginUser1(userName,userPasswprd);
    }

    @Override
    public boolean loginUser(String userName) throws SQLException {
        return iUserDao.loginUser(userName);
    }
}

OrderServiceImpl

package org.example.service.impl;

import org.example.api.IOrderService;
import org.example.dao.IOrderDao;
import org.example.dao.impl.OrderDaoImpl;

import java.sql.SQLException;

public class OrderServiceImpl implements IOrderService {
    IOrderDao iOrderDao = new OrderDaoImpl();
    public boolean selectOrder(String orderId , String userName) throws SQLException {
        return iOrderDao.selectOrder(orderId,userName) ;
    }
    public boolean select() throws SQLException {
        return iOrderDao.select();
    }


}

GoodServiceImpl

package org.example.service.impl;

import org.example.dao.impl.GoodDaoImpl;
import org.example.api.IGoodService;
import org.example.api.IGoodService;
import org.example.dao.IGoodDao;

import java.sql.SQLException;

public class GoodServiceImpl implements IGoodService {

    IGoodDao iGoodDao = new GoodDaoImpl();

    @Override
    public boolean selectGood() throws SQLException {
        return iGoodDao.selectGood();
    }

    @Override
    public boolean select(String id) throws SQLException {
        return iGoodDao.select(id);
    }
}

utils层

JavaDateConnectionUtils


```java
package org.example.api.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class JavaDateConnectionUtils {
    /**
     * 获取数据库连接
     *
     * @return Connection
     */
    public Connection getConn() {
        String url = "jdbc:mysql://localhost:3306/project?serverTimezone=UTC";
        String username = "root";
        String password = "Hyk59308";
        Connection conn = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            //classLoader,加载对应驱动
            conn = DriverManager.getConnection(url, username, password);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }
}

MyTools

package org.example.api.utils;

import java.io.UnsupportedEncodingException;

public class MyTools {
    public static String toChinese(String str) {
        if(str == null) {
            str="";
        }
        try {
            str = new String(str.getBytes("ISO-8859-1"),"gb2312");
        }catch(UnsupportedEncodingException e) {
            str="";
            e.printStackTrace();
        }
        return str;
    }
    public static int strToInt(String str) {
        if(str == null || str.equals("")) {
            str = "0";
        }
        int i = 0;
        try {
            i = Integer.parseInt(str);
        }catch(NumberFormatException e) {
            i = 0;
            e.printStackTrace();
        }
        return i;
    }

}

日志配置

<?xml version="1.0" encoding="UTF-8"?>
<!--日志级别以及优先级排序: OFF > FATAL > ERROR > WARN > INFO > DEBUG > TRACE > ALL-->
<!--Configuration后面的status,这个用于设置log4j2自身内部的信息输出,可以不设置,当设置成trace时,你会看到log4j2内部各种详细输出-->
<!--monitorInterval:Log4j能够自动检测修改配置 文件和重新配置本身,设置间隔秒数-->
<configuration status="INFO" monitorInterval="30">
    <!--先定义所有的appender-->
    <appenders>
        <!--这个输出控制台的配置-->
        <console name="Console" target="SYSTEM_OUT">
            <!--输出日志的格式-->
            <PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
        </console>
        <!--文件会打印出所有信息,这个log每次运行程序会自动清空,由append属性决定,这个也挺有用的,适合临时测试用-->
        <File name="log" fileName="${sys:user.home}/logs/test.log" append="false">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
        </File>
        <!-- 这个会打印出所有的info及以下级别的信息,每次大小超过size,则这size大小的日志会自动存入按年份-月份建立的文件夹下面并进行压缩,作为存档-->
        <RollingFile name="RollingFileInfo" fileName="${sys:user.home}/logs/info.log"
                     filePattern="${sys:user.home}/logs/$${date:yyyy-MM}/info-%d{yyyy-MM-dd}-%i.log">
            <!--控制台只输出level及以上级别的信息(onMatch),其他的直接拒绝(onMismatch)-->
            <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
            <PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy/>
                <SizeBasedTriggeringPolicy size="100 MB"/>
            </Policies>
        </RollingFile>
        <RollingFile name="RollingFileWarn" fileName="${sys:user.home}/logs/warn.log"
                     filePattern="${sys:user.home}/logs/$${date:yyyy-MM}/warn-%d{yyyy-MM-dd}-%i.log">
            <ThresholdFilter level="warn" onMatch="ACCEPT" onMismatch="DENY"/>
            <PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy/>
                <SizeBasedTriggeringPolicy size="100 MB"/>
            </Policies>
            <!-- DefaultRolloverStrategy属性如不设置,则默认为最多同一文件夹下7个文件,这里设置了20 -->
            <DefaultRolloverStrategy max="20"/>
        </RollingFile>
        <RollingFile name="RollingFileError" fileName="${sys:user.home}/logs/error.log"
                     filePattern="${sys:user.home}/logs/$${date:yyyy-MM}/error-%d{yyyy-MM-dd}-%i.log">
            <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
            <PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy/>
                <SizeBasedTriggeringPolicy size="100 MB"/>
            </Policies>
        </RollingFile>
    </appenders>
    <!--然后定义logger,只有定义了logger并引入的appender,appender才会生效-->
    <loggers>
        <!--        &lt;!&ndash;过滤掉spring和mybatis的一些无用的DEBUG信息&ndash;&gt;-->
        <!--        <logger name="org.springframework" level="INFO"></logger>-->
        <!--        <logger name="org.mybatis" level="INFO"></logger>-->
        <root level="INFO">
            <appender-ref ref="Console"/>
            <appender-ref ref="RollingFileInfo"/>
            <appender-ref ref="RollingFileWarn"/>
            <appender-ref ref="RollingFileError"/>
        </root>
    </loggers>
</configuration>
  • 2
    点赞
  • 0
    评论
  • 14
    收藏
  • 打赏
    打赏
  • 扫一扫,分享海报

©️2022 CSDN 皮肤主题:博客之星2021 设计师:Hiro_C 返回首页

打赏作者

hyk今天写算法了吗

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值