学生信息管理系统javaweb源代码
时间: 2024-04-27 12:16:25 浏览: 138
学生信息管理系统是一个常见的项目,用于管理学生的基本信息、成绩等。下面是一个简单的学生信息管理系统的JavaWeb源代码示例:
```java
// Student.java
public class Student {
private String id;
private String name;
private int age;
private String gender;
// 构造方法
public Student(String id, String name, int age, String gender) {
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
}
// getter和setter方法
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
```
```java
// StudentDao.java
import java.util.ArrayList;
import java.util.List;
public class StudentDao {
private List<Student> students;
public StudentDao() {
students = new ArrayList<>();
}
// 添加学生
public void addStudent(Student student) {
students.add(student);
}
// 根据学号查找学生
public Student getStudentById(String id) {
for (Student student : students) {
if (student.getId().equals(id)) {
return student;
}
}
return null;
}
// 获取所有学生
public List<Student> getAllStudents() {
return students;
}
}
```
```java
// StudentServlet.java
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 static final long serialVersionUID = 1L;
private StudentDao studentDao;
public void init() {
studentDao = new StudentDao();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String action = request.getServletPath();
switch (action) {
case "/add":
showAddForm(request, response);
break;
case "/insert":
insertStudent(request, response);
break;
default:
listStudents(request, response);
break;
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
private void showAddForm(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.getRequestDispatcher("add-student.jsp").forward(request, response);
}
private void insertStudent(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String id = request.getParameter("id");
String name = request.getParameter("name");
int age = Integer.parseInt(request.getParameter("age"));
String gender = request.getParameter("gender");
Student student = new Student(id, name, age, gender);
studentDao.addStudent(student);
response.sendRedirect("list");
}
private void listStudents(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
List<Student> students = studentDao.getAllStudents();
request.setAttribute("students", students);
request.getRequestDispatcher("list-students.jsp").forward(request, response);
}
}
```
以上是一个简单的学生信息管理系统的JavaWeb源代码示例,包括了学生类、数据访问对象类和Servlet类。你可以根据自己的需求进行修改和扩展。
阅读全文