首先我们先建立一个动态的WEB程序,在其中的Java Resources中建立我们所用到的包,dao包用来实现我们的数据库的各种功能的本型,增加删除查找。下面是我们实现各种能的各种代码。entity包里面我对数据库所用到的数据做一个封装,实现代码的简洁性。service包里面实现数据库的操作功能。servlet中用来连接我们的web界面。util中用来连接数据库。这些内部操作的函数就做完了。下面我们解释一下webcontent里面的代码add实现的是增加,del实现的是删除,detail实现的是我们删除的处理界面(比如选择删除弹出是否要删除这个数据),detail2实现的是错误删除的界面,index实现的是我们首页的操作,list实现的查询后的信息现实,search实现的是查询功能searchlist实现的出查询后信息的显示。
1 package com.hjf.dao; 2 3 import java.sql.Connection; 4 import java.sql.ResultSet; 5 import java.sql.SQLException; 6 import java.sql.Statement; 7 import java.util.ArrayList; 8 import java.util.List; 9 10 import com.hjf.entity.Course; 11 import com.hjf.util.DBUtil; 12 13 /** 14 * 课程Dao 15 * Dao层操作数据 16 * @author Hu 17 * 18 */ 19 public class CourseDao { 20 21 /** 22 * 添加 23 * @param course 24 * @return 25 */ 26 public boolean add(Course course) { 27 String sql = "insert into course(name, teacher, classroom) values('" + course.getName() + "','" + course.getTeacher() + "','" + course.getClassroom() + "')"; 28 Connection conn = DBUtil.getConn(); 29 Statement state = null; 30 boolean f = false; 31 int a = 0; 32 33 try { 34 state = conn.createStatement(); 35 state.executeUpdate(sql); 36 } catch (Exception e) { 37 e.printStackTrace(); 38 } finally { 39 DBUtil.close(state, conn); 40 } 41 42 if (a > 0) { 43 f = true; 44 } 45 return f; 46 } 47 48 /** 49 * 删除 50 * 51 * @param id 52 * @return 53 */ 54 public boolean delete (int id) { 55 boolean f = false; 56 String sql = "delete from course where id='" + id + "'"; 57 Connection conn = DBUtil.getConn(); 58 Statement state = null; 59 int a = 0; 60 61 try { 62 state = conn.createStatement(); 63 a = state.executeUpdate(sql); 64 } catch (SQLException e) { 65 e.printStackTrace(); 66 } finally { 67 DBUtil.close(state, conn); 68 } 69 70 if (a > 0) { 71 f = true; 72 } 73 return f; 74 } 75 76 /** 77 * 修改 78 * @param name 79 * @param pass 80 */ 81 public boolean update(Course course) { 82 String sql = "update course set name='" + course.getName() + "', teacher='" + course.getTeacher() + "', classroom='" + course.getClassroom() 83 + "' where id='" + course.getId() + "'"; 84 Connection conn = DBUtil.getConn(); 85 Statement state = null; 86 boolean f = false; 87 int a = 0; 88 89 try { 90 state = conn.createStatement(); 91 a = state.executeUpdate(sql); 92 } catch (SQLException e) { 93 e.printStackTrace(); 94 } finally { 95 DBUtil.close(state, conn); 96 } 97 98 if (a > 0) { 99 f = true; 100 } 101 return f; 102 } 103 104 /** 105 * 验证课程名称是否唯一 106 * true --- 不唯一 107 * @param name 108 * @return 109 */ 110 public boolean name(String name) { 111 boolean flag = false; 112 String sql = "select name from course where name = '" + name + "'"; 113 Connection conn = DBUtil.getConn(); 114 Statement state = null; 115 ResultSet rs = null; 116 117 try { 118 state = conn.createStatement(); 119 rs = state.executeQuery(sql); 120 while (rs.next()) { 121 flag = true; 122 } 123 } catch (SQLException e) { 124 e.printStackTrace(); 125 } finally { 126 DBUtil.close(rs, state, conn); 127 } 128 return flag; 129 } 130 131 /** 132 * 通过ID得到类 133 * @param id 134 * @return 135 */ 136 public Course getCourseById(int id) { 137 String sql = "select * from course where id ='" + id + "'"; 138 Connection conn = DBUtil.getConn(); 139 Statement state = null; 140 ResultSet rs = null; 141 Course course = null; 142 143 try { 144 state = conn.createStatement(); 145 rs = state.executeQuery(sql); 146 while (rs.next()) { 147 String name = rs.getString("name"); 148 String teacher = rs.getString("teacher"); 149 String classroom = rs.getString("classroom"); 150 course = new Course(id, name, teacher, classroom); 151 } 152 } catch (Exception e) { 153 e.printStackTrace(); 154 } finally { 155 DBUtil.close(rs, state, conn); 156 } 157 158 return course; 159 } 160 161 /** 162 * 通过name得到Course 163 * @param name 164 * @return 165 */ 166 public Course getCourseByName(String name) { 167 String sql = "select * from course where name ='" + name + "'"; 168 Connection conn = DBUtil.getConn(); 169 Statement state = null; 170 ResultSet rs = null; 171 Course course = null; 172 173 try { 174 state = conn.createStatement(); 175 rs = state.executeQuery(sql); 176 while (rs.next()) { 177 int id = rs.getInt("id"); 178 String teacher = rs.getString("teacher"); 179 String classroom = rs.getString("classroom"); 180 course = new Course(id, name, teacher, classroom); 181 } 182 } catch (Exception e) { 183 e.printStackTrace(); 184 } finally { 185 DBUtil.close(rs, state, conn); 186 } 187 188 return course; 189 } 190 191 /** 192 * 查找 193 * @param name 194 * @param teacher 195 * @param classroom 196 * @return 197 */ 198 public List<Course> search(String name, String teacher, String classroom) { 199 String sql = "select * from course where 1=1 "; 200 if (name != "") { 201 sql += "and name like '%" + name + "%'"; 202 } 203 if (teacher != "") { 204 sql += "and teacher like '%" + teacher + "%'"; 205 } 206 if (classroom != "") { 207 sql += "and classroom like '%" + classroom + "%'"; 208 } 209 List<Course> list = new ArrayList<>(); 210 Connection conn = DBUtil.getConn(); 211 Statement state = null; 212 ResultSet rs = null; 213 214 try { 215 state = conn.createStatement(); 216 rs = state.executeQuery(sql); 217 Course bean = null; 218 while (rs.next()) { 219 int id = rs.getInt("id"); 220 String name2 = rs.getString("name"); 221 String teacher2 = rs.getString("teacher"); 222 String classroom2 = rs.getString("classroom"); 223 bean = new Course(id, name2, teacher2, classroom2); 224 list.add(bean); 225 } 226 } catch (SQLException e) { 227 e.printStackTrace(); 228 } finally { 229 DBUtil.close(rs, state, conn); 230 } 231 232 return list; 233 } 234 235 /** 236 * 全部数据 237 * @param name 238 * @param teacher 239 * @param classroom 240 * @return 241 */ 242 public List<Course> list() { 243 String sql = "select * from course"; 244 List<Course> list = new ArrayList<>(); 245 Connection conn = DBUtil.getConn(); 246 Statement state = null; 247 ResultSet rs = null; 248 249 try { 250 state = conn.createStatement(); 251 rs = state.executeQuery(sql); 252 Course bean = null; 253 while (rs.next()) { 254 int id = rs.getInt("id"); 255 String name2 = rs.getString("name"); 256 String teacher2 = rs.getString("teacher"); 257 String classroom2 = rs.getString("classroom"); 258 bean = new Course(id, name2, teacher2, classroom2); 259 list.add(bean); 260 } 261 } catch (SQLException e) { 262 e.printStackTrace(); 263 } finally { 264 DBUtil.close(rs, state, conn); 265 } 266 267 return list; 268 } 269 270 }
package com.hjf.entity; public class Course { private int id; private String name; private String teacher; private String classroom; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getTeacher() { return teacher; } public void setTeacher(String teacher) { this.teacher = teacher; } public String getClassroom() { return classroom; } public void setClassroom(String classroom) { this.classroom = classroom; } public Course() {} public Course(int id, String name, String teacher, String classroom) { this.id = id; this.name = name; this.teacher = teacher; this.classroom = classroom; } public Course(String name, String teacher, String classroom) { this.name = name; this.teacher = teacher; this.classroom = classroom; } }
package com.hjf.service; import java.util.List; import com.hjf.dao.CourseDao; import com.hjf.entity.Course; /** * CourseService * 服务层 * @author Hu * */ public class CourseService { CourseDao cDao = new CourseDao(); /** * 添加 * @param course * @return */ public boolean add(Course course) { boolean f = false; if(!cDao.name(course.getName())) { cDao.add(course); f = true; } return f; } /** * 删除 */ public void del(int id) { cDao.delete(id); } /** * 修改 * @return */ public void update(Course course) { cDao.update(course); } /** * 通过ID得到一个Course * @return */ public Course getCourseById(int id) { return cDao.getCourseById(id); } /** * 通过Name得到一个Course * @return */ public Course getCourseByName(String name) { return cDao.getCourseByName(name); } /** * 查找 * @return */ public List<Course> search(String name, String teacher, String classroom) { return cDao.search(name, teacher, classroom); } /** * 全部数据 * @return */ public List<Course> list() { return cDao.list(); } }
package com.hjf.servlet; import java.io.IOException; 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 com.hjf.entity.Course; import com.hjf.service.CourseService; @WebServlet("/CourseServlet") public class CourseServlet extends HttpServlet { private static final long serialVersionUID = 1L; CourseService service = new CourseService(); /** * 方法选择 */ protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); String method = req.getParameter("method"); if ("add".equals(method)) { add(req, resp); } else if ("del".equals(method)) { del(req, resp); } else if ("update".equals(method)) { update(req, resp); } else if ("search".equals(method)) { search(req, resp); } else if ("getcoursebyid".equals(method)) { getCourseById(req, resp); } else if ("getcoursebyname".equals(method)) { getCourseByName(req, resp); } else if ("list".equals(method)) { list(req, resp); } } /** * 添加 * @param req * @param resp * @throws IOException * @throws ServletException */ private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { req.setCharacterEncoding("utf-8"); String name = req.getParameter("name"); String teacher = req.getParameter("teacher"); String classroom = req.getParameter("classroom"); Course course = new Course(name, teacher, classroom); //添加后消息显示 if(service.add(course)) { req.setAttribute("message", "添加成功"); req.getRequestDispatcher("add.jsp").forward(req,resp); } else { req.setAttribute("message", "课程名称重复,请重新录入"); req.getRequestDispatcher("add.jsp").forward(req,resp); } } /** * 全部 * @param req * @param resp * @throws ServletException */ private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); List<Course> courses = service.list(); req.setAttribute("courses", courses); req.getRequestDispatcher("list.jsp").forward(req,resp); } /** * 通过ID得到Course * @param req * @param resp * @throws ServletException */ private void getCourseById(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); int id = Integer.parseInt(req.getParameter("id")); Course course = service.getCourseById(id); req.setAttribute("course", course); req.getRequestDispatcher("detail2.jsp").forward(req,resp); } /** * 通过名字查找 * 跳转至删除 * @param req * @param resp * @throws IOException * @throws ServletException */ private void getCourseByName(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); String name = req.getParameter("name"); Course course = service.getCourseByName(name); if(course == null) { req.setAttribute("message", "查无此课程!"); req.getRequestDispatcher("del.jsp").forward(req,resp); } else { req.setAttribute("course", course); req.getRequestDispatcher("detail.jsp").forward(req,resp); } } /** * 删除 * @param req * @param resp * @throws IOException * @throws ServletException */ private void del(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); int id = Integer.parseInt(req.getParameter("id")); service.del(id); req.setAttribute("message", "删除成功!"); req.getRequestDispatcher("del.jsp").forward(req,resp); } /** * 修改 * @param req * @param resp * @throws IOException * @throws ServletException */ private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); int id = Integer.parseInt(req.getParameter("id")); String name = req.getParameter("name"); String teacher = req.getParameter("teacher"); String classroom = req.getParameter("classroom"); Course course = new Course(id, name, teacher, classroom); service.update(course); req.setAttribute("message", "修改成功"); req.getRequestDispatcher("CourseServlet?method=list").forward(req,resp); } /** * 查找 * @param req * @param resp * @throws ServletException */ private void search(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); String name = req.getParameter("name"); String teacher = req.getParameter("teacher"); String classroom = req.getParameter("classroom"); List<Course> courses = service.search(name, teacher, classroom); req.setAttribute("courses", courses); req.getRequestDispatcher("searchlist.jsp").forward(req,resp); } }
package com.hjf.util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; /** * 数据库连接工具 * @author Hu * */ public class DBUtil { public static String db_url = "jdbc:mysql://localhost:3306/course?useSSL=false"; public static String db_user = "root"; public static String db_pass = "root"; public static Connection getConn () { Connection conn = null; try { Class.forName("com.mysql.jdbc.Driver");//加载驱动 conn = DriverManager.getConnection(db_url, db_user, db_pass); } catch (Exception e) { e.printStackTrace(); } return conn; } /** * 关闭连接 * @param state * @param conn */ public static void close (Statement state, Connection conn) { if (state != null) { try { state.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void close (ResultSet rs, Statement state, Connection conn) { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (state != null) { try { state.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <style> .a{ margin-top: 20px; } .b{ font-size: 20px; width: 160px; color: white; background-color: greenyellow; } </style> </head> <body> <% Object message = request.getAttribute("message"); if(message!=null && !"".equals(message)){ %> <script type="text/javascript"> alert("<%=request.getAttribute("message")%>"); </script> <%} %> <div align="center"> <h1 style="color: red;">课程信息录入</h1> <a href="index.jsp">返回主页</a> <form action="CourseServlet?method=add" method="post" οnsubmit="return check()"> <div class="a"> 课程名称<input type="text" id="name" name="name"/> </div> <div class="a"> 任课教师<input type="text" id="teacher" name="teacher" /> </div> <div class="a"> 上课地点<input type="text" id="classroom" name="classroom" /> </div> <div class="a"> <button type="submit" class="b">保 存</button> </div> </form> </div> <script type="text/javascript"> function check() { var name = document.getElementById("name");; var teacher = document.getElementById("teacher"); var classroom = document.getElementById("classroom"); //非空 if(name.value == '') { alert('课程名称为空'); name.focus(); return false; } if(teacher.value == '') { alert('教师为空'); teacher.focus(); return false; } if(classroom.value == '') { alert('上课地点为空'); classroom.focus(); return false; } //教师 if(teacher.value != '王建民' && teacher.value != '王辉' && teacher.value != '刘丹' && teacher.value != '刘立嘉' && teacher.value != '杨子光'){ alert('教师名称错误'); return false; } //教室 if(!/^基教/.test(classroom.value) && !/^一教/.test(classroom.value) && !/^二教/.test(classroom.value) && !/^三教/.test(classroom.value)) { alert('上课地点错误'); return false; }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <style> .a{ margin-top: 20px; } .b{ font-size: 20px; width: 160px; color: white; background-color: greenyellow; } </style> </head> <body> <% Object message = request.getAttribute("message"); if(message!=null && !"".equals(message)){ %> <script type="text/javascript"> alert("<%=request.getAttribute("message")%>"); </script> <%} %> <div align="center"> <h1 style="color: red;">课程信息删除</h1> <a href="index.jsp">返回主页</a> <form action="CourseServlet?method=getcoursebyname" method="post" οnsubmit="return check()"> <div class="a"> 课程名称<input type="text" id="name" name="name"/> </div> <div class="a"> <button type="submit" class="b">查 找</button> </div> </form> </div> <script type="text/javascript"> function check() { var name = document.getElementById("name");; //非空 if(name.value == '') { alert('课程名称为空'); name.focus(); return false; } } </script> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <style> .a{ margin-top: 20px; } .b{ font-size: 20px; width: 160px; color: white; background-color: greenyellow; } </style> </head> <body> <% Object message = request.getAttribute("message"); if(message!=null && !"".equals(message)){ %> <script type="text/javascript"> alert("<%=request.getAttribute("message")%>"); </script> <%} %> <div align="center"> <h1 style="color: red;">课程信息删除</h1> <a href="index.jsp">返回主页</a> <form action="CourseServlet?method=getcoursebyname" method="post" οnsubmit="return check()"> <div class="a"> 课程名称<input type="text" id="name" name="name"/> </div> <div class="a"> <button type="submit" class="b">查 找</button> </div> </form> </div> <script type="text/javascript"> function check() { var name = document.getElementById("name");; //非空 if(name.value == '') { alert('课程名称为空'); name.focus(); return false; } } </script> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <style> .a{ margin-top: 20px; } .b{ font-size: 20px; width: 160px; color: white; background-color: greenyellow; } .tb, td { border: 1px solid black; font-size: 22px; } </style> </head> <body> <div align="center"> <h1 style="color: red;">课程信息删除</h1> <a href="index.jsp">返回主页</a> <table class="tb"> <tr> <td>课程名称</td> <td>${course.name}</td> </tr> <tr> <td>任课教师</td> <td>${course.teacher}</td> </tr> <tr> <td>上课地点</td> <td>${course.classroom}</td> </tr> </table> <div class="a"> <a οnclick="return check()" href="CourseServlet?method=del&id=${course.id}">删 除</a> </div> </div> <script type="text/javascript"> function check() { if (confirm("真的要删除吗?")){ return true; }else{ return false; } } </script> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <style> .a{ margin-top: 20px; } .b{ font-size: 20px; width: 160px; color: white; background-color: greenyellow; } </style> </head> <body> <% Object message = request.getAttribute("message"); if(message!=null && !"".equals(message)){ %> <script type="text/javascript"> alert("<%=request.getAttribute("message")%>"); </script> <%} %> <div align="center"> <h1 style="color: red;">课程信息修改</h1> <a href="index.jsp">返回主页</a> <form action="CourseServlet?method=update" method="post" οnsubmit="return check()"> <div class="a"> 课程名称<input type="text" id="name" name="name" value="${course.name}"/> </div> <div class="a"> 任课教师<input type="text" id="teacher" name="teacher" value="${course.teacher}"/> </div> <div class="a"> 上课地点<input type="text" id="classroom" name="classroom" value="${course.classroom}"/> </div> <input type="hidden" id="id" name="id" value="${course.id}"/> <div class="a"> <button type="submit" class="b">修 改</button> </div> </form> </div> <script type="text/javascript"> function check() { var name = document.getElementById("name");; var teacher = document.getElementById("teacher"); var classroom = document.getElementById("classroom"); //非空 if(name.value == '') { alert('课程名称为空'); name.focus(); return false; } if(teacher.value == '') { alert('教师为空'); teacher.focus(); return false; } if(classroom.value == '') { alert('上课地点为空'); classroom.focus(); return false; } //教师 if(teacher.value != '王建民' && teacher.value != '王辉' && teacher.value != '刘丹' && teacher.value != '刘立嘉' && teacher.value != '杨子光'){ alert('教师名称错误'); return false; } //教室 if(!/^基教/.test(classroom.value) && !/^一教/.test(classroom.value) && !/^二教/.test(classroom.value) && !/^三教/.test(classroom.value)) { alert('上课地点错误'); return false; } } </script> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>首页</title> <style> .a{ font-size: 26px; margin-top: 20px; } </style> </head> <body> <div align="center"> <h1 style="color: red;">课程基本信息管理系统</h1> <div class="a"> <a href="add.jsp">课程信息录入</a> </div> <div class="a"> <a href="CourseServlet?method=list">课程信息修改</a> </div> <div class="a"> <a href="del.jsp">课程信息删除</a> </div> <div class="a"> <a href="search.jsp">课程信息查询</a> </div> </div> </body> </html>
<%@ 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"> <title>Insert title here</title> <style> .a{ margin-top: 20px; } .b{ font-size: 20px; width: 160px; color: white; background-color: greenyellow; } .tb, td { border: 1px solid black; font-size: 22px; } </style> </head> <body> <% Object message = request.getAttribute("message"); if(message!=null && !"".equals(message)){ %> <script type="text/javascript"> alert("<%=request.getAttribute("message")%>"); </script> <%} %> <div align="center"> <h1 style="color: red;">课程信息列表</h1> <a href="index.jsp">返回主页</a> <table class="tb"> <tr> <td>id</td> <td>课程名称</td> <td>任课教师</td> <td>上课地点</td> <td align="center" colspan="2">操作</td> </tr> <c:forEach items="${courses}" var="item"> <tr> <td>${item.id}</td> <td>${item.name}</td> <td>${item.teacher}</td> <td>${item.classroom}</td> <td><a href="CourseServlet?method=getcoursebyid&id=${item.id}">修改</a></td> </tr> </c:forEach> </table> </div> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <style> .a{ margin-top: 20px; } .b{ font-size: 20px; width: 160px; color: white; background-color: greenyellow; } </style> </head> <body> <div align="center"> <h1 style="color: red;">课程信息查询</h1> <a href="index.jsp">返回主页</a> <form action="CourseServlet?method=search" method="post" οnsubmit="return check()"> <div class="a"> 课程名称<input type="text" id="name" name="name"/> </div> <div class="a"> 任课教师<input type="text" id="teacher" name="teacher" /> </div> <div class="a"> 上课地点<input type="text" id="classroom" name="classroom" /> </div> <div class="a"> <button type="submit" class="b">查 询</button> </div> </form> </div> <script type="text/javascript"> function check() { var name = document.getElementById("name");; var teacher = document.getElementById("teacher"); var classroom = document.getElementById("classroom"); //非空 if(name.value == '' && teacher.value == '' && classroom.value == '') { alert('请填写一个条件'); return false; } } </script> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <style> .a{ margin-top: 20px; } .b{ font-size: 20px; width: 160px; color: white; background-color: greenyellow; } </style> </head> <body> <div align="center"> <h1 style="color: red;">课程信息查询</h1> <a href="index.jsp">返回主页</a> <form action="CourseServlet?method=search" method="post" οnsubmit="return check()"> <div class="a"> 课程名称<input type="text" id="name" name="name"/> </div> <div class="a"> 任课教师<input type="text" id="teacher" name="teacher" /> </div> <div class="a"> 上课地点<input type="text" id="classroom" name="classroom" /> </div> <div class="a"> <button type="submit" class="b">查 询</button> </div> </form> </div> <script type="text/javascript"> function check() { var name = document.getElementById("name");; var teacher = document.getElementById("teacher"); var classroom = document.getElementById("classroom"); //非空 if(name.value == '' && teacher.value == '' && classroom.value == '') { alert('请填写一个条件'); return false; } } </script> </body> </html>