JavaWeb实现学生信息管理系统(2)
作者:笑-_-笑 发布时间:2022-03-04 02:31:12
本文接着上一篇,继续为大家分享了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. 添加结果如下:
来源:https://blog.csdn.net/weixin_44270855/article/details/104223040


猜你喜欢
- 本文实例讲述了C#中Winform窗体Form的关闭按钮变灰色的方法,对C#程序设计有一定的借鉴价值,分享给大家供大家参考之用。具体方法如下
- 实现效果:列出某个目录下的特定后缀名文件(如,列出D盘根目录下txt后缀的文件)import java.io.File;import jav
- 1 前言ATMS 即 ActivityTaskManagerService,用于管理 Activity 及其容器(任务、堆栈、显示等)。AT
- 本文实例为大家分享了Java实现小型图书馆管理系统的具体代码,供大家参考,具体内容如下以下为小型图书馆管理系统模式图:模式总体概述:其中IB
- spring WEB MVC框架提供了一个MVC(model-view-controller)模型-视图-控制器的结构和组件,利用它可以开发
- RelativePanel是在Windows 10 UWP程序中引入的一种新的布局面板,它是通过附加属性设置元素间的位置关系来对实现布局的。
- 如果要想使一个catch block能抓获多种数据类型的异常对象的话,怎么办?C++标准中定义了一种特殊的catch用法,那就是” catc
- SpringBoot配置https(SSL证书)最近在做微信小程序,https是必须条件仅需三步SpringBoot2.x版本对比一下这个小
- 先看下这个问题的背景:假设有一个spring应用,开发人员希望自定义一个注解@Log,可以加到指定的方法上,实现自动记录日志(入参、出参、响
- 今天看到一个银行的APP上面的loadingview 挺好的,就尝试着自己实现,觉得很简单,但自己实现起来还是发现了一些问题。Loading
- 定义工具类-创建对应的日志对象定义枚举类-存储定义的日志文件名称logback.xml里配置对应的日志名称和日志等级1、工具类 Logger
- 本文实例总结了C#中string.format用法。分享给大家供大家参考。具体分析如下:String.Format 方法的几种定义:Stri
- 使用DOM4J方式生成XML文件的步骤如下:引入JAR包通过DocumentHelper类的createDocument()创建Docume
- 跑起来的效果看每个类的test方法,自己调用来测试目的是看看哪个算法好用,移植的时候比较单纯没有研究懂算法,代码结构也没改动,只是移植到C#
- 获取map的值主要有四种方法,这四种方法又分为两类,一类是调用map.keySet()方法来获取key和value的值,另一类则是通过map
- 前言:Android自定义View对于刚入门乃至工作几年的程序员来说都是非常恐惧的,但也是Android进阶学习的必经之路,平时
- 介绍一款简洁实用的图片编辑器,纯dart开发。支持:涂鸦、旋转&翻转、马赛克、添加文字,及自定义ui风格。功能演示涂鸦旋转&
- 本文实例为大家分享了Androidstudio调用摄像头拍照并保存照片的具体代码,供大家参考,具体内容如下首先在manifest.xmlns
- 通常浏览器都有将网页生成图片的功能,本文实例讲述了Winform实现将网页生成图片的方法。分享给大家供大家参考。具体方法如下:工具截图如下:
- 实践过程效果代码public partial class Form1 : Form{ private HookEx