简易学生管理系统_web_字符流传输数据

功能

  • (1)添加学生信息,
  • (2)修改学生信息,
  • (3)删除学生信息,
  • (4)查询学生信息

(1)(2)(3)都在(4)的基础上编写,通过查询学生信息的页面的按钮来进行跳转,

前端代码(jsp页面)

首页

点击查看学生信息可以直接跳转到可以显示学生信息列表的页面
然后我们需要创建一个路径为/stu/accept的Servlet用来接收浏览器发送来的查看学生信息的请求,其中/stu可以用${pageContext.page.contextPath}来代替,这样可以确保模块路径名改变的情况下正常运行

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
  欢迎进入学生管理系统
  <a href="${pageContext.request.contextPath}/accept">查看学生信息</a>
  </body>
</html>
显示学生信息列表

ps:表格样式是别人弄的,哈哈
简易学生管理系统_web_字符流传输数据_第1张图片

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<!-- 网页使用的语言 -->
<html lang="zh-CN">
<head>
    <!-- 指定字符集 -->
    <meta charset="utf-8">
    <!-- 使用Edge最新的浏览器的渲染方式 -->
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <!-- viewport视口:网页可以根据设置的宽度自动进行适配,在浏览器的内部虚拟一个容器,容器的宽度与设备的宽度相同。
    width: 默认宽度与设备的宽度相同
    initial-scale: 初始的缩放比,为1:1 -->
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
    <title>用户信息管理系统</title>

    <!-- 1. 导入CSS的全局样式 -->
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <!-- 2. jQuery导入,建议使用1.9以上的版本 -->
    <script src="js/jquery-2.1.0.min.js"></script>
    <!-- 3. 导入bootstrap的js文件 -->
    <script src="js/bootstrap.min.js"></script>
    <style type="text/css">
        td, th {
     
            text-align: center;
        }
    </style>
</head>
<body>
<div class="container">
    <h3 style="text-align: center">用户信息列表</h3>
    <table border="1" class="table table-bordered table-hover">
        <tr class="success">
            <th>编号</th>
            <th>姓名</th>
            <th>性别</th>
            <th>年龄</th>
            <th>籍贯</th>
            <th>QQ</th>
            <th>邮箱</th>
            <th>操作</th>
        </tr>
        <c:forEach items="${list}" var="ele">
            <tr>
            <!--属性名和getset方法里的要一样-->
                <td>${
     ele.id}</td>
                <td>${
     ele.name}</td>
                <td>${
     ele.sex}</td>
                <td>${
     ele.age}</td>
                <td>${
     ele.address}</td>
                <td>${
     ele.QQnumber}</td>
                <td>${
     ele.email}</td>
                <!--将要修改的学生id传入request域中,让系统后台知道要修改的学生的对象是哪个,并回显到修改页面-->
                <td><a class="btn btn-default btn-sm" href="/stu/bcs?name=${ele.id}" >修改</a>&nbsp;
                    <a class="btn btn-default btn-sm" href="javascript:deletebyid(${ele.id})" >删除</a></td>
            </tr>
        </c:forEach>
        <tr>
            <td colspan="8" align="center"><a class="btn btn-primary" href="addstu.jsp">添加联系人</a></td>
        </tr>
        <!--收集域中的错误信息并显示出来,没有则不显示-->
        ${
     msg}
    </table>
</div>
<script>
    function deletebyid(id) {
     
    <!--弹窗询问-->
        if (confirm("确定删除该学生吗")) {
     
            location.href = "${pageContext.request.contextPath}/removestu?name="+id;
        }
    }
</script>
</body>
</html>
添加学生页面

