基于servlet+jsp框架的管理系统:简单实现增、删、改、查。

在上一条博客中的代码基础上,进行修改。

第一步:

将原先的UserDao名字改成IUserDao,新建一个impl包,在包里新建UserDaoImpl.java
基于servlet+jsp框架的管理系统:简单实现增、删、改、查。_第1张图片

IUserDao:

package com.zhongruan.dao;

import com.zhongruan.model.User;

import java.util.List;

public interface IUserDao {
   User findUserByUsername(String username);
   List<User>findAll();
   void delete(int id);
   void addServlet(String username, String password);
   void update(int id, String username, String password);
}

UserDaoImpl:

public class UserDaoImpl implements IUserDao {

    @Override
    public User findUserByUsername(String username) {
        User user=null;
        Connection connection=null;
        PreparedStatement statement=null;
        ResultSet resultSet=null;
        try{
            connection=DBUtil.getConnection();
            String sql="select * from account where username=?";
            statement=connection.prepareStatement(sql);
            statement.setString(1,username);
            resultSet=statement.executeQuery();
            while (resultSet.next()){
                user=new User();
                user.setId(resultSet.getInt(1));
                user.setUsername(username);
                user.setPassword(resultSet.getString(3));
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
           DBUtil.closeAll(resultSet,statement,connection);
       }
      return user;
    }

   @Override
  public List<User> findAll() {
        ResultSet resultSet = null;
        PreparedStatement statement = null;
        Connection connection = null;
        List<User>users = new ArrayList<>();
        try {
            connection = DBUtil.getConnection();
            String sql = "select * from account";
            statement = connection.prepareStatement(sql);
            resultSet = statement.executeQuery();
            while (resultSet.next()) {
                User user = new User();
                user.setId(resultSet.getInt(1));
                user.setUsername(resultSet.getString(2));
                user.setPassword(resultSet.getString(3));
                users.add(user);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeALL(resultSet, statement, connection);
        }
        return users;
    }

    @Override
    public void delete(int id) {
        Connection connection=null;
        PreparedStatement statement=null;
        try {
            connection=DBUtil.getConnection();
            statement=connection.prepareStatement("delete from account where id=?");
            statement.setInt(1,id);
            statement.executeUpdate();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.closeAll(null,statement,connection);
        }
          }

    @Override
    public void addServlet(String username, String password) {
        Connection connection=null;
        PreparedStatement statement=null;
        try {
            connection=DBUtil.getConnection();
            statement=connection.prepareStatement("insert into account(username,password) values (?,?)");
            statement.setString(1,username);
            statement.setString(2,password);
            statement.executeUpdate();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.closeAll(null,statement,connection);
        }
    }

    @Override
    public void update(int id, String username, String password) {
        Connection connection=null;
        PreparedStatement statement=null;
       try {
            connection=DBUtil.getConnection();
            statement=connection.prepareStatement("update account set username=?,password=? where id=?");
            statement.setString(1,username);
            statement.setString(2,password);
            statement.setInt(3,id);
            statement.executeUpdate();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.closeAll(null,statement,connection);
        }
    }

}

在service新建一个impl包,在包里新建UserServiceImpl.java

UserServiceImpl :

package com.zhongruan.service.impl;


import com.zhongruan.dao.IUserDao;
import com.zhongruan.dao.impl.UserDaoImpl;
import com.zhongruan.model.User;
import com.zhongruan.service.IUserService;

import java.util.List;

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

    @Override
    public Boolean login(String username, String password) {

        User user=userDao.findUserByUsername(username);
        if (user!=null&&user.getPassword().equals(password)){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public List<User> findAll() {

        List<User> users=userDao.findAll();
        return users;
    }

    @Override
    public void delete(int id2) {
        userDao.delete(id2);
    }

    @Override
    public void add(String username, String password) {
        userDao.addServlet(username,password);
    }

    @Override
    public void update(int id, String username, String password) {
        userDao.update(id,username,password);
    }

}

将UserService改成IUserService

IUserService:

package com.zhongruan.service;


import com.zhongruan.model.User;

import java.util.List;

public interface IUserService {
    Boolean login(String username, String password);
    List<User> findAll();
    void delete(int id2);
    void add(String username, String password);
    void update(int id, String username, String password);
}

新建一个servlet包,新建五个java,分别为FindServlet / LoginServlet / DeleteServlete /AddServlet /UpdateServlet。

FindServlet:

package com.zhongruan.servlet;

import com.zhongruan.model.User;
import com.zhongruan.service.IUserService;
import com.zhongruan.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
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.util.List;

public class FindServlet extends HttpServlet {
    IUserService userService=new UserServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<User> users=userService.findAll();
        HttpSession session=req.getSession();
        session.setAttribute("userlist",users);
        req.getRequestDispatcher("allUser.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<User> users=userService.findAll();
        HttpSession session=req.getSession();
        session.setAttribute("userlist",users);
        req.getRequestDispatcher("allUser.jsp").forward(req,resp);
}
}

LoginServlet:

package com.zhongruan.servlet;

import com.zhongruan.dao.IUserDao;
import com.zhongruan.model.User;
import com.zhongruan.service.IUserService;
import com.zhongruan.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
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.util.List;

public class LoginServlet extends HttpServlet{

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            String username=req.getParameter("username");
            String password=req.getParameter("password");
            IUserService userService=  new UserServiceImpl();
            Boolean flag=userService.login(username,password);
        if (flag) {
            req.getRequestDispatcher("/find").forward(req, resp);
        } else {
            req.getRequestDispatcher("failure.jsp").forward(req, resp);
        }
    }
    }


DeleteServlete:

package com.zhongruan.servlet;

import com.zhongruan.service.IUserService;
import com.zhongruan.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class DeleteServlete extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id1=req.getParameter("id");
        int id2=Integer.parseInt(id1);
        IUserService userService=new UserServiceImpl();
        userService.delete(id2);
        req.getRequestDispatcher("/find").forward(req, resp);
    }
}

