jsp+servlet+tomcat实现网上商城(上)

简介: 代码如下

项目总览

2345_image_file_copy_26.jpg

2345_image_file_copy_27.jpg


2345_image_file_copy_28.jpg


2345_image_file_copy_29.jpg

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


目录
相关文章
|
17天前
|
Java
学校教师管理系统【JSP+Servlet+JavaBean】(Java课设)
学校教师管理系统【JSP+Servlet+JavaBean】(Java课设)
19 1
|
17天前
|
Java
人事管理系统【JSP+Servlet+JavaBean】(Java课设)
人事管理系统【JSP+Servlet+JavaBean】(Java课设)
16 0
|
29天前
使用Servlet上传多张图片——前台页面层(Index.jsp)
使用Servlet上传多张图片——前台页面层(Index.jsp)
12 0
|
1月前
|
Java 应用服务中间件
解决tomcat启动报错:无法在web.xml或使用此应用程序部署的jar文件中解析绝对的url [http:java.sun.com/jsp/jstl/core]
解决tomcat启动报错:无法在web.xml或使用此应用程序部署的jar文件中解析绝对的url [http:java.sun.com/jsp/jstl/core]
105 1
|
2月前
|
Java
jsp页面中使用jstl标签报错:javax.servlet.jsp.JspTagException
jsp页面中使用jstl标签报错:javax.servlet.jsp.JspTagException
13 0
|
3月前
|
前端开发 Java 应用服务中间件
HTTP&Tomcat&Servlet
HTTP&Tomcat&Servlet
58 0
|
3月前
|
存储 前端开发 Java
JavaWeb:servlet+jsp+mybatis商品管理增删改查
商品管理通常包括增加(添加)、删除、修改和查询商品信息
43 1
JavaWeb:servlet+jsp+mybatis商品管理增删改查
|
3月前
|
Java 数据安全/隐私保护
基于JSP实现网上商城系统
基于JSP实现网上商城系统
|
3月前
|
设计模式 前端开发 Java
基于JSP/SERVLET学生管理系统
基于JSP/SERVLET学生管理系统
|
3月前
|
Java 数据安全/隐私保护 计算机视觉
基于JSP/SERVLET实现的人脸识别考勤系统(二)
基于JSP/SERVLET实现的人脸识别考勤系统