JavaWeb實現(xiàn)學(xué)生信息管理系統(tǒng)(2)
本文接著上一篇,繼續(xù)為大家分享了JavaWeb實現(xiàn)學(xué)生信息管理系統(tǒng)的第二篇,供大家參考,具體內(nèi)容如下
今日任務(wù):實現(xiàn)學(xué)生管理系統(tǒng)的查找和添加功能!
一、查詢學(xué)生信息
1. index.jsp
先寫一個JSP頁面【W(wǎng)ebContent/index.jsp】,里面放一個超鏈接
<a href="StudentListServlet" rel="external nofollow" >顯示所有學(xué)生列表</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;
/**
*
* 負責(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ù)存儲到作用域中
//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;
/**
* 這是針對學(xué)生表的數(shù)據(jù)訪問
* @author Administrator
*
*/
public interface StudentDao {
/**
* 查詢所有學(xué)生
* @return List<Student>
*/
List<Student> findAll() throws SQLException;
}
3.2 StudentDaoImpl.java【com.dao.impl包下】
實現(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存儲數(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>學(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>編號</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>
實現(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
/** * 添加學(xué)生 * @param student 需要添加到數(shù)據(jù)庫的學(xué)生對象 * @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)到查詢所有學(xué)生信息的那個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é)果如下:


以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- javaWeb實現(xiàn)學(xué)生信息管理系統(tǒng)
- JavaWeb實現(xiàn)學(xué)生信息管理系統(tǒng)(1)
- JavaWeb實現(xiàn)學(xué)生信息管理系統(tǒng)(3)
- JavaWeb倉庫管理系統(tǒng)詳解
- 基于javaweb+jsp實現(xiàn)學(xué)生宿舍管理系統(tǒng)
- 基于javaweb+jsp實現(xiàn)企業(yè)車輛管理系統(tǒng)
- 基于javaweb+jsp實現(xiàn)個人日記管理系統(tǒng)
- 基于javaweb+jsp實現(xiàn)企業(yè)財務(wù)記賬管理系統(tǒng)
- 基于javaweb+jsp的游泳館會員管理系統(tǒng)(附源碼)
- JavaWeb實現(xiàn)學(xué)生管理系統(tǒng)的超詳細過程
相關(guān)文章
SpringBoot+SpringSecurity實現(xiàn)基于真實數(shù)據(jù)的授權(quán)認證
Spring Security是一個功能強大且高度可定制的身份驗證和訪問控制框架,Spring Security主要做兩個事情,認證、授權(quán)。這篇文章主要介紹了SpringBoot+SpringSecurity實現(xiàn)基于真實數(shù)據(jù)的授權(quán)認證,需要的朋友可以參考下2021-05-05
一篇文章帶你入門java算術(shù)運算符(加減乘除余,字符連接)
這篇文章主要介紹了Java基本數(shù)據(jù)類型和運算符,結(jié)合實例形式詳細分析了java基本數(shù)據(jù)類型、數(shù)據(jù)類型轉(zhuǎn)換、算術(shù)運算符、邏輯運算符等相關(guān)原理與操作技巧,需要的朋友可以參考下2021-08-08

