JavaWeb实现学生信息管理系统(2)

 更新时间:2021年09月10日 11:56:06   作者:笑-_-笑  
这篇文章主要介绍了JavaWeb实现学生信息管理系统的第二篇,实现学生管理系统的查找和添加功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文接着上一篇,继续为大家分享了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. 添加结果如下:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • SpringBoot+SpringSecurity实现基于真实数据的授权认证

    SpringBoot+SpringSecurity实现基于真实数据的授权认证

    Spring Security是一个功能强大且高度可定制的身份验证和访问控制框架,Spring Security主要做两个事情,认证、授权。这篇文章主要介绍了SpringBoot+SpringSecurity实现基于真实数据的授权认证,需要的朋友可以参考下
    2021-05-05
  • 详解Java中的final关键字

    详解Java中的final关键字

    这篇文章主要给大家介绍了关于Java中final关键字的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2020-06-06
  • java生成图片验证码功能

    java生成图片验证码功能

    最近在用ssm框架做一个管理系统,做到登录验证时,需要实现图片验证码功能,今天小编给大家分享基于java制作的图片验证码功能,感兴趣的朋友一起看看吧
    2019-12-12
  • java时间戳转换为日期格式的多种方式

    java时间戳转换为日期格式的多种方式

    本文介绍了五种将Java时间戳转换为日期格式的方法,包括使用Date类、LocalDateTime类、Instant类、DateUtils类以及自定义时区,每种方法都有其适用场景,可以根据具体需求选择合适的方法,感兴趣的朋友跟随小编一起看看吧
    2025-01-01
  • 一篇文章带你入门java算术运算符(加减乘除余,字符连接)

    一篇文章带你入门java算术运算符(加减乘除余,字符连接)

    这篇文章主要介绍了Java基本数据类型和运算符,结合实例形式详细分析了java基本数据类型、数据类型转换、算术运算符、逻辑运算符等相关原理与操作技巧,需要的朋友可以参考下
    2021-08-08
  • Spring中基于xml配置管理Bean的步骤

    Spring中基于xml配置管理Bean的步骤

    Spring容器通常理解为BeanFactory或者ApplicationContext,我们知道spring的IOC容器能够帮我们创建对象,对象交给spring管理之后我们就不用手动去new对象,这篇文章主要介绍了Spring中基于xml配置管理Bean的步骤,需要的朋友可以参考下
    2023-11-11
  • IntelliJ安装并使用Rust IDE插件

    IntelliJ安装并使用Rust IDE插件

    这篇文章主要介绍了IntelliJ安装并使用Rust IDE插件,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-01-01
  • Java小白第一次就能看懂的网络编程

    Java小白第一次就能看懂的网络编程

    网络编程是指编写运行在多个设备(计算机)的程序,这些设备都通过网络连接起来。本文介绍了一些网络编程基础的概念,并用Java来实现TCP和UDP的Socket的编程,来让读者更好的了解其原理
    2021-08-08
  • java如何使用zip压缩实现读取写入

    java如何使用zip压缩实现读取写入

    这篇文章主要为大家介绍了java如何使用zip压缩实现读取写入示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-11-11
  • 一文详解Spring是怎样处理循环依赖的

    一文详解Spring是怎样处理循环依赖的

    循环依赖简单理解就是A,B 两个bean相互依赖,A依赖B,B依赖A,A->B、B->A大概就是这样,这篇文章主要介绍了Spring是怎样处理循环依赖的,文中通过代码示例给大家介绍的非常详细,具有一定的参考价值,需要的朋友可以参考下
    2024-01-01

最新评论