实现简单的用户管理系统

系统名称:

用户管理系统

功能需求:

对用户信息进行增加、删除、修改、查询
分页查询
条件查询
对信息进行批量删除

使用技术:

Servlet
JSP
MySQL
JDBCTempleat
Duird
BeanUtilS
tomcat

系统架构:

在编码阶段使用三层架构模式:
1.界面层(表示层):用户可以通过界面上的组件和服务器进行交互:接收用户参数,封装数据,调用业务逻辑层完成处理,转发jsp页面完成显示。
2.业务逻辑层(service层):处理业务逻辑:组合DAO层中的简单方法,形成复杂的功能(业务逻辑操作)。
3.数据访问层(dao层):操作数据存储文件:定义了对于数据库最基本的CRUD操作。

实现页面:

登录页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <meta name="viewport" content="width=device-width, initial-scale=1"/>
    <title>管理员登录</title>

    <!-- 1. 导入CSS的全局样式 -->
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <!-- 2. jQuery导入,建议使用1.9以上的版本 -->
    <script src="js/jquery-2.1.0.min.js"></script>
    <!-- 3. 导入bootstrap的js文件 -->
    <script src="js/bootstrap.min.js"></script>
    <script type="text/javascript">

        function refreshcode() {
            var vcode = document.getElementById("vcode");
            vcode.src="${pageContext.request.contextPath}/checkCodeServlet?time="+new Date().getTime();
        }
    </script>
</head>
<body>
<div class="container" style="width: 400px;">
    <h3 style="text-align: center;">管理员登录</h3>
    <form action="${pageContext.request.contextPath}/loginServlet" method="post">
        <div class="form-group">
            <label for="user">用户名:</label>
            <input type="text" name="username" class="form-control" id="user" placeholder="请输入用户名"/>
        </div>

        <div class="form-group">
            <label for="password">密码:</label>
            <input type="password" name="password" class="form-control" id="password" placeholder="请输入密码"/>
        </div>

        <div class="form-inline">
            <label for="vcode">验证码:</label>
            <input type="text" name="verifycode" class="form-control" id="verifycode" placeholder="请输入验证码" style="width: 120px;"/>
            <a href="javascript:refreshCode();">
                <img src="${pageContext.request.contextPath}/checkCodeServlet" title="看不清点击刷新" id="vcode"/></a>
        </div>
        <hr/>
        <div class="form-group" style="text-align: center;">
            <input class="btn btn btn-primary" type="submit" value="登录">
        </div>
    </form>

    <!-- 出错显示的信息框 -->
    <div class="alert alert-warning alert-dismissible" role="alert">
        <button type="button" class="close" data-dismiss="alert" >
            <span>&times;</span></button>
        <strong>${login_msg}</strong>
    </div>
</div>
</body>
</html>

登录后台

package cn.itcast.web.servlet;

