servlet实现学生管理系统
项目结构
-
封装Student类
package com.example.reqresp_test.bean; public class Student { private String username; private String age; private String score; @Override public String toString() { return "Student{" + "username='" + username + '\'' + ", age='" + age + '\'' + ", score='" + score + '\'' + '}'; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getScore() { return score; } public void setScore(String score) { this.score = score; } public Student(String username, String age, String score) { this.username = username; this.age = age; this.score = score; } public Student() { } }
-
添加学生的servlet类AddStudentServlet
package com.example.reqresp_test.servlet; import com.example.reqresp_test.bean.Student; 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 java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; @WebServlet("/addStudentServlet") public class AddStudentServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //1.获取表单中的数据 String username = req.getParameter("username"); String age = req.getParameter("age"); String score = req.getParameter("score"); //2.获取学生对象并赋值 Student student = new Student(username, age, score); //3.将学生对象保存到文件中 String realPath = getServletContext().getRealPath("/resource/stu.txt"); System.out.println(realPath); BufferedWriter writer = new BufferedWriter(new FileWriter(realPath, true)); writer.write(student.getUsername() + "," + student.getAge() + "," + student.getScore()); writer.newLine(); writer.close(); //4.通过定时刷新功能响应给浏览器 resp.setContentType("text/html;charset=UTF-8"); resp.getWriter().write("添加成功,两秒后跳转页面......"); resp.setHeader("Refresh", "2;URL=/stu/index.html"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req,resp); } }
- req.getParameter(“username”)获取表格中的数据
- new FileWriter(realPath, true)以追加的方式添加数据
- resp.setContentType(“text/html;charset=UTF-8”)防止浏览器乱码
- resp.setHeader(“Refresh”, “2;URL=/stu/index.html”);刷新页面
-
查看学生的servlet类ListStudentServlet
package com.example.reqresp_test.servlet; import com.example.reqresp_test.bean.Student; 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 java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; @WebServlet("/listStudentServlet") public class ListStudentServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //1.创建字符输入流对象 String realPath = getServletContext().getRealPath("/resource/stu.txt"); BufferedReader in = new BufferedReader(new FileReader(realPath)); //2.创建集合对象,用来保存student对象 ArrayList<Student> students = new ArrayList<>(); //3.循环读取文件中的数据,将数据封装到student对象中,再把多个对象添加到集合中去 String line; while ((line=in.readLine())!=null){ Student student = new Student(); String[] split = line.split(","); student.setUsername(split[0]); student.setAge(split[1]); student.setScore(split[2]); students.add(student); } //4.遍历集合,将数据相应给浏览器 resp.setContentType("text/html;charset=UTF-8"); PrintWriter writer = resp.getWriter(); for (Student s : students) { writer.write(s.getUsername() + "," + s.getAge() + "," +s.getScore()); writer.write("<br>"); } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { } }
1.getServletContext().getRealPath("/resource/stu.txt")获取servlet的真实路径
2.new BufferedReader(new FileReader(realPath))根据真实路径获取输入流
3.line.split(",")对相关的对象进行切分赋值
-
添加主页
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>学生管理系统首页</title> </head> <body> <a href="/stu/addStudent.html">添加学生</a> <a href="/stu/listStudentServlet">查看学生</a> </body> </html>
-
添加学生管理页面
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>添加学生</title> </head> <body> <form action="/stu/addStudentServlet" method="get" autocomplete="off"> 学生姓名:<input type="text" name="username"><br> 学生年龄:<input type="number" name="age"><br> 学生成绩:<input type="number" name="score"><br> <button type="submit">添加</button> </form> </body> </html>
最后只需要把文件保存改为数据库存储即可