综合实践与项目拓展02

  1. 编写新的 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"%>

<head> <meta charset="UTF-8"> <title>评估指标列表</title> </head> <body> <h1>评估指标列表</h1> <form action="AssessmentMetricServlet" method="post"> <label for="metricName">指标名称:</label> <input type="text" id="metricName" name="metricName" required><br> <label for="metricDescription">指标描述:</label> <textarea id="metricDescription" name="metricDescription"></textarea><br> <label for="scoringRules">评分规则:</label> <textarea id="scoringRules" name="scoringRules"></textarea><br> <label for="weight">权重:</label> <input type="number" id="weight" name="weight" step="0.01" required><br> <input type="submit" value="添加指标"> </form> <table border="1"> <tr> <th>指标 ID</th> <th>指标名称</th> <th>指标描述</th> <th>评分规则</th> <th>权重</th> </tr> <% java.util.List<AssessmentMetric> metrics = (java.util.List<AssessmentMetric>) request.getAttribute("metrics"); if (metrics != null) { for (AssessmentMetric metric : metrics) { %> <tr> <td><%= metric.getMetricId() %></td> <td><%= metric.getMetricName() %></td> <td><%= metric.getMetricDescription() %></td> <td><%= metric.getScoringRules() %></td> <td><%= metric.getWeight() %></td> </tr> <% } } %> </table> </body> `
posted @ 2025-02-19 17:08  七分之一月  阅读(27)  评论(0)    收藏  举报
//雪花飘落效果