请用java web 输出学生管理系统
时间: 2023-05-12 12:03:32 浏览: 110
java web 学生管理系统
非常感谢您的提问。以下是一个简单的 Java Web 学生管理系统的示例代码:
1. 首先,我们需要创建一个名为 Student 的 Java 类,用于存储学生的信息:
public class Student {
private int id;
private String name;
private int age;
private String gender;
private String major;
public Student(int id, String name, int age, String gender, String major) {
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
this.major = major;
}
// Getters and setters for all fields
}
2. 接下来,我们需要创建一个名为 StudentDao 的 Java 类,用于处理学生信息的持久化:
import java.util.ArrayList;
import java.util.List;
public class StudentDao {
private static List<Student> students = new ArrayList<>();
public void addStudent(Student student) {
students.add(student);
}
public List<Student> getAllStudents() {
return students;
}
public Student getStudentById(int id) {
for (Student student : students) {
if (student.getId() == id) {
return student;
}
}
return null;
}
public void updateStudent(Student student) {
for (int i = 0; i < students.size(); i++) {
if (students.get(i).getId() == student.getId()) {
students.set(i, student);
return;
}
}
}
public void deleteStudent(int id) {
students.removeIf(student -> student.getId() == id);
}
}
3. 然后,我们需要创建一个名为 StudentServlet 的 Java 类,用于处理 HTTP 请求:
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class StudentServlet extends HttpServlet {
private StudentDao studentDao = new StudentDao();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String action = request.getParameter("action");
if (action == null) {
List<Student> students = studentDao.getAllStudents();
request.setAttribute("students", students);
request.getRequestDispatcher("index.jsp").forward(request, response);
} else if (action.equals("edit")) {
int id = Integer.parseInt(request.getParameter("id"));
Student student = studentDao.getStudentById(id);
request.setAttribute("student", student);
request.getRequestDispatcher("edit.jsp").forward(request, response);
} else if (action.equals("delete")) {
int id = Integer.parseInt(request.getParameter("id"));
studentDao.deleteStudent(id);
response.sendRedirect(request.getContextPath() + "/");
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
String name = request.getParameter("name");
int age = Integer.parseInt(request.getParameter("age"));
String gender = request.getParameter("gender");
String major = request.getParameter("major");
Student student = new Student(id, name, age, gender, major);
studentDao.updateStudent(student);
response.sendRedirect(request.getContextPath() + "/");
}
}
4. 最后,我们需要创建两个 JSP 页面,用于显示学生列表和编辑学生信息:
index.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>学生管理系统</title>
</head>
<body>
<h1>学生列表</h1>
<table>
<thead>
<tr>
<th>ID</th>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
<th>专业</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<c:forEach items="${students}" var="student">
<tr>
<td>${student.id}</td>
<td>${student.name}</td>
<td>${student.age}</td>
<td>${student.gender}</td>
<td>${student.major}</td>
<td>
<a href="${pageContext.request.contextPath}/?action=edit&id=${student.id}">编辑</a>
<a href="${pageContext.request.contextPath}/?action=delete&id=${student.id}">删除</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
<a href="${pageContext.request.contextPath}/edit.jsp">添加学生</a>
</body>
</html>
edit.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>编辑学生信息</title>
</head>
<body>
<h1>编辑学生信息</h1>
<form method="post">
<input type="hidden" name="id" value="${student.id}">
<label>姓名:</label>
<input type="text" name="name" value="${student.name}"><br>
<label>年龄:</label>
<input type="number" name="age" value="${student.age}"><br>
<label>性别:</label>
<input type="radio" name="gender" value="男" ${student.gender == '男' ? 'checked' : ''}>男
<input type="radio" name="gender" value="女" ${student.gender == '女' ? 'checked' : ''}>女<br>
<label>专业:</label>
<input type="text" name="major" value="${student.major}"><br>
<input type="submit" value="保存">
</form>
<a href="${pageContext.request.contextPath}/">返回</a>
</body>
</html>
以上就是一个简单的 Java Web 学生管理系统的示例代码。希望能对您有所帮助!
阅读全文