效果图
简易学生管理系统_web_字符流传输数据_第2张图片

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<!-- 网页使用的语言 -->
<html lang="zh-CN">
<head>
    <!-- 指定字符集 -->
    <meta charset="utf-8">
    <!-- 使用Edge最新的浏览器的渲染方式 -->
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <!-- viewport视口:网页可以根据设置的宽度自动进行适配,在浏览器的内部虚拟一个容器,容器的宽度与设备的宽度相同。
    width: 默认宽度与设备的宽度相同
    initial-scale: 初始的缩放比,为1:1 -->
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
    <title>添加用户</title>

    <!-- 1. 导入CSS的全局样式 -->
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <!-- 2. jQuery导入,建议使用1.9以上的版本 -->
    <script src="js/jquery-3.2.1.min.js"></script>
    <script>
        $(function () {
     
            $("#num").blur(function () {
     
                $.ajax({
     
                    url: "${pageContext.request.contextPath}/stuname",
                    data: "num=" + $(this).val(),
                    success: function (resp) {
     
                    //若输入的学号在库中有重复的,则输入框会变红色,没有则是绿色
                        if (resp == 1) {
     
                            $("#num").removeAttr("style");
                            $("#num").css("border", "3px solid green");
                        } else {
     
                            $("#num").removeAttr("style");
                            $("#num").css("border", "3px solid red");
                        }
                    }
                });
            });
        })
    </script>
    <!-- 3. 导入bootstrap的js文件 -->
</head>
<body>
<div class="container">
    <center><h3>添加联系人页面</h3></center>
    <form action="/stu/addstuser" method="post">
        <div class="form-group">
            <input type="hidden" class="form-control" id="do" name="do" value="addstudent" readonly>
        </div>
        <div class="form-group">
            <label for="num">学号:</label>
            <input type="text" class="form-control" id="num" name="num" placeholder="请输入学号">
        </div>
        <div class="form-group">
            <label for="name">姓名:</label>
            <input type="text" class="form-control" id="name" name="name" placeholder="请输入姓名">
        </div>

        <div class="form-group">
            <label>性别:</label>
            <input type="radio" name="sex" id="sex_man" value="男" checked="checked"/><label for="sex_man"></label>
            <input type="radio" name="sex" id="sex_woman" value="女"/><label for="sex_woman"></label>
        </div>

        <div class="form-group">
            <label for="age">年龄:</label>
            <input type="text" class="form-control" id="age" name="age" placeholder="请输入年龄">
        </div>

        <div class="form-group">
            <label for="address">籍贯:</label>
            <select name="address" class="form-control" id="jiguan">
                <option value="广东">广东</option>
                <option value="广西">广西</option>
                <option value="湖南">湖南</option>
            </select>
        </div>

        <div class="form-group">
            <label for="qq">QQ:</label>
            <input type="text" class="form-control" name="qq" placeholder="请输入QQ号码"/>
        </div>

        <div class="form-group">
            <label for="email">Email:</label>
            <input type="text" class="form-control" name="email" placeholder="请输入邮箱地址"/>
        </div>

        <div class="form-group" style="text-align: center">
            <input class="btn btn-primary" type="submit" value="提交"/>
            <input class="btn btn-default" type="reset" value="重置"/>
            <input class="btn btn-default" type="button" onclick="javascript:goback()" value="返回"/>
        </div>
        ${
     msg}
    </form>
</div>
<script>
    function goback() {
     
        location.href="${pageContext.request.contextPath}/accept"
    }
</script>
</body>
</html>
修改学生信息页面

简易学生管理系统_web_字符流传输数据_第3张图片

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<!-- 网页使用的语言 -->
<html lang="zh-CN">
<head>
    <!-- 指定字符集 -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>修改用户</title>

    <link href="css/bootstrap.min.css" rel="stylesheet">
    <script src="js/jquery-2.1.0.min.js"></script>
    <script src="js/bootstrap.min.js"></script>

