当前位置:   article > 正文

基于javaweb的图书管理系统_基于web的图书管理系统

基于web的图书管理系统

一、系统简介

本项目采用eclipse工具开发,jsp+servlet技术编写,样式采用了layui前端框架,数据库采用的是mysql,navicat开发工具。

系统一共分为2个角色分别是:管理员,学生

二、模块简介
管理员


1、登录

2、修改个人信息

3、图书类型管理

4、图书管理

5、图书借阅管理

6、借阅审批管理

7、图书借阅统计

8、学生管理

9、公告管理

学生
1、登录、注册

2、个人信息修改

3、图书借阅

4、借阅管理

5、借阅统计

6、查看公告

三、相关截图

项目结构、数据库结构

 

 登录注册页面

 

 图书类型管理

 图书管理

 学生管理

 审批管理

 

借阅管理

 公告管理

 管理员统计

 学生统计

 图书借阅

 借阅历史

 四、相关代码

登录代码

用户通过login.jsp页面填写指定的用户名和密码,点击登录按钮,执行servlet里面的方法,后台校验成功后,返回main.jsp

  1. <%@ page language="java" contentType="text/html; charset=utf-8"
  2. pageEncoding="utf-8"%>
  3. <%
  4. String path = request.getContextPath();
  5. String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
  6. %>
  7. <!DOCTYPE html>
  8. <html>
  9. <head>
  10. <meta charset="utf-8">
  11. <title>Login Page</title>
  12. <!-- 样 式 文 件 -->
  13. <link rel="stylesheet" href="resource/component/pear/css/pear.css" />
  14. <link rel="stylesheet" href="resource/admin/css/other/login.css" />
  15. </head>
  16. <!-- 代 码 结 构 -->
  17. <body background="resource/admin/images/background.svg" style="background-size: cover;">
  18. <form class="layui-form" action="LoginServlet?action=login" method="post" id="loginForm" name="loginForm" >
  19. <div class="layui-form-item">
  20. <img class="logo" src="resource/admin/images/logo.png" />
  21. <div class="title">&emsp;&emsp;</div>
  22. <div class="desc">
  23. 图 书 管 理 系 统
  24. </div>
  25. </div>
  26. <div class="layui-form-item">
  27. <input type="text" placeholder="请输入用户名或者手机号" name="userName" id="userName" hover class="layui-input" />
  28. <span id="msg3" style="color:red">${message}</span>
  29. </div>
  30. <div class="layui-form-item">
  31. <input id="password" type="password" name="password" placeholder="请输入密码" hover class="layui-input" />
  32. </div>
  33. <div class="layui-form-item" style="text-align: center;">
  34. <input type="radio" name="type" id="type" value="1" title="管理员" checked>
  35. <input type="radio" name="type" id="type" value="2" title="学生" >
  36. </div>
  37. <div class="layui-form-item">
  38. <button type="submit" class="pear-btn pear-btn-success login" id="login">
  39. 登录
  40. </button>
  41. </div>
  42. <div class="layui-form-item">
  43. <a href="LoginServlet?action=toRegister" class="pear-btn pear-btn-warming layui-btn-fluid">去注册</a>
  44. </div>
  45. </form>
  46. <!-- 资 源 引 入 -->
  47. <script src="resource/component/layui/layui.js"></script>
  48. <script src="resource/component/pear/pear.js"></script>
  49. <script>
  50. layui.use(['layer', 'form','jquery'], function(){
  51. var form = layui.form,
  52. layer = layui.layer,
  53. $= layui.jquery;
  54. form.render();//这句一定要加,占坑
  55. $("#login").on("click", function() {
  56. var userName = $("#userName").val().trim(); // trim()去除空格
  57. var password = $("#password").val().trim();
  58. var type = $("#type").val();
  59. if(userName == ""){
  60. layer.msg('用户名或者手机号不能为空!');
  61. return false;
  62. }
  63. if(password == ""){
  64. layer.msg('密码不能为空!');
  65. return false;
  66. }
  67. if(type == ""){
  68. layer.msg('请选择角色!');
  69. return false;
  70. }
  71. });
  72. setTimeout(function(){ $("#msg").html("")},3000);
  73. setTimeout(function(){ $("#msg2").html("")},3000);
  74. setTimeout(function(){ $("#msg3").html("")},3000);
  75. });
  76. </script>
  77. </body>
  78. </html>
  1. private void login(HttpServletRequest request, HttpServletResponse response) {
  2. // TODO Auto-generated method stub
  3. removeAll(request,response);
  4. String userName = request.getParameter("userName");
  5. String password = request.getParameter("password");
  6. String type = request.getParameter("type");
  7. if(type.equals("1")){
  8. Admin admin = new Admin();
  9. admin.setUserName(userName);
  10. admin.setPwd(password);
  11. Admin admin1 = ls.selectAdmin(admin);
  12. try {
  13. if (admin1 == null){
  14. request.setAttribute("message", "管理员用户不存在或者密码错误");
  15. request.getRequestDispatcher("/WEB-INF/views/login.jsp").forward(request, response);
  16. }else {
  17. request.getSession().setAttribute("admin",admin1);
  18. request.getSession().setAttribute("flag",1);
  19. request.getRequestDispatcher("/WEB-INF/views/main.jsp").forward(request, response);
  20. }
  21. } catch (Exception e) {
  22. e.printStackTrace();
  23. }
  24. }else if(type.equals("2")){
  25. Student student = new Student();
  26. student.setStno(userName);
  27. student.setPwd(password);
  28. Student student1 = ls.selectStudent(student);
  29. try {
  30. if (student1 == null){
  31. request.setAttribute("message", "学号不存在或者密码错误");
  32. request.getRequestDispatcher("/WEB-INF/views/login.jsp").forward(request, response);
  33. }else {
  34. request.getSession().setAttribute("student",student1);
  35. request.getSession().setAttribute("flag",2);
  36. request.getRequestDispatcher("/WEB-INF/views/main.jsp").forward(request, response);
  37. }
  38. } catch (Exception e) {
  39. e.printStackTrace();
  40. }
  41. }else{
  42. request.setAttribute("message", "工作人员手机号不存在或者密码错误");
  43. try {
  44. request.getRequestDispatcher("/WEB-INF/views/login.jsp").forward(request, response);
  45. } catch (Exception e) {
  46. e.printStackTrace();
  47. }
  48. }
  49. }

