课程信息管理系统
2018-12-09 21:20 一念永恒s 阅读(309) 评论(0) 编辑 收藏 举报
1系统的设计分为两部分,一部分是Javabean,一部分是jsp界面的设计
javabean
CourseDao.java(数据库和daos交互)
package dao; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import entity.Course; import util.DBUtil; /** * 课程Dao * Dao层操作数据 * * */ public class CourseDao { /** * 添加 * @param course * @return */ public boolean add(Course course) { String sql = "insert into course(name, teacher, classroom) values('" + course.getName() + "','" + course.getTeacher() + "','" + course.getClassroom() + "')"; Connection conn = DBUtil.getConn(); Statement state = null; boolean f = false; int a = 0; try { state = conn.createStatement(); state.executeUpdate(sql); } catch (Exception e) { e.printStackTrace(); } finally { DBUtil.close(state, conn); } if (a > 0) { f = true; } return f; } /** * 删除 * * @param id * @return */ public boolean delete (int id) { boolean f = false; String sql = "delete from course where id='" + id + "'"; Connection conn = DBUtil.getConn(); Statement state = null; int a = 0; try { state = conn.createStatement(); a = state.executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(state, conn); } if (a > 0) { f = true; } return f; } /** * 修改 * @param name * @param pass */ public boolean update(Course course) { String sql = "update course set name='" + course.getName() + "', teacher='" + course.getTeacher() + "', classroom='" + course.getClassroom() + "' where id='" + course.getId() + "'"; Connection conn = DBUtil.getConn(); Statement state = null; boolean f = false; int a = 0; try { state = conn.createStatement(); a = state.executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(state, conn); } if (a > 0) { f = true; } return f; } /** * 验证课程名称是否唯一 * true --- 不唯一 * @param name * @return */ public boolean name(String name) { boolean flag = false; String sql = "select name from course where name = '" + name + "'"; Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); while (rs.next()) { flag = true; } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return flag; } /** * 通过ID得到类 * @param id * @return */ public Course getCourseById(int id) { String sql = "select * from course where id ='" + id + "'"; Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; Course course = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); while (rs.next()) { String name = rs.getString("name"); String teacher = rs.getString("teacher"); String classroom = rs.getString("classroom"); course = new Course(id, name, teacher, classroom); } } catch (Exception e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return course; } /** * 通过name得到Course * @param name * @return */ public Course getCourseByName(String name) { String sql = "select * from course where name ='" + name + "'"; Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; Course course = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); while (rs.next()) { int id = rs.getInt("id"); String teacher = rs.getString("teacher"); String classroom = rs.getString("classroom"); course = new Course(id, name, teacher, classroom); } } catch (Exception e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return course; } /** * 查找 * @param name * @param teacher * @param classroom * @return */ public List<Course> search(String name, String teacher, String classroom) { String sql = "select * from course where "; if (name != "") { sql += "name like '%" + name + "%'"; } if (teacher != "") { sql += "teacher like '%" + teacher + "%'"; } if (classroom != "") { sql += "classroom like '%" + classroom + "%'"; } List<Course> list = new ArrayList<>(); Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); Course bean = null; while (rs.next()) { int id = rs.getInt("id"); String name2 = rs.getString("name"); String teacher2 = rs.getString("teacher"); String classroom2 = rs.getString("classroom"); bean = new Course(id, name2, teacher2, classroom2); list.add(bean); } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return list; } /** * 全部数据 * @param name * @param teacher * @param classroom * @return */ public List<Course> list() { String sql = "select * from course"; List<Course> list = new ArrayList<>(); Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); Course bean = null; while (rs.next()) { int id = rs.getInt("id"); String name2 = rs.getString("name"); String teacher2 = rs.getString("teacher"); String classroom2 = rs.getString("classroom"); bean = new Course(id, name2, teacher2, classroom2); list.add(bean); } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return list; } }
Course.java(数据)
1 package entity; 2 3 public class Course { 4 private int id; 5 private String name; 6 private String teacher; 7 private String classroom; 8 9 public int getId() { 10 return id; 11 } 12 public void setId(int id) { 13 this.id = id; 14 } 15 public String getName() { 16 return name; 17 } 18 public void setName(String name) { 19 this.name = name; 20 } 21 public String getTeacher() { 22 return teacher; 23 } 24 public void setTeacher(String teacher) { 25 this.teacher = teacher; 26 } 27 public String getClassroom() { 28 return classroom; 29 } 30 public void setClassroom(String classroom) { 31 this.classroom = classroom; 32 } 33 34 public Course() {} 35 36 public Course(int id, String name, String teacher, String classroom) { 37 this.id = id; 38 this.name = name; 39 this.teacher = teacher; 40 this.classroom = classroom; 41 } 42 43 public Course(String name, String teacher, String classroom) { 44 this.name = name; 45 this.teacher = teacher; 46 this.classroom = classroom; 47 } 48 }
Courseservice.java
1 package service; 2 import java.util.List; 3 import dao.CourseDao; 4 import entity.Course; 5 6 /** 7 * CourseService 8 * 服务层 9 * 10 * 11 */ 12 public class CourseService { 13 CourseDao cDao = new CourseDao(); 14 15 /** 16 * 添加 17 * @param course 18 * @return 19 */ 20 public boolean add(Course course) { 21 boolean f = false; 22 if(!cDao.name(course.getName())) { 23 cDao.add(course); 24 f = true; 25 } 26 return f; 27 } 28 29 /** 30 * 删除 31 */ 32 public void del(int id) { 33 cDao.delete(id); 34 } 35 36 /** 37 * 修改 38 * @return 39 */ 40 public void update(Course course) { 41 cDao.update(course); 42 } 43 44 /** 45 * 通过ID得到一个Course 46 * @return 47 */ 48 public Course getCourseById(int id) { 49 return cDao.getCourseById(id); 50 } 51 52 /** 53 * 通过Name得到一个Course 54 * @return 55 */ 56 public Course getCourseByName(String name) { 57 return cDao.getCourseByName(name); 58 } 59 60 /** 61 * 查找 62 * @return 63 */ 64 public List<Course> search(String name, String teacher, String classroom) { 65 return cDao.search(name, teacher, classroom); 66 } 67 68 /** 69 * 全部数据 70 * @return 71 */ 72 public List<Course> list() { 73 return cDao.list(); 74 } 75 }
Courseservlet.java
1 package servlet; 2 import java.io.IOException; 3 import java.util.List; 4 5 import javax.servlet.ServletException; 6 import javax.servlet.annotation.WebServlet; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 11 import entity.Course; 12 import service.CourseService; 13 14 /** 15 * Servlet implementation class CourseServlet 16 */ 17 @WebServlet("/CourseServlet") 18 public class CourseServlet extends HttpServlet { 19 private static final long serialVersionUID = 1L; 20 CourseService service = new CourseService(); 21 /** 22 * Default constructor. 23 */ 24 /** 25 * 方法选择 26 */ 27 protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 28 req.setCharacterEncoding("utf-8"); 29 String method = req.getParameter("method"); 30 if ("add".equals(method)) { 31 add(req, resp); 32 } else if ("del".equals(method)) { 33 del(req, resp); 34 } else if ("update".equals(method)) { 35 update(req, resp); 36 } else if ("search".equals(method)) { 37 search(req, resp); 38 } else if ("getcoursebyid".equals(method)) { 39 getCourseById(req, resp); 40 } else if ("getcoursebyname".equals(method)) { 41 getCourseByName(req, resp); 42 } else if ("list".equals(method)) { 43 list(req, resp); 44 } 45 } 46 47 /** 48 * 添加 49 * @param req 50 * @param resp 51 * @throws IOException 52 * @throws ServletException 53 */ 54 private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { 55 req.setCharacterEncoding("utf-8"); 56 String name = req.getParameter("name"); 57 String teacher = req.getParameter("teacher"); 58 String classroom = req.getParameter("classroom"); 59 Course course = new Course(name, teacher, classroom); 60 61 //添加后消息显示 62 if(service.add(course)) { 63 req.setAttribute("message", "添加成功"); 64 req.getRequestDispatcher("add.jsp").forward(req,resp); 65 } else { 66 req.setAttribute("message", "课程名称重复,请重新录入"); 67 req.getRequestDispatcher("add.jsp").forward(req,resp); 68 } 69 } 70 71 /** 72 * 全部 73 * @param req 74 * @param resp 75 * @throws ServletException 76 */ 77 private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ 78 req.setCharacterEncoding("utf-8"); 79 List<Course> courses = service.list(); 80 req.setAttribute("courses", courses); 81 req.getRequestDispatcher("list.jsp").forward(req,resp); 82 } 83 84 /** 85 * 通过ID得到Course 86 * @param req 87 * @param resp 88 * @throws ServletException 89 */ 90 private void getCourseById(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ 91 req.setCharacterEncoding("utf-8"); 92 int id = Integer.parseInt(req.getParameter("id")); 93 Course course = service.getCourseById(id); 94 req.setAttribute("course", course); 95 req.getRequestDispatcher("detail2.jsp").forward(req,resp); 96 } 97 98 /** 99 * 通过名字查找 100 * 跳转至删除 101 * @param req 102 * @param resp 103 * @throws IOException 104 * @throws ServletException 105 */ 106 private void getCourseByName(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ 107 req.setCharacterEncoding("utf-8"); 108 String name = req.getParameter("name"); 109 Course course = service.getCourseByName(name); 110 if(course == null) { 111 req.setAttribute("message", "查无此课程!"); 112 req.getRequestDispatcher("del.jsp").forward(req,resp); 113 } else { 114 req.setAttribute("course", course); 115 req.getRequestDispatcher("detail.jsp").forward(req,resp); 116 } 117 } 118 119 /** 120 * 删除 121 * @param req 122 * @param resp 123 * @throws IOException 124 * @throws ServletException 125 */ 126 private void del(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ 127 req.setCharacterEncoding("utf-8"); 128 int id = Integer.parseInt(req.getParameter("id")); 129 service.del(id); 130 req.setAttribute("message", "删除成功!"); 131 req.getRequestDispatcher("del.jsp").forward(req,resp); 132 } 133 134 /** 135 * 修改 136 * @param req 137 * @param resp 138 * @throws IOException 139 * @throws ServletException 140 */ 141 private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ 142 req.setCharacterEncoding("utf-8"); 143 int id = Integer.parseInt(req.getParameter("id")); 144 String name = req.getParameter("name"); 145 String teacher = req.getParameter("teacher"); 146 String classroom = req.getParameter("classroom"); 147 Course course = new Course(id, name, teacher, classroom); 148 149 service.update(course); 150 req.setAttribute("message", "修改成功"); 151 req.getRequestDispatcher("CourseServlet?method=list").forward(req,resp); 152 } 153 154 /** 155 * 查找 156 * @param req 157 * @param resp 158 * @throws ServletException 159 */ 160 private void search(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ 161 req.setCharacterEncoding("utf-8"); 162 String name = req.getParameter("name"); 163 String teacher = req.getParameter("teacher"); 164 String classroom = req.getParameter("classroom"); 165 List<Course> courses = service.search(name, teacher, classroom); 166 req.setAttribute("courses", courses); 167 req.getRequestDispatcher("searchlist.jsp").forward(req,resp); 168 } 169 }
DBUtil.java(连接数据库)
1 package util; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.sql.Statement; 8 9 /** 10 * 数据库连接工具 11 12 * 13 */ 14 public class DBUtil { 15 public static String db_url = "jdbc:mysql://localhost:3306/course?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT"; 16 public static String db_user = "root"; 17 public static String db_pass = "root"; 18 19 public static Connection getConn () { 20 Connection conn = null; 21 22 try { 23 Class.forName("com.mysql.cj.jdbc.Driver");//加载驱动 24 System.out.println("驱动包连接成功"); 25 conn = DriverManager.getConnection(db_url, db_user, db_pass); 26 System.out.println("连接成功"); 27 } catch (Exception e) { 28 System.out.println("连接失败"); 29 e.printStackTrace(); 30 } 31 32 return conn; 33 } 34 35 /** 36 * 关闭连接 37 * @param state 38 * @param conn 39 */ 40 public static void close (Statement state, Connection conn) { 41 if (state != null) { 42 try { 43 state.close(); 44 } catch (SQLException e) { 45 e.printStackTrace(); 46 } 47 } 48 49 if (conn != null) { 50 try { 51 conn.close(); 52 } catch (SQLException e) { 53 e.printStackTrace(); 54 } 55 } 56 } 57 58 public static void close (ResultSet rs, Statement state, Connection conn) { 59 if (rs != null) { 60 try { 61 rs.close(); 62 } catch (SQLException e) { 63 e.printStackTrace(); 64 } 65 } 66 67 if (state != null) { 68 try { 69 state.close(); 70 } catch (SQLException e) { 71 e.printStackTrace(); 72 } 73 } 74 75 if (conn != null) { 76 try { 77 conn.close(); 78 } catch (SQLException e) { 79 e.printStackTrace(); 80 } 81 } 82 } 83 }
jsp
add.jsp(课程信息录入子页)
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title>Insert title here</title> 8 <style> 9 .a{ 10 margin-top: 20px; 11 } 12 .b{ 13 font-size: 20px; 14 width: 160px; 15 color: white; 16 background-color: greenyellow; 17 } 18 </style> 19 </head> 20 <body> 21 <% 22 Object message = request.getAttribute("message"); 23 if(message!=null && !"".equals(message)){ 24 25 %> 26 <script type="text/javascript"> 27 alert("<%=request.getAttribute("message")%>"); 28 </script> 29 <%} %> 30 <div align="center"> 31 <h1 style="color: red;">课程信息录入</h1> 32 <a href="index.jsp">返回主页</a> 33 <form action="CourseServlet?method=add" method="post" onsubmit="return check()"> 34 <div class="a"> 35 课程名称<input type="text" id="name" name="name"/> 36 </div> 37 <div class="a"> 38 任课教师<input type="text" id="teacher" name="teacher" /> 39 </div> 40 <div class="a"> 41 上课地点<input type="text" id="classroom" name="classroom" /> 42 </div> 43 <div class="a"> 44 <button type="submit" class="b">保 存</button> 45 </div> 46 </form> 47 </div> 48 <script type="text/javascript"> 49 function check() { 50 var name = document.getElementById("name");; 51 var teacher = document.getElementById("teacher"); 52 var classroom = document.getElementById("classroom"); 53 54 //非空 55 if(name.value == '') { 56 alert('课程名称为空'); 57 name.focus(); 58 return false; 59 } 60 if(teacher.value == '') { 61 alert('教师为空'); 62 teacher.focus(); 63 return false; 64 } 65 if(classroom.value == '') { 66 alert('上课地点为空'); 67 classroom.focus(); 68 return false; 69 } 70 71 //教师 72 if(teacher.value != '王建民' && teacher.value != '王辉' && teacher.value != '刘丹' && teacher.value != '刘立嘉' && teacher.value != '杨子光'){ 73 alert('教师名称错误'); 74 return false; 75 } 76 77 //教室 78 if(!/^基教/.test(classroom.value) && !/^一教/.test(classroom.value) && !/^二教/.test(classroom.value) && !/^三教/.test(classroom.value)) { 79 alert('上课地点错误'); 80 return false; 81 } 82 } 83 </script> 84 </body> 85 </html>
del.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title>Insert title here</title> 8 <style> 9 .a{ 10 margin-top: 20px; 11 } 12 .b{ 13 font-size: 20px; 14 width: 160px; 15 color: white; 16 background-color: greenyellow; 17 } 18 </style> 19 </head> 20 <body> 21 <% 22 Object message = request.getAttribute("message"); 23 if(message!=null && !"".equals(message)){ 24 25 %> 26 <script type="text/javascript"> 27 alert("<%=request.getAttribute("message")%>"); 28 </script> 29 <%} %> 30 <div align="center"> 31 <h1 style="color: red;">课程信息删除</h1> 32 <a href="index.jsp">返回主页</a> 33 <form action="CourseServlet?method=getcoursebyname" method="post" onsubmit="return check()"> 34 <div class="a"> 35 课程名称<input type="text" id="name" name="name"/> 36 </div> 37 <div class="a"> 38 <button type="submit" class="b">查 找</button> 39 </div> 40 </form> 41 </div> 42 <script type="text/javascript"> 43 function check() { 44 var name = document.getElementById("name");; 45 46 //非空 47 if(name.value == '') { 48 alert('课程名称为空'); 49 name.focus(); 50 return false; 51 } 52 } 53 </script> 54 </body> 55 </html>
detail.java(课程信息删除子页)
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title>Insert title here</title> 8 <style> 9 .a{ 10 margin-top: 20px; 11 } 12 .b{ 13 font-size: 20px; 14 width: 160px; 15 color: white; 16 background-color: greenyellow; 17 } 18 .tb, td { 19 border: 1px solid black; 20 font-size: 22px; 21 } 22 </style> 23 </head> 24 <body> 25 <div align="center"> 26 <h1 style="color: red;">课程信息删除</h1> 27 <a href="index.jsp">返回主页</a> 28 <table class="tb"> 29 <tr> 30 <td>课程名称</td> 31 <td>${course.name}</td> 32 </tr> 33 <tr> 34 <td>任课教师</td> 35 <td>${course.teacher}</td> 36 </tr> 37 <tr> 38 <td>上课地点</td> 39 <td>${course.classroom}</td> 40 </tr> 41 </table> 42 <div class="a"> 43 <a onclick="return check()" href="CourseServlet?method=del&id=${course.id}">删 除</a> 44 </div> 45 </div> 46 <script type="text/javascript"> 47 function check() { 48 if (confirm("真的要删除吗?")){ 49 return true; 50 }else{ 51 return false; 52 } 53 } 54 </script> 55 </body> 56 </html>
detail.jsp(课程信息修改子页)
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title>Insert title here</title> 8 <style> 9 .a{ 10 margin-top: 20px; 11 } 12 .b{ 13 font-size: 20px; 14 width: 160px; 15 color: white; 16 background-color: greenyellow; 17 } 18 </style> 19 </head> 20 <body> 21 <% 22 Object message = request.getAttribute("message"); 23 if(message!=null && !"".equals(message)){ 24 25 %> 26 <script type="text/javascript"> 27 alert("<%=request.getAttribute("message")%>"); 28 </script> 29 <%} %> 30 <div align="center"> 31 <h1 style="color: red;">课程信息修改</h1> 32 <a href="index.jsp">返回主页</a> 33 <form action="CourseServlet?method=update" method="post" onsubmit="return check()"> 34 <div class="a"> 35 课程名称<input type="text" id="name" name="name" value="${course.name}"/> 36 </div> 37 <div class="a"> 38 任课教师<input type="text" id="teacher" name="teacher" value="${course.teacher}"/> 39 </div> 40 <div class="a"> 41 上课地点<input type="text" id="classroom" name="classroom" value="${course.classroom}"/> 42 </div> 43 <input type="hidden" id="id" name="id" value="${course.id}"/> 44 <div class="a"> 45 <button type="submit" class="b">修 改</button> 46 </div> 47 </form> 48 </div> 49 <script type="text/javascript"> 50 function check() { 51 var name = document.getElementById("name");; 52 var teacher = document.getElementById("teacher"); 53 var classroom = document.getElementById("classroom"); 54 55 //非空 56 if(name.value == '') { 57 alert('课程名称为空'); 58 name.focus(); 59 return false; 60 } 61 if(teacher.value == '') { 62 alert('教师为空'); 63 teacher.focus(); 64 return false; 65 } 66 if(classroom.value == '') { 67 alert('上课地点为空'); 68 classroom.focus(); 69 return false; 70 } 71 72 //教师 73 if(teacher.value != '王建民' && teacher.value != '王辉' && teacher.value != '刘丹' && teacher.value != '刘立嘉' && teacher.value != '杨子光'){ 74 alert('教师名称错误'); 75 return false; 76 } 77 78 //教室 79 if(!/^基教/.test(classroom.value) && !/^一教/.test(classroom.value) && !/^二教/.test(classroom.value) && !/^三教/.test(classroom.value)) { 80 alert('上课地点错误'); 81 return false; 82 } 83 } 84 </script> 85 </body> 86 </html>
index.jsp(主页杰界面)
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title>首页</title> 8 <style> 9 .a{ 10 font-size: 26px; 11 margin-top: 20px; 12 } 13 </style> 14 </head> 15 <body> 16 <div align="center"> 17 <h1 style="color: red;">课程基本信息管理系统</h1> 18 <div class="a"> 19 <a href="add.jsp">课程信息录入</a> 20 </div> 21 <div class="a"> 22 <a href="CourseServlet?method=list">课程信息修改</a> 23 </div> 24 <div class="a"> 25 <a href="del.jsp">课程信息删除</a> 26 </div> 27 <div class="a"> 28 <a href="search.jsp">课程信息查询</a> 29 </div> 30 </div> 31 </body> 32 </html>
list.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 4 <!DOCTYPE html> 5 <html> 6 <head> 7 <meta charset="UTF-8"> 8 <title>Insert title here</title> 9 <style> 10 .a{ 11 margin-top: 20px; 12 } 13 .b{ 14 font-size: 20px; 15 width: 160px; 16 color: white; 17 background-color: greenyellow; 18 } 19 .tb, td { 20 border: 1px solid black; 21 font-size: 22px; 22 } 23 </style> 24 </head> 25 <body> 26 <% 27 Object message = request.getAttribute("message"); 28 if(message!=null && !"".equals(message)){ 29 30 %> 31 <script type="text/javascript"> 32 alert("<%=request.getAttribute("message")%>"); 33 </script> 34 <%} %> 35 <div align="center"> 36 <h1 style="color: red;">课程信息列表</h1> 37 <a href="index.jsp">返回主页</a> 38 <table class="tb"> 39 <tr> 40 <td>id</td> 41 <td>课程名称</td> 42 <td>任课教师</td> 43 <td>上课地点</td> 44 <td align="center" colspan="2">操作</td> 45 </tr> 46 <c:forEach items="${courses}" var="item"> 47 <tr> 48 <td>${item.id}</td> 49 <td>${item.name}</td> 50 <td>${item.teacher}</td> 51 <td>${item.classroom}</td> 52 <td><a href="CourseServlet?method=getcoursebyid&id=${item.id}">修改</a></td> 53 </tr> 54 </c:forEach> 55 </table> 56 </div> 57 </body> 58 </html>
search.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title>Insert title here</title> 8 <style> 9 .a{ 10 margin-top: 20px; 11 } 12 .b{ 13 font-size: 20px; 14 width: 160px; 15 color: white; 16 background-color: greenyellow; 17 } 18 </style> 19 </head> 20 <body> 21 <div align="center"> 22 <h1 style="color: red;">课程信息查询</h1> 23 <a href="index.jsp">返回主页</a> 24 <form action="CourseServlet?method=search" method="post" onsubmit="return check()"> 25 <div class="a"> 26 课程名称<input type="text" id="name" name="name"/> 27 </div> 28 <div class="a"> 29 任课教师<input type="text" id="teacher" name="teacher" /> 30 </div> 31 <div class="a"> 32 上课地点<input type="text" id="classroom" name="classroom" /> 33 </div> 34 <div class="a"> 35 <button type="submit" class="b">查 询</button> 36 </div> 37 </form> 38 </div> 39 <script type="text/javascript"> 40 function check() { 41 var name = document.getElementById("name");; 42 var teacher = document.getElementById("teacher"); 43 var classroom = document.getElementById("classroom"); 44 45 //非空 46 if(name.value == '' && teacher.value == '' && classroom.value == '') { 47 alert('请填写一个条件'); 48 return false; 49 } 50 } 51 </script> 52 </body> 53 </html>
search.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 4 <!DOCTYPE html> 5 <html> 6 <head> 7 <meta charset="UTF-8"> 8 <title>Insert title here</title> 9 <style> 10 .a{ 11 margin-top: 20px; 12 } 13 .b{ 14 font-size: 20px; 15 width: 160px; 16 color: white; 17 background-color: greenyellow; 18 } 19 .tb, td { 20 border: 1px solid black; 21 font-size: 22px; 22 } 23 </style> 24 </head> 25 <body> 26 <div align="center"> 27 <h1 style="color: red;">课程信息列表</h1> 28 <a href="index.jsp">返回主页</a> 29 <table class="tb"> 30 <tr> 31 <td>id</td> 32 <td>课程名称</td> 33 <td>任课教师</td> 34 <td>上课地点</td> 35 </tr> 36 <!-- forEach遍历出adminBeans --> 37 <c:forEach items="${courses}" var="item" varStatus="status"> 38 <tr> 39 <td>${item.id}</td> 40 <td><a>${item.name}</a></td> 41 <td>${item.teacher}</td> 42 <td>${item.classroom}</td> 43 </tr> 44 </c:forEach> 45 </table> 46 </div> 47 </body> 48 </html>