</head>
<body>
<div class="container" style="width: 400px;">
    <h3 style="text-align: center;">修改联系人</h3>
    <form action="/stu/change" method="post">
        <div class="form-group">
            <label for="name">姓名:</label>
            <input type="text" class="form-control" id="name" name="name"  readonly="readonly" placeholder="请输入姓名" value="${student.name}"/>
        </div>

        <div class="form-group">
            <label>性别:</label>
            <input type="radio" name="sex" value="男" ${
     student.sex == "男"?"checked":""}/><input type="radio" name="sex" value="女" ${
     student.sex == "女"?"checked":""}/></div>

        <div class="form-group">
            <label for="age">年龄:</label>
            <input type="text" class="form-control" id="age"  name="age" placeholder="请输入年龄" value="${student.age}"/>
        </div>

        <div class="form-group">
            <label for="address">籍贯:</label>
            <select id = "address"name="address" class="form-control" >
            <!--想多加可以复制粘贴改名即可-->
                <option value="广东">广东</option>
                <option value="广西">广西</option>
                <option value="湖南">湖南</option>
            </select>
        </div>


        <div class="form-group">
            <label for="qq">QQ:</label>
            <input  id = "qq" type="text" class="form-control" name="qq" placeholder="请输入QQ号码" value="${student.QQnumber}"/>
        </div>

        <div class="form-group">
            <label for="email">Email:</label>
            <input id = "email"type="text" class="form-control" name="email" placeholder="请输入邮箱地址" value="${student.email}"/>
        </div>

        <div class="form-group" style="text-align: center">
            <input class="btn btn-primary" type="submit" value="提交" />
            <input class="btn btn-default" type="reset" value="重置" />
            <input class="btn btn-default" type="button" onclick="javascript:goback()" value="返回"/>
        </div>
    </form>
    ${
     msg}
</div>
<script>
    <!--将籍贯的下拉栏进行回显-->
    document.querySelectorAll("[value='${student.address}']")[0].selected=true;
    function goback() {
     
        location.href="${pageContext.request.contextPath}/accept"
    }
</script>
</body>
</html>

首先我们先定义一个学生类,7个属性,有无参构造,getter&setter还有tostring()方法:

学生类_java
public class Student {
     
    /*<>编号<姓名<>性别<>年龄<>籍贯<>QQ<>邮箱<>操作<>*/
    String id;
    String name;
    String sex;
    String age;
    String address;
    String QQnumber;
    String email;

    public Student(String id, String name, String sex, String age, String address, String QQnumber, String email) {
     
        this.id = id;
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.address = address;
        this.QQnumber = QQnumber;
        this.email = email;
    }

    public Student() {
     
    }

    @Override
    public String toString() {
     
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", age=" + age +
                ", address='" + address + '\'' +
                ", QQnumber='" + QQnumber + '\'' +
                ", email='" + email + '\'' +
                '}';
    }

    public void setId(String id) {
     
        this.id = id;
    }

    public void setName(String name) {
     
        this.name = name;
    }

    public void setSex(String sex) {
     
        this.sex = sex;
    }

    public void setAge(String age) {
     
        this.age = age;
    }

    public void setAddress(String address) {
     
        this.address = address;
    }

    public void setQQnumber(String QQnumber) {
     
        this.QQnumber = QQnumber;
    }

    public void setEmail(String email) {
     
        this.email = email;
    }

    public String getId() {
     
        return id;
    }

    public String getName() {
     
        return name;
    }

    public String getSex() {
     
        return sex;
    }

    public String getAge() {
     
        return age;
    }

    public String getAddress() {
     
        return address;
    }

    public String getQQnumber() {
     
        return QQnumber;
    }

    public String getEmail() {
     
        return email;
    }
}
过滤类(解决全局的中文乱码问题)

之后在编写其他程序之前,需要保证其他的程序在运行时不会检测到中文乱码,所以事先创建一个过滤类,将编码都转为"utf-8":

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter(filterName = "CharFilter", value = "/*", dispatcherTypes = {
     DispatcherType.FORWARD, DispatcherType.REQUEST})