注册代码

学生初始化注册自己的账号,填写form表单,提价后,后台进行插入的操作,注册成功后跳转登录页面

  1. <%@ page language="java" contentType="text/html; charset=utf-8"
  2. pageEncoding="utf-8"%>
  3. <%
  4. String path = request.getContextPath();
  5. String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
  6. %>
  7. <!DOCTYPE html>
  8. <html>
  9. <head>
  10. <meta charset="utf-8">
  11. <title>Login Page</title>
  12. <!-- 样 式 文 件 -->
  13. <link rel="stylesheet" href="resource/component/pear/css/pear.css" />
  14. <link rel="stylesheet" href="resource/admin/css/other/login.css" />
  15. </head>
  16. <!-- 代 码 结 构 -->
  17. <body background="resource/admin/images/background.svg" style="background-size: cover;">
  18. <form class="layui-form" action="StudentServlet?action=addStudent" method="post" id="loginForm" name="loginForm" >
  19. <input id="te" type="hidden" name="te" value="1" />
  20. <div class="layui-form-item">
  21. <img class="logo" src="resource/admin/images/logo.png" />
  22. <div class="title">&emsp;&emsp;</div>
  23. <div class="desc">
  24. 图 书 管 理 系 统
  25. </div>
  26. </div>
  27. <div class="layui-form-item">
  28. <input type="text" placeholder="请输入姓名" name="name" id="name" hover class="layui-input" />
  29. <span id="msg" style="color:red">${message}</span>
  30. </div>
  31. <div class="layui-form-item">
  32. <input id="stno" type="text" name="stno" placeholder="请输入学号" hover class="layui-input" />
  33. </div>
  34. <div class="layui-form-item">
  35. <select name="sex" id ="sex">
  36. <option value="">请选择性别</option>
  37. <option value="男"></option>
  38. <option value="女"></option>
  39. </select>
  40. </div>
  41. <div class="layui-form-item">
  42. <input id="classes" type="text" name="classes" placeholder="请输入班级" hover class="layui-input" />
  43. </div>
  44. <div class="layui-form-item">
  45. <input id="time" type="text" name="time" placeholder="请输入入学时间" hover class="layui-input" />
  46. </div>
  47. <div class="layui-form-item">
  48. <input id="phone" type="text" name="phone" placeholder="请输入手机号" hover class="layui-input" />
  49. </div>
  50. <div class="layui-form-item">
  51. <input id="pwd" type="text" name="pwd" placeholder="请输入登录密码" hover class="layui-input" />
  52. </div>
  53. <div class="layui-form-item">
  54. <input id="xy" type="text" name="xy" placeholder="请输入学院" hover class="layui-input" />
  55. </div>
  56. <div class="layui-form-item">
  57. <button type="submit" class="pear-btn pear-btn-success login" id="login">
  58. 注册
  59. </button>
  60. </div>
  61. <div class="layui-form-item">
  62. <a href="LoginServlet?action=toLogin" class="pear-btn pear-btn-warming layui-btn-fluid">返回</a>
  63. </div>
  64. </form>
  65. <!-- 资 源 引 入 -->
  66. <script src="resource/component/layui/layui.js"></script>
  67. <script src="resource/component/pear/pear.js"></script>
  68. <script>
  69. layui.use(['layer', 'form','jquery','laydate'], function(){
  70. var form = layui.form,
  71. layer = layui.layer,
  72. laydate=layui.laydate,
  73. $= layui.jquery;
  74. form.render();//这句一定要加,占坑
  75. laydate.render({
  76. elem: '#time'
  77. });
  78. $("#login").on("click", function() {
  79. var name = $("#name").val();
  80. var sex = $("#sex").val();
  81. var classes = $("#classes").val();
  82. var time = $("#time").val();
  83. var xy = $("#xy").val();
  84. var phone = $("#phone").val();
  85. var pwd = $("#pwd").val();
  86. var stno = $("#stno").val();
  87. if(sex == null || sex== ""){
  88. layer.msg('性别不能为空!');
  89. return false;
  90. }
  91. if(name == null || name== ""){
  92. layer.msg('姓名不能为空!');
  93. return false;
  94. }
  95. if(classes == null || classes== ""){
  96. layer.msg('班级不能为空!');
  97. return false;
  98. }
  99. if(time == null || time== ""){
  100. layer.msg('入学时间不能为空!');
  101. return false;
  102. }
  103. if(xy == null || xy== ""){
  104. layer.msg('学院不能为空!');
  105. return false;
  106. }
  107. if(phone == null || phone== ""){
  108. layer.msg('手机号不能为空!');
  109. return false;
  110. }
  111. if(pwd == null || pwd== ""){
  112. layer.msg('密码不能为空!');
  113. return false;
  114. }
  115. if(stno == null || stno== ""){
  116. layer.msg('学号不能为空!');
  117. return false;
  118. }
  119. });
  120. setTimeout(function(){ $("#msg").html("")},3000);
  121. setTimeout(function(){ $("#msg2").html("")},3000);
  122. setTimeout(function(){ $("#msg3").html("")},3000);
  123. });
  124. </script>
  125. </body>
  126. </html>
  1. /**
  2. * 新增学生信息
  3. * @param request
  4. * @param response
  5. */
  6. private void addStudent(HttpServletRequest request,
  7. HttpServletResponse response) {
  8. String te = request.getParameter("te");
  9. String name = request.getParameter("name");
  10. String sex = request.getParameter("sex");
  11. String classes = request.getParameter("classes");
  12. String time = request.getParameter("time");
  13. String xy = request.getParameter("xy");
  14. String phone = request.getParameter("phone");
  15. String pwd = request.getParameter("pwd");
  16. String stno = request.getParameter("stno");
  17. Student stu = new Student();
  18. stu.setClasses(classes);
  19. stu.setName(name);
  20. stu.setPhone(phone);
  21. stu.setPwd(pwd);
  22. stu.setSex(sex);
  23. stu.setState("1");
  24. stu.setStno(stno);
  25. stu.setTime(time);
  26. stu.setXy(xy);
  27. Student student = ss.selectStudentByPhone(phone);
  28. Student student1 = ss.selectStudentByStuNo(stno);
  29. if(te == null || te.equals("")){
  30. if(student != null){
  31. request.setAttribute("message", "电话号码重复!");
  32. try {
  33. request.getRequestDispatcher("/WEB-INF/views/student/addStudent.jsp").forward(request,response);
  34. } catch (Exception e) {
  35. e.printStackTrace();
  36. }
  37. }else if(student1 != null){
  38. request.setAttribute("message", "学号重复!");
  39. try {
  40. request.getRequestDispatcher("/WEB-INF/views/student/addStudent.jsp").forward(request,response);
  41. } catch (Exception e) {
  42. e.printStackTrace();
  43. }
  44. }else{
  45. try {
  46. ss.saveStudent(stu);
  47. response.sendRedirect(request.getContextPath()+"/StudentServlet?action=studentList");
  48. } catch (Exception e) {
  49. e.printStackTrace();
  50. }
  51. }
  52. }else{
  53. if(student != null){
  54. request.setAttribute("message", "电话号码重复!");
  55. try {
  56. request.getRequestDispatcher("/WEB-INF/views/register.jsp").forward(request,response);
  57. } catch (Exception e) {
  58. e.printStackTrace();
  59. }
  60. }else if(student1 != null){
  61. request.setAttribute("message", "学号重复!");
  62. try {
  63. request.getRequestDispatcher("/WEB-INF/views/register.jsp").forward(request,response);
  64. } catch (Exception e) {
  65. e.printStackTrace();
  66. }
  67. }else{
  68. try {
  69. ss.saveStudent(stu);
  70. response.sendRedirect(request.getContextPath()+"/LoginServlet?action=toLogin");
  71. } catch (Exception e) {
  72. e.printStackTrace();
  73. }
  74. }
  75. }
  76. }