AddServlet:


import com.zhongruan.service.IUserService;
import com.zhongruan.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class AddServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username=req.getParameter("username");
        String password=req.getParameter("password");
        IUserService userService=  new UserServiceImpl();
        userService.add(username,password);
        req.getRequestDispatcher("/find").forward(req, resp);
    }
}

UpdateServlet :

package com.zhongruan.servlet;

import com.zhongruan.service.IUserService;
import com.zhongruan.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class UpdateServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username=req.getParameter("username");
        String password=req.getParameter("password");
        String id1=req.getParameter("id");
        int id2=Integer.parseInt(id1);
        IUserService userService=new UserServiceImpl();
        userService.update(id2,username,password);
        req.getRequestDispatcher("/find").forward(req, resp);
    }
}

更改web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <servlet>
        <servlet-name>LoginServlet</servlet-name>
        <servlet-class>com.zhongruan.servlet.LoginServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>LoginServlet</servlet-name>
        <url-pattern>/login</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>DeleteServlet</servlet-name>
        <servlet-class>com.zhongruan.servlet.DeleteServlete</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>DeleteServlet</servlet-name>
        <url-pattern>/delete</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>FindServlet</servlet-name>
        <servlet-class>com.zhongruan.servlet.FindServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>FindServlet</servlet-name>
        <url-pattern>/find</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>AddServlet</servlet-name>
        <servlet-class>com.zhongruan.servlet.AddServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>AddServlet</servlet-name>
        <url-pattern>/add</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>UpdateServlet</servlet-name>
        <servlet-class>com.zhongruan.servlet.UpdateServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>UpdateServlet</servlet-name>
        <url-pattern>/update</url-pattern>
    </servlet-mapping>
</web-app>

添加addUser.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" isELIgnored="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>
<head>
    <title>新增用户</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- 引入 Bootstrap -->
    <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
    <div class="row clearfix">
        <div class="col-md-12 column">
            <div class="page-header">
                <h1>
                    基于servlet+jsp框架的管理系统:简单实现增、删、改、查。
                </h1>
            </div>
        </div>
    </div>

    <div class="row clearfix">
        <div class="col-md-12 column">
            <div class="page-header">
                <h1>
                    <small>新增用户</small>
                </h1>
            </div>
        </div>
    </div>
    <form action="/add" method="post">
        <%--&nbsp;&nbsp;&nbsp;&nbsp;id:<input type="text" name="id"><br><br><br>--%>
        用户姓名:<input type="text" name="username"><br><br><br>
        用户密码:<input type="text" name="password"><br><br><br>
        <input type="submit" value="添加" >
    </form>

</div>

添加update.jsp

<%--
  Created by IntelliJ IDEA.
  User: MyPC
  Date: 2019/9/29
  Time: 14:45
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>修改</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- 引入 Bootstrap -->
    <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>

<div class="container">
    <div class="row clearfix">
        <div class="col-md-12 column">
            <div class="page-header">
                <h1>
                    基于servlet+jsp框架的管理系统:简单实现增、删、改、查。
                </h1>
            </div>
        </div>
    </div>

    <div class="row clearfix">
        <div class="col-md-12 column">
            <div class="page-header">
                <h1>
                    <small>修改用户</small>
                </h1>
            </div>
        </div>
    </div>
    <form action="/update" method="post">&nbsp;&nbsp;&nbsp;&nbsp;id:<input type="text" name="id"><br><br><br>
        用户姓名:<input type="text" name="username"><br><br><br>
        用户密码:<input type="text" name="password"><br><br><br>
        <input type="submit" value="更改" >
    </form>
</div>
</body>
</html>

新建allUrer

<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" isELIgnored="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>
<head>
    <title>user列表</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- 引入 Bootstrap -->
    <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
    <div class="row clearfix">
        <div class="col-md-12 column">
            <div class="page-header">
                <h1>
                    基于servlet+jsp框架的管理系统:简单实现增、删、改、查。
                </h1>
            </div>
        </div>
    </div>

    <div class="row clearfix">
        <div class="col-md-12 column">
            <div class="page-header">
                <h1>
                    <small>用户列表 —— 显示所有用户</small>
                </h1>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-md-4 column">
            <a class="btn btn-primary" href="addUser.jsp">新增</a>
        </div>
    </div>
    <div class="row clearfix">
        <div class="col-md-12 column">
            <table class="table table-hover table-striped">
                <thead>
                <tr>
                    <th>id</th>
                    <th>用户名</th>
                    <th>密码</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                <c:forEach var="user" items="${userlist}">
                    <tr>
                        <td>${user.id}</td>
                        <td>${user.username}</td>
                        <td>${user.password}</td>
                        <td>
                            <a href="/delete?id=${user.id}">删除</a>|
                            <a href="updateUser.jsp">修改</a>
                        </td>
                    </tr>
                </c:forEach>

                </tbody>
            </table>
        </div>
    </div>
</div>

你可能感兴趣的:(基于servlet+jsp框架的管理系统:简单实现增、删、改、查。)