public class CharFilter implements Filter {
     
    public void destroy() {
     
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
     
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;
        if ("post".equalsIgnoreCase(request.getMethod())) {
     
            request.setCharacterEncoding("utf-8");
        }
        chain.doFilter(req, resp);
    }
    public void init(FilterConfig config) throws ServletException {
     
    }
}
import com.itheima.stu_system.domain.Student;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class StudentDao {
     
    
    public void writeAll(List<Student> studentList) {
     

        try {
     
            BufferedWriter bw = new BufferedWriter(new FileWriter("H:\\JAVA_WORD\\Web_newclass\\student_system\\students.txt"));
            //遍历集合,得到每一个学生对象

            for (Student student : studentList) {
     
                //把学生对象的数据拼接成指定格式的字符串
                String stu_text = student.getId()+","+student.getName()+","+student.getSex()+","+student.getAge()+","+student.getAddress()+","+student.getQQnumber()+","+student.getEmail();
                //调用字符缓冲输出流对象的方法写数据
                bw.write(stu_text);
                bw.newLine();
                bw.flush();
            }
            //释放资源
            bw.close();
        } catch (IOException e) {
     
            e.printStackTrace();
        }
    }

    public List<Student> findAll() {
     
        List<Student> userList =new ArrayList<>();
        try {
     
        	//因为暂时还没学那种可变的路径的写法,所以用绝对路径
        	File file = new File("H:\\JAVA_WORD\\Web_newclass\\student_system\\students.txt");
            if (!file.exists()) {
     
                file.createNewFile();
            }
            BufferedReader br = new BufferedReader(new FileReader("H:\\JAVA_WORD\\Web_newclass\\student_system\\students.txt"));
            String readLine ;
            //读取文件生成学生对象
            while ((readLine=br.readLine()) != null) {
     
                String[] split = readLine.split(",");
                Student student = new Student();
                student.setId(split[0]);
                student.setName(split[1]);
                student.setSex(split[2]);
                student.setAge(split[3]);
                student.setAddress(split[4]);
                student.setQQnumber(split[5]);
                student.setEmail(split[6]);
                //将学生对象加入到集合中
                userList.add(student);
            }
        } catch (FileNotFoundException e) {
     
            e.printStackTrace();
        } catch (IOException e) {
     
            e.printStackTrace();
            return null;
        }
        return userList;
    }
}
ServiceServlet

做增删改查操作的地方,拿数据库的信息和前面传入的信息进行操作,之后数据库的返回数据库,传入的信息处理后返回一个返回值

import com.itheima.stu_system.domain.Student;

import java.util.List;

public class ServiceServlet {
     
    StudentDao studentDao = new StudentDao();
    
    /*
        查找所有学生信息
    */
    public List<Student> findAll() {
     

        List<Student> studentList=studentDao.findAll();
        return studentList;
    }
    
    /*
        添加学生信息
    */
    public List<Student> addStudent(Student student) {
     
        List<Student> studentList = studentDao.findAll();
        studentList.add(student);
        studentDao.writeAll(studentList);
        return studentList;
    }

    /*
        删除学生信息
    */
    public List<Student> remove(String id) {
     
        StudentDao studentDao = new StudentDao();
        List<Student> studentList = studentDao.findAll();
        for (Student student : studentList) {
     
            if (student.getId().equals(id)) {
     
                studentList.remove(student);
                break;
            }
        }
        studentDao.writeAll(studentList);
        return studentList;
    }
    
    /*
        修改学生信息
    */
    public List<Student> changeStudent(Student student) {
     
        StudentDao studentDao = new StudentDao();
        List<Student> students = studentDao.findAll();
        String id = student.getId();
        
        for (int i = 0; i < students.size(); i++) {
     
            Student stu = students.get(i);
            if (stu.getId().equals(id)) {
     
                student.setName(stu.getName());
                student.setId(id);
                students.set(i, student);
                break;
            }
        }
        studentDao.writeAll(students);
        return students;
    }
    
