第82節:Java中的學生管理系統

標題圖
第82節:Java中的學生管理系統
學生管理系統的刪除功能
刪除,點選超連結,點選彈出對話方塊式是否進行刪除,如果確定,就刪除,超連結執行的是js方法,在js裡訪問,跳轉servlet,,servlet中呼叫dao方法。
<a href="#" onclick="doDelete(${stu.sid})">刪除</a>
<script type="text/javascript"> function doDelete(sid) { // 彈出對話方塊,點選確定,請求Servlet var flag = confirm("是否確定刪除?"); if(flag){ //訪問servlet //window.location.href="DeleteServlet?sid="+sid; location.href="DeleteServlet?sid="+sid; } } </script>
package com.dashucoding.servlet; import java.io.IOException; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.dashucoding.service.StudentService; import com.dashucoding.service.impl.StudentServiceImpl; /** * 用於處理刪除學生 */ public class DeleteServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { int sid = Integer.parseInt(request.getParameter("sid")); // System.out.println("sid="+sid); // 執行刪除 StudentService service = new StudentServiceImpl(); service.delete(sid); // 跳轉到列表頁 request.getRequestDispatcher("StudentListServlet").forward(request, response); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
package com.dashucoding.service.impl; import java.sql.SQLException; import java.util.List; import com.dashucoding.dao.StudentDao; import com.dashucoding.dao.impl.StudentDaoImpl; import com.dashucoding.domain.Student; import com.dashucoding.service.StudentService; /* * 這是學生業務實現 * */ public class StudentServiceImpl implements StudentService{ @Override public List<Student> findAll() throws SQLException { StudentDao dao = new StudentDaoImpl(); return dao.findAll(); } @Override public void insert(Student student) throws SQLException { // TODO Auto-generated method stub StudentDao dao = new StudentDaoImpl(); dao.insert(student); } @Override public void delete(int sid) throws SQLException { // TODO Auto-generated method stub StudentDao dao = new StudentDaoImpl(); dao.delete(sid); } }
package com.dashucoding.dao; import java.sql.SQLException; import java.util.List; import com.dashucoding.domain.Student; /* * 這是針對學生表的資料訪問 * * */ public interface StudentDao { /* * 查詢所有學生 list<Student> */ List<Student> findAll() throws SQLException; void insert(Student student) throws SQLException; // sid根據id刪除學生 void delete(int sid) throws SQLException; }
package com.dashucoding.dao.impl; import java.sql.Connection; import java.sql.SQLException; import java.util.List; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanListHandler; import com.dashucoding.dao.StudentDao; import com.dashucoding.domain.Student; import com.dashucoding.util.JDBCUtil02; /* *這是StudentDao的實現,針對前面定義的規範,做出具體的實現 * */ public class StudentDaoImpl implements StudentDao { /* * 查詢所有學生 */ @Override public List<Student> findAll() throws SQLException { QueryRunner runner = new QueryRunner(JDBCUtil02.getDataSource()); return runner.query("select * from stu", new BeanListHandler<Student>(Student.class)); } @Override public void insert(Student student) throws SQLException { // TODO Auto-generated method stub QueryRunner runner = new QueryRunner(JDBCUtil02.getDataSource()); runner.update("insert into stu values(null, ?,?,?,?,?,?)", student.getSname(), student.getGender(), student.getPhone(), student.getBirthday(), student.getHobby(), student.getInfo() ); } @Override public void delete(int sid) throws SQLException { // TODO Auto-generated method stub QueryRunner runner = new QueryRunner(JDBCUtil02.getDataSource()); runner.update("delete from stu where sid=?", sid); } }
學生管理系統更新
fn:contains()函式
fn:contain()函式用於確定一個字串是否包含指定的子串,函式的語法格式如下:
<c:if test="${fn:contains()"></c:if>
fn:contains Tests if an input string contains the specified substring.
更新,點選列表上的按鈕進行更新,跳轉EditServlet,根據id查詢這個學生的所有資訊,跳轉到更新的頁面,顯示在瀏覽器,修改後提交到UpdateServlet,提交資料要帶id,獲取資料,呼叫service和呼叫dao。
程式碼案例:

程式碼
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c"uri="http://java.sun.com/jsp/jstl/core"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>學生列表頁面</title> <script type="text/javascript"> function doDelete(sid) { // 彈出對話方塊,點選確定,請求Servlet var flag = confirm("是否確定刪除?"); if(flag){ //訪問servlet //window.location.href="DeleteServlet?sid="+sid; location.href="DeleteServlet?sid="+sid; } } </script> </head> <body> <form action="SearchStudentServlet" method="post"> <table border="1" width="700"> <tr > <td colspan="8"> 按姓名查詢:<input type="text" name="sname"/> 按性別查詢:<select name="sgender"> <option value="">--請選擇-- <option value="男">男 <option value="女">女 </select> <input type="submit" value="查詢"> <a href="add.jsp">新增</a> </td> </tr> <tr align="center"> <td>編號</td> <td>姓名</td> <td>性別</td> <td>電話</td> <td>生日</td> <td>愛好</td> <td>簡介</td> <td>操作</td> </tr> <c:forEach items="${list }" var="stu"> <tr align="center"> <td>${stu.sid }</td> <td>${stu.sname }</td> <td>${stu.gender }</td> <td>${stu.phone }</td> <td>${stu.birthday }</td> <td>${stu.hobby }</td> <td>${stu.info }</td> <td><a href="EditServlet?sid=${stu.sid }">更新</a><a href="#" onclick="doDelete(${stu.sid})">刪除</a></td> </tr> </c:forEach> </table> </form> </body> </html>
package com.dashucoding.servlet; import java.io.IOException; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.dashucoding.domain.Student; import com.dashucoding.service.StudentService; import com.dashucoding.service.impl.StudentServiceImpl; /** * 處理單個學生的更新,查詢學生的資訊,跳轉到更新的頁面 */ public class EditServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { // 接收id int sid = Integer.parseInt(request.getParameter("sid")); // 查詢學生資料 StudentService service = new StudentServiceImpl(); Student stu = service.findStudentById(sid); // 顯示資料 // 儲存資料 request.setAttribute("stu", stu); // 跳轉 request.getRequestDispatcher("edit.jsp").forward(request, response); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse *response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package com.dashucoding.dao; import java.sql.SQLException; import java.util.List; import com.dashucoding.domain.Student; /* * 這是針對學生表的資料訪問 * * */ public interface StudentDao { /* * 查詢所有學生 list<Student> */ List<Student> findAll() throws SQLException; void insert(Student student) throws SQLException; // sid根據id刪除學生 void delete(int sid) throws SQLException; // 根據id查詢單個學生物件 Student findStudentById(int sid) throws SQLException; // 更新學生資訊 void update(Student student) throws SQLException; }
package com.dashucoding.dao.impl; import java.sql.Connection; import java.sql.SQLException; import java.util.List; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanHandler; import org.apache.commons.dbutils.handlers.BeanListHandler; import com.dashucoding.dao.StudentDao; import com.dashucoding.domain.Student; import com.dashucoding.util.JDBCUtil02; /* *這是StudentDao的實現,針對前面定義的規範,做出具體的實現 * */ public class StudentDaoImpl implements StudentDao { /* * 查詢所有學生 */ @Override public List<Student> findAll() throws SQLException { QueryRunner runner = new QueryRunner(JDBCUtil02.getDataSource()); return runner.query("select * from stu", new BeanListHandler<Student>(Student.class)); } @Override public void insert(Student student) throws SQLException { // TODO Auto-generated method stub QueryRunner runner = new QueryRunner(JDBCUtil02.getDataSource()); runner.update("insert into stu values(null, ?,?,?,?,?,?)", student.getSname(), student.getGender(), student.getPhone(), student.getBirthday(), student.getHobby(), student.getInfo() ); } @Override public void delete(int sid) throws SQLException { // TODO Auto-generated method stub QueryRunner runner = new QueryRunner(JDBCUtil02.getDataSource()); runner.update("delete from stu where sid=?", sid); } @Override public Student findStudentById(int sid) throws SQLException { // TODO Auto-generated method stub QueryRunner runner = new QueryRunner(JDBCUtil02.getDataSource()); return runner.query("select * from stu where sid = ?", new BeanHandler<Student>(Student.class), sid); } @Override public void update(Student student) throws SQLException { // TODO Auto-generated method stub QueryRunner runner = new QueryRunner(JDBCUtil02.getDataSource()); runner.update("update stu set sname=?, gender=?, phone=?, birthday=?, hobby=?, info=? where sid=?", student.getSname(), student.getGender(), student.getPhone(), student.getBirthday(), student.getHobby(), student.getInfo(), student.getSid()); } }
package com.dashucoding.service; import java.sql.SQLException; import java.util.List; import com.dashucoding.domain.Student; /* * 這是學生的業務處理規範 * */ public interface StudentService { /* * 查詢所有學生 list<Student> */ List<Student> findAll() throws SQLException; void insert(Student student) throws SQLException; // sid根據id刪除學生 void delete(int sid) throws SQLException; // 根據id查詢單個學生物件 Student findStudentById(int sid) throws SQLException; // 更新學生資訊 void update(Student student) throws SQLException; }
package com.dashucoding.service.impl; import java.sql.SQLException; import java.util.List; import com.dashucoding.dao.StudentDao; import com.dashucoding.dao.impl.StudentDaoImpl; import com.dashucoding.domain.Student; import com.dashucoding.service.StudentService; /* * 這是學生業務實現 * */ public class StudentServiceImpl implements StudentService { @Override public List<Student> findAll() throws SQLException { StudentDao dao = new StudentDaoImpl(); return dao.findAll(); } @Override public void insert(Student student) throws SQLException { // TODO Auto-generated method stub StudentDao dao = new StudentDaoImpl(); dao.insert(student); } @Override public void delete(int sid) throws SQLException { // TODO Auto-generated method stub StudentDao dao = new StudentDaoImpl(); dao.delete(sid); } @Override public Student findStudentById(int sid) throws SQLException { // TODO Auto-generated method stub StudentDao dao = new StudentDaoImpl(); return dao.findStudentById(sid); } @Override public void update(Student student) throws SQLException { // TODO Auto-generated method stub StudentDao dao = new StudentDaoImpl(); dao.update(student); } }
package com.dashucoding.servlet; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Arrays; import java.util.Date; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.dashucoding.domain.Student; import com.dashucoding.service.StudentService; import com.dashucoding.service.impl.StudentServiceImpl; /** * Servlet implementation class UpdateServlet */ public class UpdateServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); try { // 1. 獲取客戶端提交上來的資料 int sid = Integer.parseInt(request.getParameter("sid")); String sname = request.getParameter("sname"); String gender = request.getParameter("gender"); String phone = request.getParameter("phone"); String birthday = request.getParameter("birthday"); String info = request.getParameter("info"); // String hobby = request.getParameter("hobby"); String[] h = request.getParameterValues("hobby"); String hobby = Arrays.toString(h); hobby = hobby.substring(1, hobby.length() - 1); // 2. 新增到資料庫 Date date = new SimpleDateFormat("yyyy-MM-dd").parse(birthday); Student student = new Student(sid, sname, gender, phone, hobby, info, date); // 2. 更新資料庫資料 StudentService service = new StudentServiceImpl(); service.update(student); // 3. 跳轉介面 request.getRequestDispatcher("StudentListServlet").forward(request, response); } catch (Exception e) { e.printStackTrace(); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package com.dashucoding.servlet; import java.io.IOException; import java.sql.SQLException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.dashucoding.dao.StudentDao; import com.dashucoding.dao.impl.StudentDaoImpl; import com.dashucoding.domain.Student; import com.dashucoding.service.StudentService; import com.dashucoding.service.impl.StudentServiceImpl; public class StudentListServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { // 查詢所有的學生 StudentService service = new StudentServiceImpl(); List<Student> list = service.findAll(); // 把資料儲存到作用域中 request.setAttribute("list", list); // 跳轉頁面 request.getRequestDispatcher("list.jsp").forward(request,response); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>更新學生頁面</title> </head> <body> <h3>更新學生頁面</h3> <form method="post" action="UpdateServlet"> <input type="hidden" name="sid" value="${stu.sid }"> <table border="1" width="600"> <tr> <td>姓名</td> <td><input type="text" name="sname" value="${stu.sname }"></td> </tr> <tr> <td>性別</td> <td> <input type="radio" name="gender" value="男" <c:if test="${stu.gender == '男'}">checked</c:if>>男 <input type="radio" name="gender" value="女" <c:if test="${stu.gender == '女'}">checked</c:if>>女 </td> </tr> <tr> <td>電話</td> <td><input type="text" name="phone" value="${stu.phone }"></td> </tr> <tr> <td>生日</td> <td><input type="text" name="birthday" value="${stu.birthday }"></td> </tr> <tr> <td>愛好</td> <td> <input type="checkbox" name="hobby" value="游泳" <c:if test="${fn:contains(stu.hobby,'游泳') }">checked</c:if>>游泳 <input type="checkbox" name="hobby" value="籃球" <c:if test="${fn:contains(stu.hobby,'籃球') }">checked</c:if>>籃球 <input type="checkbox" name="hobby" value="足球" <c:if test="${fn:contains(stu.hobby,'足球') }">checked</c:if>>足球 <input type="checkbox" name="hobby" value="看書" <c:if test="${fn:contains(stu.hobby,'看書') }">checked</c:if>>看書 <input type="checkbox" name="hobby" value="寫字" <c:if test="${fn:contains(stu.hobby,'寫字') }">checked</c:if>>寫字 </td> </tr> <tr> <td>簡介</td> <td><textarea name="info" rows="3" cols="20">${stu.info }</textarea></td> </tr> <tr> <td colspan="2"> <input type="submit" value="更新"> </td> </tr> </table> </form> </body> </html>

程式碼結構
進行模糊查詢
查詢結果

結果

結構
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c"uri="http://java.sun.com/jsp/jstl/core"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>學生列表頁面</title> <script type="text/javascript"> function doDelete(sid) { // 彈出對話方塊,點選確定,請求Servlet var flag = confirm("是否確定刪除?"); if(flag){ //訪問servlet //window.location.href="DeleteServlet?sid="+sid; location.href="DeleteServlet?sid="+sid; } } </script> </head> <body> <form action="SearchStudentServlet" method="post"> <table border="1" width="700"> <tr > <td colspan="8"> 按姓名查詢:<input type="text" name="sname"/> 按性別查詢:<select name="sgender"> <option value="">--請選擇-- <option value="男">男 <option value="女">女 </select> <input type="submit" value="查詢"> <a href="add.jsp">新增</a> </td> </tr> <tr align="center"> <td>編號</td> <td>姓名</td> <td>性別</td> <td>電話</td> <td>生日</td> <td>愛好</td> <td>簡介</td> <td>操作</td> </tr> <c:forEach items="${list }" var="stu"> <tr align="center"> <td>${stu.sid }</td> <td>${stu.sname }</td> <td>${stu.gender }</td> <td>${stu.phone }</td> <td>${stu.birthday }</td> <td>${stu.hobby }</td> <td>${stu.info }</td> <td><a href="EditServlet?sid=${stu.sid }">更新</a><a href="#" onclick="doDelete(${stu.sid})">刪除</a></td> </tr> </c:forEach> </table> </form> </body> </html>
package com.dashucoding.util; public class TextUtils { /** * 判斷某一個字串是否為空。 * * @param s * @return */ public static boolean isEmpty(CharSequence s) { return s == null || s.length() == 0; } }
package com.dashucoding.dao; import java.sql.SQLException; import java.util.List; import com.dashucoding.domain.Student; /* * 這是針對學生表的資料訪問 * * */ public interface StudentDao { // 根據姓名或性別,查詢 List<Student> searchStudent(String sname, String sgender) throws SQLException; /* * 查詢所有學生 list<Student> */ List<Student> findAll() throws SQLException; void insert(Student student) throws SQLException; // sid根據id刪除學生 void delete(int sid) throws SQLException; // 根據id查詢單個學生物件 Student findStudentById(int sid) throws SQLException; // 更新學生資訊 void update(Student student) throws SQLException; }
package com.dashucoding.dao.impl; import java.sql.Connection; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanHandler; import org.apache.commons.dbutils.handlers.BeanListHandler; import com.dashucoding.dao.StudentDao; import com.dashucoding.domain.Student; import com.dashucoding.util.JDBCUtil02; import com.dashucoding.util.TextUtils; /* *這是StudentDao的實現,針對前面定義的規範,做出具體的實現 * */ public class StudentDaoImpl implements StudentDao { /* * 查詢所有學生 */ @Override public List<Student> findAll() throws SQLException { QueryRunner runner = new QueryRunner(JDBCUtil02.getDataSource()); return runner.query("select * from stu", new BeanListHandler<Student>(Student.class)); } @Override public void insert(Student student) throws SQLException { // TODO Auto-generated method stub QueryRunner runner = new QueryRunner(JDBCUtil02.getDataSource()); runner.update("insert into stu values(null, ?,?,?,?,?,?)", student.getSname(), student.getGender(), student.getPhone(), student.getBirthday(), student.getHobby(), student.getInfo() ); } @Override public void delete(int sid) throws SQLException { // TODO Auto-generated method stub QueryRunner runner = new QueryRunner(JDBCUtil02.getDataSource()); runner.update("delete from stu where sid=?", sid); } @Override public Student findStudentById(int sid) throws SQLException { // TODO Auto-generated method stub QueryRunner runner = new QueryRunner(JDBCUtil02.getDataSource()); return runner.query("select * from stu where sid = ?", new BeanHandler<Student>(Student.class), sid); } @Override public void update(Student student) throws SQLException { // TODO Auto-generated method stub QueryRunner runner = new QueryRunner(JDBCUtil02.getDataSource()); runner.update("update stu set sname=?, gender=?, phone=?, birthday=?, hobby=?, info=? where sid=?", student.getSname(), student.getGender(), student.getPhone(), student.getBirthday(), student.getHobby(), student.getInfo(), student.getSid()); } // 模糊查詢 @Override public List<Student> searchStudent(String sname, String sgender) throws SQLException { // TODO Auto-generated method stub /*System.out.println(sname + sgender);*/ QueryRunner runner = new QueryRunner(JDBCUtil02.getDataSource()); /* * String sql = "select * from stu where sname=? or sgender=?"; * select * from stu where sname like ?; * select * from stu where gender = ?; * select * from stu where sname like ? and gender = ?; * 如果兩個都沒有就查詢所有 * sql = "select * from stu" * if(姓名){ *sql = sql + "where sname like ?"; * } * if(性別){ *sql = sql + "where gender = ?"; * } * * String sql = "select * from stu where 1=1"; * if(姓名){ *sql = sql + " and sname like ? "; * } * if(性別){ *sql = sql + " and gender = ? "; * } * */ String sql = "select * from stu where 1=1"; List<String> list = new ArrayList<String>(); if(!TextUtils.isEmpty(sname)) { sql = sql + " and sname like ? "; list.add("%"+sname+"%"); } if(!TextUtils.isEmpty(sgender)) { sql = sql + " and gender = ? "; list.add(sgender); } /*list.toArray()*/ return runner.query(sql, new BeanListHandler<Student>(Student.class),list.toArray()); } }
package com.dashucoding.service; import java.sql.SQLException; import java.util.List; import com.dashucoding.domain.Student; /* * 這是學生的業務處理規範 * */ public interface StudentService { // 根據姓名或性別,查詢 List<Student> searchStudent(String sname, String sgender) throws SQLException; /* * 查詢所有學生 list<Student> */ List<Student> findAll() throws SQLException; void insert(Student student) throws SQLException; // sid根據id刪除學生 void delete(int sid) throws SQLException; // 根據id查詢單個學生物件 Student findStudentById(int sid) throws SQLException; // 更新學生資訊 void update(Student student) throws SQLException; }
package com.dashucoding.service.impl; import java.sql.SQLException; import java.util.List; import com.dashucoding.dao.StudentDao; import com.dashucoding.dao.impl.StudentDaoImpl; import com.dashucoding.domain.Student; import com.dashucoding.service.StudentService; /* * 這是學生業務實現 * */ public class StudentServiceImpl implements StudentService { @Override public List<Student> findAll() throws SQLException { StudentDao dao = new StudentDaoImpl(); return dao.findAll(); } @Override public void insert(Student student) throws SQLException { // TODO Auto-generated method stub StudentDao dao = new StudentDaoImpl(); dao.insert(student); } @Override public void delete(int sid) throws SQLException { // TODO Auto-generated method stub StudentDao dao = new StudentDaoImpl(); dao.delete(sid); } @Override public Student findStudentById(int sid) throws SQLException { // TODO Auto-generated method stub StudentDao dao = new StudentDaoImpl(); return dao.findStudentById(sid); } @Override public void update(Student student) throws SQLException { // TODO Auto-generated method stub StudentDao dao = new StudentDaoImpl(); dao.update(student); } @Override public List<Student> searchStudent(String sname, String sgender) throws SQLException { // TODO Auto-generated method stub StudentDao dao = new StudentDaoImpl(); return dao.searchStudent(sname, sgender); } }
package com.dashucoding.servlet; import java.io.IOException; import java.sql.SQLException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.dashucoding.domain.Student; import com.dashucoding.service.StudentService; import com.dashucoding.service.impl.StudentServiceImpl; /** * Servlet implementation class SearchStudentServlet */ public class SearchStudentServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); try { // 取到了要查詢的關鍵資料 String sname = request.getParameter("sname"); String sgender = request.getParameter("sgender"); // 找service查詢 StudentService service = new StudentServiceImpl(); List<Student> list = service.searchStudent(sname, sgender); /*for(Student student : list) { System.out.println("stu=" + student); }*/ request.setAttribute("list", list); // 跳轉介面 request.getRequestDispatcher("list.jsp").forward(request, response); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package com.dashucoding.servlet; import java.io.IOException; import java.sql.SQLException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.dashucoding.dao.StudentDao; import com.dashucoding.dao.impl.StudentDaoImpl; import com.dashucoding.domain.Student; import com.dashucoding.service.StudentService; import com.dashucoding.service.impl.StudentServiceImpl; public class StudentListServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { // 查詢所有的學生 StudentService service = new StudentServiceImpl(); List<Student> list = service.findAll(); // 把資料儲存到作用域中 request.setAttribute("list", list); // 跳轉頁面 request.getRequestDispatcher("list.jsp").forward(request,response); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
結言
好了,歡迎在留言區留言,與大家分享你的經驗和心得。
感謝你學習今天的內容,如果你覺得這篇文章對你有幫助的話,也歡迎把它分享給更多的朋友,感謝。
達叔小生:往後餘生,唯獨有你
You and me, we are family !
90後帥氣小夥,良好的開發習慣;獨立思考的能力;主動並且善於溝通
簡書部落格: 達叔小生
https://www.jianshu.com/u/c785ece603d1結語
- 下面我將繼續對 其他知識 深入講解 ,有興趣可以繼續關注
- 小禮物走一走 or 點贊