您好,登錄后才能下訂單哦!
這篇文章主要講解了“JavaWeb實現(xiàn)學生信息管理系統(tǒng)之二”,文中的講解內(nèi)容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“JavaWeb實現(xiàn)學生信息管理系統(tǒng)之二”吧!
本文接著上一篇,繼續(xù)為大家分享了JavaWeb實現(xiàn)學生信息管理系統(tǒng)的第二篇,供大家參考,具體內(nèi)容如下
今日任務(wù):實現(xiàn)學生管理系統(tǒng)的查找和添加功能!
1. index.jsp
先寫一個JSP頁面【W(wǎng)ebContent/index.jsp】,里面放一個超鏈接
<a href="StudentListServlet" rel="external nofollow" >顯示所有學生列表</a>
2. StudentListServlet.java
寫StudentListServlet【com.servlet包下的StudentListServlet.java】,接受請求,去調(diào)用service,再由service調(diào)用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; /** * * 負責查詢所有的學生信息,然后呈現(xiàn)到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.先把數(shù)據(jù)存儲到作用域中 //3..跳轉(zhuǎn)頁面 } 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; /** * 這是針對學生表的數(shù)據(jù)訪問 * @author Administrator * */ public interface StudentDao { /** * 查詢所有學生 * @return List<Student> */ List<Student> findAll() throws SQLException; }
3.2 StudentDaoImpl.java【com.dao.impl包下】
實現(xiàn)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存儲數(shù)據(jù),并作出頁面響應(yīng)
//2.先把數(shù)據(jù)存儲到作用域中 request.setAttribute("list", list); //3..跳轉(zhuǎn)頁面 request.getRequestDispatcher("list.jsp").forward(request, response);
6. list.jsp
在list.jsp【W(wǎng)ebContent/list.jsp】上顯示數(shù)據(jù)。
<%@ 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. 查找結(jié)果如下:
1. add.jsp
我們需要先跳轉(zhuǎn)到增加頁面,編寫增加頁面add.jsp【W(wǎng)ebContent/add.jsp】
<body> <h4>添加學生頁面</h4> <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>
實現(xiàn)結(jié)果如下:
2. AddServlet.java
點擊添加,提交數(shù)據(jù)到AddServlet,處理數(shù)據(jù)。
【備:com.servlet包下的AddServlet.java】
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); try { //1.獲取客戶端提交上來的數(shù)據(jù) 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.添加到數(shù)據(jù)庫 //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.跳轉(zhuǎn)到列表頁 //這里是直接跳轉(zhuǎn)到頁面上,那么這個頁面會重新翻譯一次,上面那個request里面的數(shù)據(jù)就沒有了 //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 需要添加到數(shù)據(jù)庫的學生對象 * @throws SQLException */ void insert(Student student) throws SQLException;
4. Dao & Service的實現(xiàn)
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. 注意
完成了上述存儲工作之后,需要跳轉(zhuǎn)到列表頁面,這里不能直接跳轉(zhuǎn)到列表頁面,否則沒有什么內(nèi)容顯示。應(yīng)該先跳轉(zhuǎn)到查詢所有學生信息的那個servlet,即StudentListServlet,再由這個servlet跳轉(zhuǎn)到列表頁面。
request.getRequestDispatcher("StudentListServlet").forward(request, response);
hobby的value有多個值。處理時需多次轉(zhuǎn)化:
//String hobby = request.getParameter("hobby"); String [] h = request.getParameterValues("hobby"); //[籃球,足球,寫字]-----籃球,足球,寫字 String hobby = Arrays.toString(h); hobby = hobby.substring(1,hobby.length()-1);
6. 添加結(jié)果如下:
未完待續(xù)。。。
感謝各位的閱讀,以上就是“JavaWeb實現(xiàn)學生信息管理系統(tǒng)之二”的內(nèi)容了,經(jīng)過本文的學習后,相信大家對JavaWeb實現(xiàn)學生信息管理系統(tǒng)之二這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。