    /*
        查重
    */
    
    public boolean judge(String id) {
     
        StudentDao studentDao = new StudentDao();
        List<Student> students = studentDao.findAll();
        for (Student student : students) {
     
            if (student.getId().equals(id)) {
     
                return false;
            }
        }
        return true;
    }
    
    /*
        根据查找对应学生对象
    */
    public Student getStudent(String id) {
     
        StudentDao studentDao = new StudentDao();
        List<Student> all = studentDao.findAll();
        for (Student student : all) {
     
            if (id.equals(student.getId())) {
     
                return student;
            }
        }
        return new Student();
    }
}

以上是针对全局的

accept页面:

在这里可以事先在idea设置里设置一下doGet和doPost方法

import com.itheima.stu_system.domain.Student;
import com.itheima.stu_system.utils.ServiceServlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

public class AcceptServlet extends HttpServlet {
     
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     
        //接收到查看学生信息的请求
        //创建service对应类的对象
        ServiceServlet serviceServlet = new ServiceServlet();
        //获得session域的对象
        HttpSession session = request.getSession();
        //调用service对应类的有查找功能的方法
        List<Student> studentList = serviceServlet.findAll();
        //将上一步调用方法得到的学生列表的集合传入session域中
        session.setAttribute("list", studentList);
        //之后重定向回学生信息列表的页面
        response.sendRedirect("http://localhost:2333${pageContext.request.contextPath}/studentlist.jsp");
    }
}
添加学生信息

首先先要从request域中获取到学生的各项信息,之后进行判断信息是否合规,若不合规要弹回添加页面并提示用户,合规的话继续进行赋值,并在赋值后将学生对象传给serviceServlet进行添加处理,得到一个添加后的集合对象,将该对象传入域中,最后跳回学生列表页面

查重

在鼠标移开输入学号一栏时进行判断数据库中是否有一样的,之后传给添加学生的jsp页面

import com.itheima.stu_system.utils.ServiceServlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.Service;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

public class DetectionServlet extends HttpServlet {
     
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     
        doGet(request, response);
    }
    /*
        查重复id
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     
        try {
     
            String num = request.getParameter("num");
            ServiceServlet serviceServlet = new ServiceServlet();
            Boolean flag = serviceServlet.judge(num);
            response.getWriter().print(flag?1:0);

        } catch (UnsupportedEncodingException e) {
     
            e.printStackTrace();
        }
    }
}
添加学生信息
import com.itheima.stu_system.domain.Student;
import com.itheima.stu_system.utils.ServiceServlet;
import com.itheima.stu_system.utils.StudentDao;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
/*
    添加学生信息
*/
public class AddStudentServlet extends HttpServlet {
     
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     
        
        Student student = new Student();
        /*从request域中获取id之后,传入serviServlet中进行查重,若数据库中已有改id则返回页面提示用户该id不可用*/
        String id = request.getParameter("num");
        ServiceServlet serviceServlet = new ServiceServlet();
        boolean flag = serviceServlet.judge(id);
        if (!flag) {
     
            request.setAttribute("msg", "该学号已存在,请确认后重试");
            request.getRequestDispatcher("/addstu.jsp").forward(request, response);
        } else {
     
        //若不重复,从request域中获取各种name的value值,并将value值设置给学生对象各变量值
            student.setId(id);
            student.setName(request.getParameter("name"));
            student.setSex(request.getParameter("sex"));
            //判断年龄的数字或用户输入的格式是否正确,若不正确,则弹回原页面
            if (!(Integer.parseInt(request.getParameter("age")) < 150 && Integer.parseInt(request.getParameter("age")) > 0)) {
     
                request.setAttribute("msg", "请输入正确的年龄(阿拉伯数字)");
                request.getRequestDispatcher("/addstu.jsp").forward(request,response);
            } else {
     
            //若正确,继续对学生对象进行赋值                student.setAge(request.getParameter("age"));
                student.setAddress(request.getParameter("address"));
                student.setQQnumber(request.getParameter("qq"));
                student.setEmail(request.getParameter("email"));

                //将封装好的学生对象通过方法交给ServiceServlet处理,返回一个进行添加操作后的学生集合
                List<Student> studentList_add = serviceServlet.addStudent(student);
                //获取session域的对象
                HttpSession session = request.getSession();
                //将返回的进行添加操作后的学生集合对象添加到session域中并返回(转发回)学生列表页面
                session.setAttribute("list", studentList_add);
                response.sendRedirect("http://localhost:2333/stu/studentlist.jsp");
            }
        }
    }
}

