写一段代码,运用servlet,实现“评教”功能,给教师打分,分数存储到数据库,要求前端页面能够显示数据库已有的教师姓名,且实现打分和提交功能
时间: 2024-03-27 19:41:07 浏览: 78
基于jsp+servlet+layui教师科研论文管理系统+源代码+文档说明+数据库.zip
好的,以下给出一个简单的示例代码实现。
首先,我们需要创建一个名为"TeacherEvaluationServlet"的servlet,用于处理评教相关请求。接着,我们需要在web.xml文件中将该servlet映射到特定的URL上,如下所示:
```xml
<servlet>
<servlet-name>TeacherEvaluationServlet</servlet-name>
<servlet-class>com.example.TeacherEvaluationServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TeacherEvaluationServlet</servlet-name>
<url-pattern>/teacher-evaluation</url-pattern>
</servlet-mapping>
```
接下来,我们需要编写TeacherEvaluationServlet类的doGet和doPost方法,用于处理GET和POST请求。在doGet方法中,我们需要从数据库中读取教师姓名列表,并将其显示在前端页面上。
在doPost方法中,我们需要从请求参数中获取教师姓名和评分,并将其存储到数据库中。
以下是示例代码:
```java
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
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 TeacherEvaluationServlet extends HttpServlet {
private static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
private static final String DB_URL = "jdbc:mysql://localhost:3306/teacher_evaluation";
private static final String USER = "root";
private static final String PASS = "root";
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 从数据库中读取教师姓名列表
List<String> names = new ArrayList<>();
try {
Class.forName(JDBC_DRIVER);
Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
String sql = "SELECT DISTINCT name FROM teachers";
PreparedStatement stmt = conn.prepareStatement(sql);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
String name = rs.getString("name");
names.add(name);
}
rs.close();
stmt.close();
conn.close();
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
}
// 将教师姓名列表传递到前端页面
req.setAttribute("names", names);
req.getRequestDispatcher("/teacher-evaluation.jsp").forward(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 从请求参数中获取教师姓名和评分
String name = req.getParameter("name");
int score = Integer.parseInt(req.getParameter("score"));
// 将评分存储到数据库中
try {
Class.forName(JDBC_DRIVER);
Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
String sql = "INSERT INTO scores (name, score) VALUES (?, ?)";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setString(1, name);
stmt.setInt(2, score);
stmt.executeUpdate();
stmt.close();
conn.close();
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
}
// 返回成功响应
resp.getWriter().write("评分成功");
}
}
```
最后,我们需要编写一个名为"teacher-evaluation.jsp"的JSP页面,用于显示教师姓名列表和评分表单,并实现提交评分功能。以下是示例代码:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>教师评教</title>
</head>
<body>
<h1>教师评教</h1>
<form method="POST" action="/teacher-evaluation">
<label for="name">教师姓名:</label>
<select id="name" name="name">
<c:forEach var="name" items="${names}">
<option value="${name}">${name}</option>
</c:forEach>
</select>
<br><br>
<label for="score">评分:</label>
<input type="number" id="score" name="score" min="0" max="100" required>
<br><br>
<input type="submit" value="提交">
</form>
</body>
</html>
```
在这个JSP页面中,我们使用了JSTL标签库来动态生成教师姓名列表。评分表单中包含一个下拉框和一个输入框,用于选择教师和输入评分。提交按钮用于提交评分表单。
阅读全文