学生列表代码

后台分页查询所有学生的信息,返回到studentList.jsp中,进行遍历展示

  1. /**
  2. * 分页查询学生
  3. * @param request
  4. * @param response
  5. */
  6. private void studentList(HttpServletRequest request,
  7. HttpServletResponse response) {
  8. String p=request.getParameter("p");//接收页码
  9. int pageSize=4;//每页显示5条
  10. int pageNum=1; //默认第一页
  11. if(p!=null){
  12. pageNum= Integer.parseInt(p);
  13. }
  14. //调用分页查询
  15. List<Student> list=ss.getstudentListPage(pageNum,pageSize);
  16. //携带参数到页面
  17. int nums=ss.queryStudentCount(); //查询总数
  18. //计算总页数
  19. int totalPage=(nums%pageSize==0)? (nums/pageSize):(nums/pageSize+1);
  20. request.setAttribute("cp",pageNum); //当前页
  21. request.setAttribute("tp",totalPage); //总页数
  22. try {
  23. request.removeAttribute("msg");
  24. request.setAttribute("list" ,list);
  25. request.getRequestDispatcher("/WEB-INF/views/student/studentList.jsp").forward(request, response);
  26. } catch (Exception e) {
  27. e.printStackTrace();
  28. }
  29. }
  1. <%@ page language="java" contentType="text/html; charset=utf-8"
  2. pageEncoding="utf-8"%>
  3. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
  4. <%
  5. String path = request.getContextPath();
  6. String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
  7. + path + "/";
  8. %>
  9. <jsp:include page="/WEB-INF/common/form_header.jsp"/>
  10. <body>
  11. <div class="layui-col-md12">
  12. <div class="layui-card">
  13. <div class="layui-card-header">学生列表</div>
  14. <div class="layui-card-body">
  15. <form action="StudentServlet?action=studentListLike" method="post">
  16. <div class="layui-form-item">
  17. <div class="layui-inline">
  18. <div class="layui-input-inline">
  19. <input type="text" placeholder="根据入学年份查询" name="time" id ="time" class="layui-input" />
  20. </div>
  21. <div class="layui-input-inline">
  22. <input type="text" placeholder="根据入学生姓名查询" name="names" id ="names" class="layui-input" />
  23. </div>
  24. </div>
  25. <div class="layui-inline">
  26. <button type="submit" class="layui-btn layui-btn-sm layui-btn-normal">查询</button>
  27. <a href="StudentServlet?action=toAddStudent"
  28. class="layui-btn layui-btn-sm layui-btn-normal">新增学生</a>
  29. </div>
  30. </div>
  31. <%-- </c:if> --%>
  32. </form>
  33. <table class="layui-table">
  34. <thead>
  35. <tr>
  36. <th>姓名</th>
  37. <th>性别</th>
  38. <th>班级</th>
  39. <th>入学时间</th>
  40. <th>学院</th>
  41. <!-- <th>能否借阅书籍</th> -->
  42. <th>手机号</th>
  43. <th>密码</th>
  44. <th>学号</th>
  45. <c:if test="${flag == 1}">
  46. <th>操作</th>
  47. </c:if>
  48. </tr>
  49. </thead>
  50. <tbody>
  51. <c:forEach items="${list}" var="u">
  52. <tr>
  53. <td>${u.name }</td>
  54. <td>${u.sex }</td>
  55. <td>${u.classes }</td>
  56. <td>${u.time }</td>
  57. <td>${u.xy }</td>
  58. <%-- <td>
  59. <c:if test="${u.state == '1'}">
  60. <span style="color:green"></span>
  61. </c:if>
  62. <c:if test="${u.state == '2'}">
  63. <span style="color:red">不能</span>
  64. </c:if>
  65. </td> --%>
  66. <td>${u.phone }</td>
  67. <td>${u.pwd }</td>
  68. <td>${u.stno }</td>
  69. <c:if test="${flag == 1}">
  70. <td>
  71. <input id ="${u.id}" value="${u.id}" type="hidden" class="weui-input"/>
  72. <a href="StudentServlet?action=toUpdateStudent&id=${u.id}" id="update"
  73. class="layui-btn layui-btn-sm layui-btn-norma"><i
  74. class="layui-icon">修改</i></a>
  75. <a href='javascript:void(0)' class="layui-btn layui-btn-sm layui-btn-danger"><i
  76. class="layui-icon">删除</i></a>
  77. <%-- <a href="StudentServlet?action=deleteStudent&id=${u.id}" id="delete"
  78. class="layui-btn layui-btn-sm layui-btn-danger"><i
  79. class="layui-icon">删除</i></a> --%>
  80. </td>
  81. </c:if>
  82. </tr>
  83. </c:forEach>
  84. </tbody>
  85. </table>
  86. <div style="text-align: center">
  87. <div class="layui-btn-group">
  88. <a class="layui-btn layui-btn-primary"
  89. href="StudentServlet?action=studentList&p=1">首页</a>
  90. <%-- 判断是否有上一页--%>
  91. <c:if test="${cp>1}">
  92. <a class="layui-btn layui-btn-primary"
  93. href="StudentServlet?action=studentList&p=${cp-1}">上一页</a>
  94. </c:if>
  95. <%-- 循环显示页码--%>
  96. <c:forEach begin="${cp-2>1 ? (cp-2) :1}"
  97. end="${cp+2>tp?tp:(cp+2)}" var="e">
  98. <%-- 判断是否是当前页--%>
  99. <c:if test="${cp==e}">
  100. <a class="layui-btn layui-btn-danger"
  101. href="StudentServlet?action=studentList&p=${e}">${e}</a>
  102. </c:if>
  103. <c:if test="${cp!=e}">
  104. <a class="layui-btn layui-btn-primary"
  105. href="StudentServlet?action=studentList&p=${e}">${e}</a>
  106. </c:if>
  107. </c:forEach>
  108. <%-- 判断是否有下一页--%>
  109. <c:if test="${cp<tp}">
  110. <a class="layui-btn layui-btn-primary"
  111. href="StudentServlet?action=studentList&p=${cp+1}">下一页</a>
  112. </c:if>
  113. <a class="layui-btn layui-btn-primary"
  114. href="StudentServlet?action=studentList&p=${tp}">尾页</a> <a
  115. clss="layui-btn" href="StudentServlet?action=studentList&p=2">2</a>
  116. <a clss="layui-btn" href="StudentServlet?action=studentList&p=3">3</a>
  117. <a clss="layui-btn" href="StudentServlet?action=studentList&p=4">4</a>
  118. </div>
  119. </div>
  120. </div>
  121. </div>
  122. </div>
  123. <script src="layui/layui.js"></script>
  124. <script src="layui/jquery-1.9.1.min.js"></script>
  125. <script type="application/javascript">
  126. layui.use([ 'form','jquery','layer','laydate' ], function() {
  127. var form = layui.form,
  128. layer = layui.layer,
  129. laydate=layui.laydate,
  130. $= layui.jquery;
  131. form.render();//这句一定要加,占坑
  132. $('a.layui-btn-danger').click(function(e){
  133.             var id = $(e.currentTarget).parent().find('.weui-input').attr('id');
  134. if(confirm("确定要删除吗?")){
  135. $.post("StudentServlet?action=deleteStudent&id="+id, function(data) {
  136. layer.msg('删除成功!',{icon:6,offset:"auto",time:2000},function(){
  137. document.location.reload();//当前页面
  138. });
  139. });
  140. }
  141. })
  142. laydate.render({
  143. elem: '#time'
  144. ,type: 'year'
  145. });
  146. });
  147. </script>
  148. </body>
  149. </html>