修改学生信息
回显
import com.itheima.stu_system.domain.Student;
import com.itheima.stu_system.utils.ServiceServlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
/*
    回显修改学生的信息
*/
public class BeforeChangeServlet extends HttpServlet {
     
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     
        //从request域中获取到用户想要修改的学生的id
        String id = request.getParameter("name");
        ServiceServlet serviceServlet = new ServiceServlet();
        //利用getStudent方法拿到数据库中的id对应的学生对象
        Student student =serviceServlet.getStudent(id);
        //获取context域的对象
        ServletContext servletContext = request.getServletContext();
        //将该学生对象的id传入context域中
        servletContext.setAttribute("changeid",id);
        //将student对象传入context域中
        servletContext.setAttribute("student",student);
        //重定向到修改页面(回显)
        response.sendRedirect("http://localhost:2333/stu/update.jsp");
    }
}
修改学生信息
import com.itheima.stu_system.domain.Student;
import com.itheima.stu_system.utils.ServiceServlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/*
    修改学生信息
*/
public class ChangeServlet extends HttpServlet {
     
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     
        
        Student student = new Student();
        //获取context域对象
        ServletContext servletContext = request.getServletContext();
        //从context域中获取学生id
        String changeid = (String) servletContext.getAttribute("changeid");
        
        //对学生对象赋值
        student.setId(changeid);
        student.setName("待添加");
        student.setSex(request.getParameter("sex"));
        if (!(Integer.parseInt(request.getParameter("age")) < 150 && Integer.parseInt(request.getParameter("age")) > 0)) {
     
            request.setAttribute("msg", "请输入正确的年龄(阿拉伯数字)");
            request.getRequestDispatcher("/update.jsp");
        }
        student.setAge(request.getParameter("age"));
        student.setAddress(request.getParameter("address"));
        student.setQQnumber(request.getParameter("qq"));
        student.setEmail(request.getParameter("email"));

        
        ServiceServlet serviceServlet = new ServiceServlet();
        //交给serviceServlet处理后得到一个修改后的集合对象
        List<Student> studentList_change = serviceServlet.changeStudent(student);
        HttpSession session = request.getSession();
        //将集合对象传入session域中
        session.setAttribute("list", studentList_change);
        //重定向回学生列表页面
        response.sendRedirect("http://localhost:2333/stu/studentlist.jsp");
    }
}
删除学生信息
import com.itheima.stu_system.domain.Student;
import com.itheima.stu_system.utils.ServiceServlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
/*
    删除学生信息
*/

public class RemoveStudentsServlet extends HttpServlet {
     
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     
        String id = request.getParameter("name");
        ServiceServlet serviceServlet = new ServiceServlet();
        //调用删除方法,传入id
        List<Student> studentList = serviceServlet.remove(id);

        HttpSession session = request.getSession();
        //向域中传入删除信息后的集合,并转回学生列表页面
        session.setAttribute("list",studentList);
        request.getRequestDispatcher("studentlist.jsp").forward(request,response);
    }
}

你可能感兴趣的:(jsp,web,servlet,javaee)