JavaWeb實現學生信息管理系統(2)
本文接著上一篇,繼續為大傢分享瞭JavaWeb實現學生信息管理系統的第二篇,供大傢參考,具體內容如下
今日任務:實現學生管理系統的查找和添加功能!
一、查詢學生信息
1. index.jsp
先寫一個JSP頁面【WebContent/index.jsp】,裡面放一個超鏈接
<a href="StudentListServlet" rel="external nofollow" >顯示所有學生列表</a>
2. StudentListServlet.java
寫StudentListServlet【com.servlet包下的StudentListServlet.java】,接受請求,去調用service,再由service調用dao
package com.servlet; import java.io.IOException; import java.sql.SQLException; 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.domain.Student; import com.service.StudentService; import com.service.impl.StudentServiceImpl; /** * * 負責查詢所有的學生信息,然後呈現到list.jsp頁面上 * */ @WebServlet("/StudentListServlet") public class StudentListServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { //1.查詢出來所有的學生 StudentService service = new StudentServiceImpl(); List<Student> list = service.findAll(); //2.先把數據存儲到作用域中 //3..跳轉頁面 } catch (SQLException e) { e.printStackTrace(); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
3. StudentDao.java & StudentDaoImpl.java
3.1 StudentDao.java【com.dao包下】
package com.dao; import java.sql.SQLException; import java.util.List; import com.domain.Student; /** * 這是針對學生表的數據訪問 * @author Administrator * */ public interface StudentDao { /** * 查詢所有學生 * @return List<Student> */ List<Student> findAll() throws SQLException; }
3.2 StudentDaoImpl.java【com.dao.impl包下】
實現StudentDao裡的findAll()方法。
public class StudentDaoImpl implements StudentDao { /** * 查詢所有學生 * @throws SQLException */ @Override public List<Student> findAll() throws SQLException { QueryRunner runner = new QueryRunner(JDBCUtil02.getDataSource()); String sql = "select * from stu"; List<Student> list = runner.query(sql, new BeanListHandler<Student>(Student.class)); return list; } }
4. StudentService.java 和 StudentService.java
4.1 StudentService.java
代碼同StudentDao.java,
public interface StudentService { /** * 查詢所有學生 * @return List<Student> * */ List<Student> findAll() throws SQLException; }
4.2 StudentService.java
public class StudentServiceImpl implements StudentService{ @Override public List<Student> findAll() throws SQLException { StudentDao dao = new StudentDaoImpl(); return dao.findAll(); } }
5. 在StudentListServlet存儲數據,並作出頁面響應
//2.先把數據存儲到作用域中 request.setAttribute("list", list); //3..跳轉頁面 request.getRequestDispatcher("list.jsp").forward(request, response);
6. list.jsp
在list.jsp【WebContent/list.jsp】上顯示數據。
<%@ 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> <html> <head> <meta charset="UTF-8"> <title>學生列表頁面 </title> </head> <body> <table border="1" width="700"> <tr> <td colspan="8"> <a href="add.jsp" rel="external nofollow" >添加</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="#" rel="external nofollow" rel="external nofollow" >更新</a> <a href="#" rel="external nofollow" rel="external nofollow" >刪除</a></td> </tr> </c:forEach> </table> </body> </html>
7. 查找結果如下:
二、添加學生信息
1. add.jsp
我們需要先跳轉到增加頁面,編寫增加頁面add.jsp【WebContent/add.jsp】
<body> <h3>添加學生頁面</h3> <form action="AddServlet" method="post"> <table border="1" width="600"> <tr> <td>姓名</td> <td><input type="text" name="sname"></td> </tr> <tr> <td>性別</td> <td> <input type="radio" name="gender" value="男">男 <input type="radio" name="gender" value="女">女 </td> </tr> <tr> <td>電話</td> <td><input type="text" name="phone"></td> </tr> <tr> <td>生日</td> <td><input type="text" name="birthday"></td> </tr> <tr> <td>愛好</td> <td> <input type="checkbox" name="hobby" value="遊泳">遊泳 <input type="checkbox" name="hobby" value="籃球">籃球 <input type="checkbox" name="hobby" value="足球">足球 <input type="checkbox" name="hobby" value="看書">看書 <input type="checkbox" name="hobby" value="寫字">寫字 </td> </tr> <tr> <td>簡介</td> <td> <textarea rows="3" cols="20" name="info"></textarea> </td> </tr> <tr> <td colspan="2"><input type="submit" value="添加"></td> </tr> </table> </form> </body>
實現結果如下:
2. AddServlet.java
點擊添加,提交數據到AddServlet,處理數據。
【備:com.servlet包下的AddServlet.java】
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); try { //1.獲取客戶端提交上來的數據 String sname = request.getParameter("sname"); String gender = request.getParameter("gender"); String phone = request.getParameter("phone"); String birthday = request.getParameter("birthday"); //傳過來是1989-10-18 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.添加到數據庫 //String-------Date Date date = new SimpleDateFormat("yyyy-MM-dd").parse(birthday); Student student = new Student(sname,gender,phone,hobby,info,date); StudentService service = new StudentServiceImpl(); service.insert(student); //3.跳轉到列表頁 //這裡是直接跳轉到頁面上,那麼這個頁面會重新翻譯一次,上面那個request裡面的數據就沒有瞭 //request.getRequestDispatcher("list.jsp").forward(request, response); //servlet除瞭能跳jsp之外,還能跳servlet request.getRequestDispatcher("StudentListServlet").forward(request, response); } catch (Exception e) { e.printStackTrace(); } }
3. StudentDao & StudentService
/** * 添加學生 * @param student 需要添加到數據庫的學生對象 * @throws SQLException */ void insert(Student student) throws SQLException;
4. Dao & Service的實現
4.1 StudentDaoImpl.java
@Override public void insert(Student student) throws SQLException { QueryRunner runner = new QueryRunner(JDBCUtil02.getDataSource()); String sql = "insert into stu values(null,?,?,?,?,?,?)"; runner.update(sql, student.getSname(), student.getGender(), student.getPhone(), student.getBirthday(), student.getHobby(), student.getInfo() ); }
4.2 StudentServiceImpl.java
@Override public void insert(Student student) throws SQLException { StudentDao dao = new StudentDaoImpl(); dao.insert(student); }
5. 註意
完成瞭上述存儲工作之後,需要跳轉到列表頁面,這裡不能直接跳轉到列表頁面,否則沒有什麼內容顯示。應該先跳轉到查詢所有學生信息的那個servlet,即StudentListServlet,再由這個servlet跳轉到列表頁面。
request.getRequestDispatcher("StudentListServlet").forward(request, response);
hobby的value有多個值。處理時需多次轉化:
//String hobby = request.getParameter("hobby"); String [] h = request.getParameterValues("hobby"); //[籃球,足球,寫字]-----籃球,足球,寫字 String hobby = Arrays.toString(h); hobby = hobby.substring(1,hobby.length()-1);
6. 添加結果如下:
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。
推薦閱讀:
- JavaWeb實現學生信息管理系統(3)
- 解決Javaweb 提交表單到servlet時出現空白頁面,但網站不報錯問題
- Java springboot接口迅速上手,帶你半小時極速入門
- JavaWeb中請求轉發和請求重定向的區別以及使用
- JavaWeb實現簡單的自動登錄功能