新增学生代码

点击新增按钮跳转addStudent.jsp页面,用户填写相关信息后提交代码到studentservlet里面,进行插入操作后返回页面,遍历展示信息

  1. <%@ page language="java" contentType="text/html; charset=utf-8"
  2. pageEncoding="utf-8"%>
  3. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
  4. <%
  5. String path = request.getContextPath();
  6. String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
  7. + path + "/";
  8. %>
  9. <jsp:include page="/WEB-INF/common/form_header.jsp"/>
  10. <body>
  11. <div class="layui-fluid">
  12. <div class="layui-row layui-col-space15">
  13. <div class="layui-col-md12">
  14. <div class="layui-card">
  15. <div class="layui-card-header">新增学生</div>
  16. <div class="layui-card-body">
  17. <form action="StudentServlet?action=addStudent" method="post" id="registerForm" name="registerForm" >
  18. <div class="layui-form-item">
  19. <label class="layui-form-label">姓名:</label>
  20. <div class="layui-input-inline">
  21. <input name="name" id="name" class="layui-input" />
  22. <span id="msg3" style="color:red">${message}</span>
  23. </div>
  24. </div>
  25. <div class="layui-form-item">
  26. <label class="layui-form-label">学号:</label>
  27. <div class="layui-input-inline">
  28. <input name="stno" id="stno" class="layui-input" />
  29. </div>
  30. </div>
  31. <div class="layui-form-item">
  32. <label class="layui-form-label">性别:</label>
  33. <select name="sex" id ="sex" style="width:13%;height:38px">
  34. <option value="">请选择性别</option>
  35. <option value="男"></option>
  36. <option value="女"></option>
  37. </select>
  38. </div>
  39. <div class="layui-form-item">
  40. <label class="layui-form-label">班级:</label>
  41. <div class="layui-input-inline">
  42. <input name="classes" id="classes" class="layui-input" />
  43. </div>
  44. </div>
  45. <div class="layui-form-item">
  46. <label class="layui-form-label">入学时间:</label>
  47. <div class="layui-input-inline">
  48. <input name="time" id="time" class="layui-input" />
  49. </div>
  50. </div>
  51. <div class="layui-form-item">
  52. <label class="layui-form-label">手机号:</label>
  53. <div class="layui-input-inline">
  54. <input name="phone" id="phone" class="layui-input" />
  55. </div>
  56. </div>
  57. <div class="layui-form-item">
  58. <label class="layui-form-label">登录密码:</label>
  59. <div class="layui-input-inline">
  60. <input name="pwd" id="pwd" class="layui-input" />
  61. </div>
  62. </div>
  63. <div class="layui-form-item">
  64. <label class="layui-form-label">学院:</label>
  65. <div class="layui-input-inline">
  66. <input name="xy" id="xy" class="layui-input" />
  67. </div>
  68. </div>
  69. <br>
  70. <div class="layui-form-item">
  71. <div class="layui-input-block">
  72. <button class="layui-btn" type="submit" id="sub_btn">提交</button>
  73. <a href="StudentServlet?action=studentList" class="layui-btn layui-btn-primary" >返回</a>
  74. </div>
  75. </div>
  76. </form>
  77. </div>
  78. </div>
  79. </div>
  80. </div>
  81. </div>
  82. <script src="layui/layui.js"></script>
  83. <script src="layui/jquery-1.9.1.min.js"></script>
  84. <script>
  85. layui.use([ 'form','jquery','layer','laydate' ], function() {
  86. var form = layui.form,
  87. layer = layui.layer,
  88. laydate=layui.laydate,
  89. $= layui.jquery;
  90. form.render();//这句一定要加,占坑
  91. laydate.render({
  92. elem: '#time'
  93. });
  94. $(function () {
  95. // 给注册绑定单击事件i
  96. $("#sub_btn").click(function () {
  97. var name = $("#name").val();
  98. var sex = $("#sex").val();
  99. var classes = $("#classes").val();
  100. var time = $("#time").val();
  101. var xy = $("#xy").val();
  102. var phone = $("#phone").val();
  103. var pwd = $("#pwd").val();
  104. var stno = $("#stno").val();
  105. if(sex == null || sex== ""){
  106. layer.msg('性别不能为空!');
  107. return false;
  108. }
  109. if(name == null || name== ""){
  110. layer.msg('姓名不能为空!');
  111. return false;
  112. }
  113. if(classes == null || classes== ""){
  114. layer.msg('班级不能为空!');
  115. return false;
  116. }
  117. if(time == null || time== ""){
  118. layer.msg('入学时间不能为空!');
  119. return false;
  120. }
  121. if(xy == null || xy== ""){
  122. layer.msg('学院不能为空!');
  123. return false;
  124. }
  125. if(phone == null || phone== ""){
  126. layer.msg('手机号不能为空!');
  127. return false;
  128. }
  129. if(pwd == null || pwd== ""){
  130. layer.msg('密码不能为空!');
  131. return false;
  132. }
  133. if(stno == null || stno== ""){
  134. layer.msg('学号不能为空!');
  135. return false;
  136. }
  137. });
  138. setTimeout(function(){ $("#errorMsg").html("")},3000);
  139. });
  140. setTimeout(function(){ $("#msg3").html("")},3000);
  141. });
  142. </script>
  143. </body>
  144. </html>
  1. /**
  2. * 新增学生信息
  3. * @param request
  4. * @param response
  5. */
  6. private void addStudent(HttpServletRequest request,
  7. HttpServletResponse response) {
  8. String te = request.getParameter("te");
  9. String name = request.getParameter("name");
  10. String sex = request.getParameter("sex");
  11. String classes = request.getParameter("classes");
  12. String time = request.getParameter("time");
  13. String xy = request.getParameter("xy");
  14. String phone = request.getParameter("phone");
  15. String pwd = request.getParameter("pwd");
  16. String stno = request.getParameter("stno");
  17. Student stu = new Student();
  18. stu.setClasses(classes);
  19. stu.setName(name);
  20. stu.setPhone(phone);
  21. stu.setPwd(pwd);
  22. stu.setSex(sex);
  23. stu.setState("1");
  24. stu.setStno(stno);
  25. stu.setTime(time);
  26. stu.setXy(xy);
  27. Student student = ss.selectStudentByPhone(phone);
  28. Student student1 = ss.selectStudentByStuNo(stno);
  29. if(te == null || te.equals("")){
  30. if(student != null){
  31. request.setAttribute("message", "电话号码重复!");
  32. try {
  33. request.getRequestDispatcher("/WEB-INF/views/student/addStudent.jsp").forward(request,response);
  34. } catch (Exception e) {
  35. e.printStackTrace();
  36. }
  37. }else if(student1 != null){
  38. request.setAttribute("message", "学号重复!");
  39. try {
  40. request.getRequestDispatcher("/WEB-INF/views/student/addStudent.jsp").forward(request,response);
  41. } catch (Exception e) {
  42. e.printStackTrace();
  43. }
  44. }else{
  45. try {
  46. ss.saveStudent(stu);
  47. response.sendRedirect(request.getContextPath()+"/StudentServlet?action=studentList");
  48. } catch (Exception e) {
  49. e.printStackTrace();
  50. }
  51. }
  52. }else{
  53. if(student != null){
  54. request.setAttribute("message", "电话号码重复!");
  55. try {
  56. request.getRequestDispatcher("/WEB-INF/views/register.jsp").forward(request,response);
  57. } catch (Exception e) {
  58. e.printStackTrace();
  59. }
  60. }else if(student1 != null){
  61. request.setAttribute("message", "学号重复!");
  62. try {
  63. request.getRequestDispatcher("/WEB-INF/views/register.jsp").forward(request,response);
  64. } catch (Exception e) {
  65. e.printStackTrace();
  66. }
  67. }else{
  68. try {
  69. ss.saveStudent(stu);
  70. response.sendRedirect(request.getContextPath()+"/LoginServlet?action=toLogin");
  71. } catch (Exception e) {
  72. e.printStackTrace();
  73. }
  74. }
  75. }
  76. }

