JavaWeb實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)(2)
本文接著上一篇,繼續(xù)為大家分享了JavaWeb實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)的第二篇,供大家參考,具體內(nèi)容如下
今日任務(wù):實(shí)現(xiàn)學(xué)生管理系統(tǒng)的查找和添加功能!
一、查詢學(xué)生信息
1. index.jsp
先寫一個(gè)JSP頁面【W(wǎng)ebContent/index.jsp】,里面放一個(gè)超鏈接
<a href="StudentListServlet" rel="external nofollow" >顯示所有學(xué)生列表</a>
2. StudentListServlet.java
寫StudentListServlet【com.servlet包下的StudentListServlet.java】,接受請(qǐng)求,去調(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;
/**
*
* 負(fù)責(zé)查詢所有的學(xué)生信息,然后呈現(xiàn)到list.jsp頁面上
*
*/
@WebServlet("/StudentListServlet")
public class StudentListServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
//1.查詢出來所有的學(xué)生
StudentService service = new StudentServiceImpl();
List<Student> list = service.findAll();
//2.先把數(shù)據(jù)存儲(chǔ)到作用域中
//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;
/**
* 這是針對(duì)學(xué)生表的數(shù)據(jù)訪問
* @author Administrator
*
*/
public interface StudentDao {
/**
* 查詢所有學(xué)生
* @return List<Student>
*/
List<Student> findAll() throws SQLException;
}
3.2 StudentDaoImpl.java【com.dao.impl包下】
實(shí)現(xiàn)StudentDao里的findAll()方法。
public class StudentDaoImpl implements StudentDao {
/**
* 查詢所有學(xué)生
* @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 {
/**
* 查詢所有學(xué)生
* @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存儲(chǔ)數(shù)據(jù),并作出頁面響應(yīng)
//2.先把數(shù)據(jù)存儲(chǔ)到作用域中
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>學(xué)生列表頁面 </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>編號(hào)</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é)果如下:

二、添加學(xué)生信息
1. add.jsp
我們需要先跳轉(zhuǎn)到增加頁面,編寫增加頁面add.jsp【W(wǎng)ebContent/add.jsp】
<body>
<h3>添加學(xué)生頁面</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>
實(shí)現(xiàn)結(jié)果如下:

2. AddServlet.java
點(diǎn)擊添加,提交數(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)到頁面上,那么這個(gè)頁面會(huì)重新翻譯一次,上面那個(gè)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
/** * 添加學(xué)生 * @param student 需要添加到數(shù)據(jù)庫的學(xué)生對(duì)象 * @throws SQLException */ void insert(Student student) throws SQLException;
4. Dao & Service的實(shí)現(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. 注意
完成了上述存儲(chǔ)工作之后,需要跳轉(zhuǎn)到列表頁面,這里不能直接跳轉(zhuǎn)到列表頁面,否則沒有什么內(nèi)容顯示。應(yīng)該先跳轉(zhuǎn)到查詢所有學(xué)生信息的那個(gè)servlet,即StudentListServlet,再由這個(gè)servlet跳轉(zhuǎn)到列表頁面。
request.getRequestDispatcher("StudentListServlet").forward(request, response);
hobby的value有多個(gè)值。處理時(shí)需多次轉(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é)果如下:


以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- javaWeb實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)
- JavaWeb實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)(1)
- JavaWeb實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)(3)
- JavaWeb倉庫管理系統(tǒng)詳解
- 基于javaweb+jsp實(shí)現(xiàn)學(xué)生宿舍管理系統(tǒng)
- 基于javaweb+jsp實(shí)現(xiàn)企業(yè)車輛管理系統(tǒng)
- 基于javaweb+jsp實(shí)現(xiàn)個(gè)人日記管理系統(tǒng)
- 基于javaweb+jsp實(shí)現(xiàn)企業(yè)財(cái)務(wù)記賬管理系統(tǒng)
- 基于javaweb+jsp的游泳館會(huì)員管理系統(tǒng)(附源碼)
- JavaWeb實(shí)現(xiàn)學(xué)生管理系統(tǒng)的超詳細(xì)過程
相關(guān)文章
SpringBoot+SpringSecurity實(shí)現(xiàn)基于真實(shí)數(shù)據(jù)的授權(quán)認(rèn)證
Spring Security是一個(gè)功能強(qiáng)大且高度可定制的身份驗(yàn)證和訪問控制框架,Spring Security主要做兩個(gè)事情,認(rèn)證、授權(quán)。這篇文章主要介紹了SpringBoot+SpringSecurity實(shí)現(xiàn)基于真實(shí)數(shù)據(jù)的授權(quán)認(rèn)證,需要的朋友可以參考下2021-05-05
java時(shí)間戳轉(zhuǎn)換為日期格式的多種方式
本文介紹了五種將Java時(shí)間戳轉(zhuǎn)換為日期格式的方法,包括使用Date類、LocalDateTime類、Instant類、DateUtils類以及自定義時(shí)區(qū),每種方法都有其適用場(chǎng)景,可以根據(jù)具體需求選擇合適的方法,感興趣的朋友跟隨小編一起看看吧2025-01-01
一篇文章帶你入門java算術(shù)運(yùn)算符(加減乘除余,字符連接)
這篇文章主要介紹了Java基本數(shù)據(jù)類型和運(yùn)算符,結(jié)合實(shí)例形式詳細(xì)分析了java基本數(shù)據(jù)類型、數(shù)據(jù)類型轉(zhuǎn)換、算術(shù)運(yùn)算符、邏輯運(yùn)算符等相關(guān)原理與操作技巧,需要的朋友可以參考下2021-08-08
java如何使用zip壓縮實(shí)現(xiàn)讀取寫入
這篇文章主要為大家介紹了java如何使用zip壓縮實(shí)現(xiàn)讀取寫入示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11

