1. 程式人生 > >關於石家莊鐵道大學課程資訊管理系統詳細製作過程

關於石家莊鐵道大學課程資訊管理系統詳細製作過程

 

 

1.環境的配置:eclipse+tomcat+MySQL+Navicat Premium

2.在Navicat Premium中新建db_book資料庫,其中新建tb_books資料表。包含四個欄位:id、name、teacher、workplace,將id設為自動遞增,否則後面遞增會出錯。

3.新建web專案,點選兩次next,勾選自動生成web.xml。

生成的專案結構如下:

 

 將連線mysql的驅動jar包(mysql-connector-java-8.0.13-bin.jar)複製到WEB-INF下的lib目錄下,直接拖拽即可。

一:首先連線資料庫,建立連線

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(); } } } }

  二:課程Dao層對於資料庫進行的增刪改查操作

package com.hjf.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 com.hjf.entity.Course;
import com.hjf.util.DBUtil;

/**
 * 課程Dao
 * Dao層操作資料
 * @author Hu
 *
 */
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 1=1 ";
		if (name != "") {
			sql += "and name like '%" + name + "%'";
		}
		if (teacher != "") {
			sql += "and teacher like '%" + teacher + "%'";
		}
		if (classroom != "") {
			sql += "and 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;
	}

}

  三:定義資料庫中的變數,私有變數,公有函式

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;
	}
}

  

四:編寫service層,通過連結可以呼叫增刪改查的函式

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();
	}
}

  五:編寫servlet,在服務介面可以進行方法選擇

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);
	}
}

  六:編寫Web主介面add.jsp裡面可以輸入教師名字,上課地點,課程名稱。同時可以進行判斷是否輸入正確

 

<%@ 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" onsubmit="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;
			}

  七:web課程刪除介面del.jsp

<%@ 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" onsubmit="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>

  八:jsp檔案有各種介面和功能

detail.jsp

<%@ 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 onclick="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>

  detail2.jsp

<%@ 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" onsubmit="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">修&nbsp;&nbsp;&nbsp;改</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>

index.jsp//索引

<%@ 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>

list.jsp

<%@ 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>

search.jsp

<%@ 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" onsubmit="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">查&nbsp;&nbsp;&nbsp;詢</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>

searchlist.jsp

<%@ 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>
    <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>
            </tr>
            <!-- forEach遍歷出adminBeans -->
            <c:forEach items="${courses}" var="item" varStatus="status">
                <tr>
                    <td>${item.id}</td>
                    <td><a>${item.name}</a></td>
                    <td>${item.teacher}</td>
                    <td>${item.classroom}</td>
                </tr>
            </c:forEach>
        </table>
    </div>
</body>
</html>