【08 javaweb 简单demo案例实现】

简单demo案例实现

  • 1. 需求
  • 2 代码

1. 需求

【08 javaweb 简单demo案例实现】_第1张图片

2 代码

前端:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录页面</title>
</head>
<body>

<form action="/jou/add" method="get" autocomplete="off">
  学生姓名<input type="text" name="username"><br/>
  学生年龄<input type="text" name="age"><br/>
  学生成绩<input type="text" name="score"><br/>
  <br/>
  <button type="submit">保存</button>



</form>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>学生管理系统首页</title>
</head>
<body>

<a href="/jou/addStudent.html">添加学生</a>
<a href="/jou/listStudentServlet" >查看学生</a>

</body>
</html>

后端:


public class Student {
  private   String username;
  private   int age;
  private   int score;

    @Override
    public String toString() {
        return "Student{" +
                "username='" + username + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }

    public Student() {
    }

    public Student(String username, int age, int score) {
        this.username = username;
        this.age = age;
        this.score = score;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public int getAge() {
        return age;
    }

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

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }
}

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.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

@WebServlet("/add")
public class AddStudent 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, Integer.parseInt(age), Integer.parseInt(score));
        //3.将学生对象的数据保存到d:\\stu.txt
        BufferedWriter bw=new BufferedWriter(new FileWriter("d:\\stu.txt",true));
        bw.write(student.getUsername()+","+student.getAge()+","+student.getScore());
        bw.newLine();
        bw.close();
        //4.通过定时刷新功能响应给浏览器
        resp.setContentType("text/html;charset=UTF-8");
        resp.getWriter().write("添加成功。2秒后自动跳转到首页");
        resp.setHeader("Refresh","2;URL=/jou/index.html");




    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }
}

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.*;
import java.util.ArrayList;

/**
 * @PackageName:com.example.demo0416
 * @ClassName: listStudentServlet
 * @Description:
 * @Author: soulcoder zhufanzhi
 * @date: 2023/4/16 22:55
 **/
@WebServlet("/listStudentServlet")
public class ListStudentServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //1. 创建字符输入流对象 关联读取文件
        BufferedReader br=new BufferedReader(new FileReader("d:\\stu.txt"));
        //2. 创建集合对象 用于保存Student对象
        ArrayList<Student > list=new ArrayList<>();
        //3.循环读取文件中的数据 将数据封装到Student 对象中 再把多个学生对象添加到集合中
        String line;
        while ((line=br.readLine())!=null){
            // 张三 18 100
            Student stu=new Student();
            String[] arr = line.split(",");
            stu.setUsername(arr[0]);
            stu.setAge(Integer.parseInt(arr[1]));
            stu.setScore(Integer.parseInt(arr[2]));
            list.add(stu);

        }

        //4.遍历集合 将数据响应到浏览器
        //获取输出流对象
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter pw = resp.getWriter();
        for (Student s:list){
            pw.write(s.getUsername()+","+s.getAge()+","+s.getScore());
            pw.write("
"
); } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doGet(req, resp); } }

你可能感兴趣的:(JavaWeb,java)