综合实践与项目拓展02
- 编写新的 Servlet
`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.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
// 定义 Servlet 映射路径
@WebServlet("/AssessmentMetricServlet")
public class AssessmentMetricServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final String DB_URL = "jdbc:mysql://localhost:3306/elderly_assessment";
private static final String DB_USER = "your_username";
private static final String DB_PASSWORD = "your_password";
// 处理 GET 请求,用于显示所有评估指标
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
List<AssessmentMetric> metrics = new ArrayList<>();
try (Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM assessment_metrics")) {
while (rs.next()) {
int metricId = rs.getInt("metric_id");
String metricName = rs.getString("metric_name");
String metricDescription = rs.getString("metric_description");
String scoringRules = rs.getString("scoring_rules");
double weight = rs.getDouble("weight");
metrics.add(new AssessmentMetric(metricId, metricName, metricDescription, scoringRules, weight));
}
} catch (SQLException e) {
e.printStackTrace();
}
// 将评估指标列表存入请求属性
request.setAttribute("metrics", metrics);
// 转发到 JSP 页面
request.getRequestDispatcher("assessmentMetrics.jsp").forward(request, response);
}
// 处理 POST 请求,用于添加新的评估指标
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String metricName = request.getParameter("metricName");
String metricDescription = request.getParameter("metricDescription");
String scoringRules = request.getParameter("scoringRules");
double weight = Double.parseDouble(request.getParameter("weight"));
try (Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
PreparedStatement pstmt = conn.prepareStatement(
"INSERT INTO assessment_metrics (metric_name, metric_description, scoring_rules, weight) VALUES (?,?,?,?)")) {
pstmt.setString(1, metricName);
pstmt.setString(2, metricDescription);
pstmt.setString(3, scoringRules);
pstmt.setDouble(4, weight);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
// 重定向到显示所有评估指标的页面
response.sendRedirect("AssessmentMetricServlet");
}
}
// 评估指标实体类
class AssessmentMetric {
private int metricId;
private String metricName;
private String metricDescription;
private String scoringRules;
private double weight;
public AssessmentMetric(int metricId, String metricName, String metricDescription, String scoringRules,
double weight) {
this.metricId = metricId;
this.metricName = metricName;
this.metricDescription = metricDescription;
this.scoringRules = scoringRules;
this.weight = weight;
}
// Getters 和 Setters 方法
public int getMetricId() {
return metricId;
}
public void setMetricId(int metricId) {
this.metricId = metricId;
}
public String getMetricName() {
return metricName;
}
public void setMetricName(String metricName) {
this.metricName = metricName;
}
public String getMetricDescription() {
return metricDescription;
}
public void setMetricDescription(String metricDescription) {
this.metricDescription = metricDescription;
}
public String getScoringRules() {
return scoringRules;
}
public void setScoringRules(String scoringRules) {
this.scoringRules = scoringRules;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
}2. 编写相应的 JSP 页面<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

浙公网安备 33010602011771号