import cn.itcast.domain.User;
import cn.itcast.service.UserService;
import cn.itcast.service.impl.UserServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

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.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    request.setCharacterEncoding("utf-8");

        String verifycode = request.getParameter("verifycode");

        HttpSession session = request.getSession();
        String checkcode_server = (String) session.getAttribute("CHECKCODE_SERVER");
        session.removeAttribute("CHECKCODE_SERVER");
        if(!checkcode_server.equalsIgnoreCase(verifycode)){
            request.setAttribute("login_msg","验证码错误!");
            request.getRequestDispatcher("/login.jsp").forward(request,response);

            return;
        }

        Map<String, String[]> map = request.getParameterMap();
        User user =new User();
        try {
            BeanUtils.populate(user,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        UserService service=new UserServiceImpl();
        User loginUser=service.login(user);
        if(loginUser!=null){
            session.setAttribute("user",loginUser);
            response.sendRedirect(request.getContextPath()+"/index.jsp");

        }else {
            request.setAttribute("login_msg","用户名或密码错误!");
            request.getRequestDispatcher("/login.jsp").forward(request,response);
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}

添加页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>



<html lang="zh-CN">
<head>
    
    <meta charset="utf-8">
    
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    
    <meta name="viewport" content="width=device-width, initial-scale=1">
    
    <title>添加用户title>

    
    <link href="css/bootstrap.min.css" rel="stylesheet">
    
    <script src="js/jquery-2.1.0.min.js">script>
    
    <script src="js/bootstrap.min.js">script>
head>
<body>
<div class="container">
    <center><h3>添加联系人页面h3>center>
    <form action="${pageContext.request.contextPath}/addUserServlet" method="post">
        <div class="form-group">
            <label for="name">姓名:label>
            <input type="text" class="form-control" id="name" name="name" placeholder="请输入姓名">
        div>

        <div class="form-group">
            <label>性别:label>
            <input type="radio" name="gender" value="" checked="checked"/><input type="radio" name="gender" value=""/>div>

        <div class="form-group">
            <label for="age">年龄:label>
            <input type="text" class="form-control" id="age" name="age" placeholder="请输入年龄">
        div>

        <div class="form-group">
            <label for="address">籍贯:label>
            <select name="address" class="form-control" id="jiguan">
                <option value="广东">广东option>
                <option value="广西">广西option>
                <option value="湖南">湖南option>
            select>
        div>

        <div class="form-group">
            <label for="qq">QQ:label>
            <input type="text" class="form-control" name="qq" placeholder="请输入QQ号码"/>
        div>

        <div class="form-group">
            <label for="email">Email:label>
            <input type="text" class="form-control" name="email" placeholder="请输入邮箱地址"/>
        div>

        <div class="form-group" style="text-align: center">
            <input class="btn btn-primary" type="submit" value="提交" />
            <input class="btn btn-default" type="reset" value="重置" />
            <input class="btn btn-default" type="button" value="返回" />
        div>
    form>
div>
body>
html>

添加后台

package cn.itcast.web.servlet;

import cn.itcast.domain.User;
import cn.itcast.service.UserService;
import cn.itcast.service.impl.UserServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/addUserServlet")
public class AddUserServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        Map<String, String[]> map = request.getParameterMap();
        User user =new User();
        try {
            BeanUtils.populate(user,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        UserService service=new UserServiceImpl();
        service.addUser(user);

        response.sendRedirect(request.getContextPath()+"/userListServlet");

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}

修改页面

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


<html lang="zh-CN">
    <head>
        
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>修改用户title>

        <link href="css/bootstrap.min.css" rel="stylesheet">
        <script src="js/jquery-2.1.0.min.js">script>
        <script src="js/bootstrap.min.js">script>
        
    head>
    <body>
        <div class="container" style="width: 400px;">
        <h3 style="text-align: center;">修改联系人h3>
        <form action="${pageContext.request.contextPath}/updateUserServlet" method="post">

            <input type="hidden" name="id" value="${user.id}">

          <div class="form-group">
            <label for="name">姓名:label>
            <input type="text" class="form-control" id="name" name="name" value="${user.name}" readonly="readonly" placeholder="请输入姓名" />
          div>

          <div class="form-group">
            <label>性别:label>
              <c:if test="${user.gender==''}">
                  <input type="radio" name="gender" value="" checked /><input type="radio" name="gender" value=""  />c:if>
              <c:if test="${user.gender==''}">
                  <input type="radio" name="gender" value=""  /><input type="radio" name="gender" value="" checked />c:if>

          div>

          <div class="form-group">
            <label for="age">年龄:label>
            <input type="text" class="form-control" id="age" value="${user.age}" name="age" placeholder="请输入年龄" />
          div>

          <div class="form-group">
            <label for="address">籍贯:label>
             <select name="address" class="form-control" >
                 <c:if test="${user.address=='广东'}">
                <option value="广东" selected>广东option>
                <option value="广西">广西option>
                <option value="湖南">湖南option>
                 c:if>

                 <c:if test="${user.address=='广西'}">
                     <option value="广东" >广东option>
                     <option value="广西" selected>广西option>
                     <option value="湖南">湖南option>
                 c:if>

                 <c:if test="${user.address=='湖南'}">
                     <option value="广东" >广东option>
                     <option value="广西">广西option>
                     <option value="湖南" selected>湖南option>
                 c:if>
            select>
          div>

          <div class="form-group">
            <label for="qq">QQ:label>
            <input type="text" class="form-control" value="${user.qq}" name="qq" placeholder="请输入QQ号码"/>
          div>

          <div class="form-group">
            <label for="email">Email:label>
            <input type="text" class="form-control" value="${user.email}" name="email" placeholder="请输入邮箱地址"/>
          div>

             <div class="form-group" style="text-align: center">
                <input class="btn btn-primary" type="submit" value="提交" />
                <input class="btn btn-default" type="reset" value="重置" />
                <input class="btn btn-default" type="button" value="返回"/>
             div>
        form>
        div>
    body>
html>

修改后台

package cn.itcast.web.servlet;

import cn.itcast.domain.User;
import cn.itcast.service.UserService;
import cn.itcast.service.impl.UserServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/updateUserServlet")
public class UpdateUserServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        Map<String, String[]> map = request.getParameterMap();
        User user=new User();
        try {
            BeanUtils.populate(user,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        UserService service=new UserServiceImpl();
        service.updateUser(user);

        response.sendRedirect(request.getContextPath()+"/userListServlet");

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}

分页查询

package cn.itcast.web.servlet;

import cn.itcast.domain.PageBean;
import cn.itcast.domain.User;
import cn.itcast.service.UserService;
import cn.itcast.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 java.io.IOException;
import java.util.Map;

@WebServlet("/findUserByPageServlet")
public class FindUserByPageServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String currentPage = request.getParameter("currentPage");
        String rows = request.getParameter("rows");



        if(currentPage==null||"".equals(currentPage)){
            currentPage="1";
        }
        if(rows==null||"".equals(rows)){
            rows="5";
        }

        Map<String, String[]> condition = request.getParameterMap();

        UserService service=new UserServiceImpl();
        PageBean<User> pb =service.findUserByPage(currentPage,rows,condition);

        System.out.println(pb);
        request.setAttribute("pb",pb);
        request.setAttribute("condition",condition);
        request.getRequestDispatcher("/list.jsp").forward(request,response);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}

public class UserServiceImpl implements UserService {
 @Override
    public PageBean<User> findUserByPage(String _currentPage, String _rows, Map<String, String[]> condition) {
        int currentPage = Integer.parseInt(_currentPage);
        int rows = Integer.parseInt(_rows);

        if (currentPage<=0){
            currentPage = 1;
        }

        //创建空的PageBean对象
        PageBean<User> pb = new PageBean<>();
        //设置参数
        pb.setCurrentPage(currentPage);
        pb.setRows(rows);
        //调用dao查询总记录数
        int totalCount = dao.findTotalCount(condition);
        pb.setTotalCount(totalCount);
        //调用dao查询list集合
            //计算开始的记录索引
        int start = (currentPage-1)*rows;
        List<User> list = dao.finByPage(start,rows,condition);
        pb.setList(list);
        //计算总页码
        int totalPage = totalCount % rows == 0 ? totalCount/rows : (totalCount/rows)+1;
        pb.setTotalPage(totalPage);

        return pb;
    }
}

模糊查询
复杂条件查询的代码只需要在上面分页查询的代码上更改一点东西就行。
主要的原理就是在查询的具体方法中,多传入一个condition条件参数,并且将最后查询到的结果保存在一个map集合中,返回后暂时显示在page页面上

你可能感兴趣的:(jsp)