修改学生代码

页面点击修改按钮,跳转到修改的页面,用户修改制定内容后提交代码,执行action里面的servlet路径,serlvet执行相关代码后,返回到前端展示数据!!!!!

  1. <%@ page language="java" contentType="text/html; charset=utf-8"
  2. pageEncoding="utf-8"%>
  3. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
  4. <%
  5. String path = request.getContextPath();
  6. String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
  7. + path + "/";
  8. %>
  9. <jsp:include page="/WEB-INF/common/form_header.jsp"/>
  10. <body>
  11. <div class="layui-fluid">
  12. <div class="layui-row layui-col-space15">
  13. <div class="layui-col-md12">
  14. <div class="layui-card">
  15. <div class="layui-card-header">修改学生</div>
  16. <div class="layui-card-body">
  17. <form action="StudentServlet?action=updateStudent" method="post" id="registerForm" name="registerForm" >
  18. <input name="sid" id="sid" value="${student.id }" type="hidden" class="layui-input" />
  19. <div class="layui-form-item">
  20. <label class="layui-form-label">姓名:</label>
  21. <div class="layui-input-inline">
  22. <input name="name" id="name" value="${student.name }" class="layui-input" />
  23. <span id="msg3" style="color:red">${message}</span>
  24. </div>
  25. </div>
  26. <div class="layui-form-item">
  27. <label class="layui-form-label">学号:</label>
  28. <div class="layui-input-inline">
  29. <input name="stno" id="stno" value="${student.stno }" class="layui-input" />
  30. </div>
  31. </div>
  32. <div class="layui-form-item">
  33. <label class="layui-form-label">性别:</label>
  34. <select name="sex" id ="sex" style="width:13%;height:38px">
  35. <option value="">请选择性别</option>
  36. <c:if test="${student.sex == '男' }">
  37. <option value="男" selected></option>
  38. <option value="女"></option>
  39. </c:if>
  40. <c:if test="${student.sex == '女' }">
  41. <option value="男" ></option>
  42. <option value="女" selected></option>
  43. </c:if>
  44. </select>
  45. </div>
  46. <div class="layui-form-item">
  47. <label class="layui-form-label">班级:</label>
  48. <div class="layui-input-inline">
  49. <input name="classes" id="classes" value="${student.classes }" class="layui-input" />
  50. </div>
  51. </div>
  52. <div class="layui-form-item">
  53. <label class="layui-form-label">学院:</label>
  54. <div class="layui-input-inline">
  55. <input name="xy" id="xy" value="${student.xy }" class="layui-input" />
  56. </div>
  57. </div>
  58. <div class="layui-form-item">
  59. <label class="layui-form-label">入学时间:</label>
  60. <div class="layui-input-inline">
  61. <input name="time" id="time" value="${student.time }" class="layui-input" />
  62. </div>
  63. </div>
  64. <div class="layui-form-item">
  65. <label class="layui-form-label">手机号:</label>
  66. <div class="layui-input-inline">
  67. <input name="phone" id="phone" value="${student.phone }" class="layui-input" />
  68. </div>
  69. </div>
  70. <div class="layui-form-item">
  71. <label class="layui-form-label">登录密码:</label>
  72. <div class="layui-input-inline">
  73. <input name="pwd" id="pwd" value="${student.pwd }" class="layui-input" />
  74. </div>
  75. </div>
  76. <br>
  77. <div class="layui-form-item">
  78. <div class="layui-input-block">
  79. <button class="layui-btn" type="submit" id="sub_btn">提交</button>
  80. <c:if test="${flag == 1 }">
  81. <a href="StudentServlet?action=studentList" class="layui-btn layui-btn-primary" >返回</a>
  82. </c:if>
  83. <c:if test="${flag == 2 }">
  84. <a href="StudentServlet?action=toUpdateStudent&id=${student.id }" class="layui-btn layui-btn-primary" >重置</a>
  85. </c:if>
  86. </div>
  87. </div>
  88. </form>
  89. </div>
  90. </div>
  91. </div>
  92. </div>
  93. </div>
  94. <script src="layui/layui.js"></script>
  95. <script src="layui/jquery-1.9.1.min.js"></script>
  96. <script>
  97. layui.use([ 'form','jquery','layer','laydate' ], function() {
  98. var form = layui.form,
  99. layer = layui.layer,
  100. laydate=layui.laydate,
  101. $= layui.jquery;
  102. form.render();//这句一定要加,占坑
  103. laydate.render({
  104. elem: '#time'
  105. });
  106. $(function () {
  107. // 给注册绑定单击事件
  108. $("#sub_btn").click(function () {
  109. var name = $("#name").val();
  110. var sex = $("#sex").val();
  111. var classes = $("#classes").val();
  112. var time = $("#time").val();
  113. var xy = $("#xy").val();
  114. var phone = $("#phone").val();
  115. var pwd = $("#pwd").val();
  116. var stno = $("#stno").val();
  117. if(sex == null || sex== ""){
  118. layer.msg('性别不能为空!');
  119. return false;
  120. }
  121. if(name == null || name== ""){
  122. layer.msg('姓名不能为空!');
  123. return false;
  124. }
  125. if(classes == null || classes== ""){
  126. layer.msg('班级不能为空!');
  127. return false;
  128. }
  129. if(time == null || time== ""){
  130. layer.msg('入学时间不能为空!');
  131. return false;
  132. }
  133. if(xy == null || xy== ""){
  134. layer.msg('学院不能为空!');
  135. return false;
  136. }
  137. if(phone == null || phone== ""){
  138. layer.msg('手机号不能为空!');
  139. return false;
  140. }
  141. if(pwd == null || pwd== ""){
  142. layer.msg('密码不能为空!');
  143. return false;
  144. }
  145. if(stno == null || stno== ""){
  146. layer.msg('学号不能为空!');
  147. return false;
  148. }
  149. });
  150. setTimeout(function(){ $("#errorMsg").html("")},3000);
  151. });
  152. setTimeout(function(){ $("#msg3").html("")},3000);
  153. });
  154. </script>
  155. </body>
  156. </html>
  1. /**
  2. * 修改学生信息
  3. * @param request
  4. * @param response
  5. */
  6. private void updateStudent(HttpServletRequest request,
  7. HttpServletResponse response) {
  8. Object flag = request.getSession().getAttribute("flag");
  9. String sid = request.getParameter("sid");
  10. String name = request.getParameter("name");
  11. String sex = request.getParameter("sex");
  12. String classes = request.getParameter("classes");
  13. String time = request.getParameter("time");
  14. String xy = request.getParameter("xy");
  15. String phone = request.getParameter("phone");
  16. String pwd = request.getParameter("pwd");
  17. String stno = request.getParameter("stno");
  18. Student stu = new Student();
  19. stu.setClasses(classes);
  20. stu.setName(name);
  21. stu.setPhone(phone);
  22. stu.setPwd(pwd);
  23. stu.setSex(sex);
  24. stu.setStno(stno);
  25. stu.setTime(time);
  26. stu.setXy(xy);
  27. stu.setId(Integer.parseInt(sid));
  28. try {
  29. ss.updateStudent(stu);
  30. if(flag != null && flag.toString().equals("1")){
  31. response.sendRedirect(request.getContextPath()+"/StudentServlet?action=studentList");
  32. }else{
  33. response.sendRedirect(request.getContextPath()+"/StudentServlet?action=toUpdateStudent&id="+sid);
  34. }
  35. } catch (Exception e) {
  36. e.printStackTrace();
  37. }
  38. }

其他模块代码都是类似的,此项目适合初学者学习借鉴,项目整体比较简单!!!!!!!

喜欢的朋友的点赞加关注,感兴趣的同学可以研究!!!!!

感谢  = v =

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/酷酷是懒虫/article/detail/734812
推荐阅读
相关标签
  

闽ICP备14008679号