数据库程序设计第四天--管理员权限
一、说在前面
今天目标完成管理员权限的相关操作,对用户的增删改查操作
二、任务进度
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <link rel="stylesheet" href="layui/layui.css" > <script type="text/javascript" src="js/bootstrap.js"></script> <script type="text/javascript" src="layui/layui.js"></script> <script src="layui/layui.all.js"></script> <title>用户管理</title> </head> <body> <% Object message = request.getAttribute("message"); Object list =request.getAttribute("list"); if(message!=null && !"".equals(message)){ %> <script type="text/javascript"> alert("<%=request.getAttribute("message")%>"); </script> <% } %> <table class="layui-table"> <colgroup> <col width="250"> <col width="250"> <col width="300"> <col width="150"> </colgroup> <thead> <tr> <th>账号</th> <th>密码</th> <th>级别(0:管理员 / 1:隔离地管理员 / 2:隔离人员)</th> <th>操作</th> </tr> </thead> <tbody> <tr> <form action="insertPeopleServlet" class="layui-form" id="form" onsubmit="return check()"> <td><input type="text" id="account" name="account" required lay-verify="required" autocomplete="off" class="layui-input"></td> <td><input type="text" id="password" name="password" required lay-verify="required" autocomplete="off" class="layui-input"></td> <td><input type="text" id="level" name="level" required lay-verify="required|number" autocomplete="off" class="layui-input"></td> <td><button type="submit" class="layui-btn" lay-submit lay-filter="formDemo" ><i class="layui-icon " style="font-size: 23px; color: white;">  </i></button></td> </form> </tr> <c:forEach var="item" items="${list}"> <tr> <td>${item.account}</td> <td>${item.password}</td> <td>${item.level}</td> <td><a href="update_people.jsp?account=${item.account}&password=${item.password}&level=${item.level}" target="_self"> <button type="button" class="layui-btn layui-btn-sm"><i class="layui-icon" style="font-size: 23px; color: white;"></i></button></a> <a href="deletePeopleServlet?account=${item.account}" onclick="return confirm('确定删除么')"><button type="button" class="layui-btn layui-btn-danger layui-btn-sm"><i class="layui-icon" style="font-size: 23px; color: white;"></i></button></a> </td> </tr> </c:forEach> </tbody> </table> </body> <script> //Demo layui.use('form', function(){ var form = layui.form; //监听提交 form.on('submit(formDemo)', function(data){ layer.msg(JSON.stringify(data.field)); return true; }); }); function check(){ var account=document.getElementById("account"); var password=document.getElementById("password"); var level=document.getElementById("level"); if(account.value==""){ alert("账号不能为空"); account.focus(); return false; } else if(password.value==""){ alert("密码不能为空"); password.focus(); return false; } else if(level.value==""){ alert("等级不能为空"); level.focus(); return false; } else if(!(level.value=='0'||level.value=='1'||level.value=='2')){ alert(level.value); level.focus(); return false; } } </script> </html>
package Servlet; import java.io.IOException; import java.util.ArrayList; import java.util.List; 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 Bean.LoginBean; import Dao.LoginDao; /** * Servlet implementation class AdminShowServlet */ @WebServlet("/adminShowServlet") public class AdminShowServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public AdminShowServlet() { super(); // TODO Auto-generated constructor stub } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); List<LoginBean> list=new ArrayList(); LoginDao login=new LoginDao(); list=login.list(); request.setAttribute("list", list); request.getRequestDispatcher("adminshow.jsp").forward(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package Servlet; import java.io.IOException; 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 Bean.LoginBean; import Dao.LoginDao; /** * Servlet implementation class DeletePeopleServlet */ @WebServlet("/deletePeopleServlet") public class DeletePeopleServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public DeletePeopleServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); String account=request.getParameter("account"); System.out.println(account); LoginDao loginjudger=new LoginDao(); if(loginjudger.delete(account)){ request.setAttribute("message", "删除成功"); request.getRequestDispatcher("adminShowServlet").forward(request, response); }else { request.setAttribute("message", "删除失败"); request.getRequestDispatcher("adminShowServlet").forward(request, response); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package Servlet; import java.io.IOException; 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 Bean.LoginBean; import Dao.LoginDao; /** * Servlet implementation class UpdatePeopleServlet */ @WebServlet("/updatePeopleServlet") public class UpdatePeopleServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public UpdatePeopleServlet() { super(); // TODO Auto-generated constructor stub } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); String account=request.getParameter("account"); String password=request.getParameter("password"); int level=Integer.parseInt(request.getParameter("level")); LoginDao loginjudger=new LoginDao(); LoginBean login=new LoginBean(account, password, level); if(loginjudger.update(login)){ request.setAttribute("message", "修改成功"); request.getRequestDispatcher("adminShowServlet").forward(request, response); }else { // System.out.println("用户不存在"); request.setAttribute("message", "修改失败"); request.getRequestDispatcher("adminShowServlet").forward(request, response); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package Servlet; import java.io.IOException; 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 Bean.LoginBean; import Dao.LoginDao; /** * Servlet implementation class InsertPeopleServlet */ @WebServlet("/insertPeopleServlet") public class InsertPeopleServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public InsertPeopleServlet() { super(); // TODO Auto-generated constructor stub } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); String account=request.getParameter("account"); String password=request.getParameter("password"); int level=Integer.parseInt(request.getParameter("level")); LoginBean login=new LoginBean(account, password, level); LoginDao loginjudger=new LoginDao(); if(loginjudger.insert(login)) { request.setAttribute("message", "添加成功"); request.getRequestDispatcher("adminShowServlet").forward(request, response); }else { request.setAttribute("message", "添加失败"); request.getRequestDispatcher("adminShowServlet").forward(request, response); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }