用jsp wed写一个阶级计算器
时间: 2024-09-28 09:09:46 浏览: 37
在JSP(JavaServer Pages)中创建一个简单的班级计算器,你可以设计一个网页界面,用户可以输入学生的分数,然后计算平均分、最高分和最低分。以下是一个基本步骤:
1. **HTML部分**:
```html
<!DOCTYPE html>
<html>
<head>
<title>班级成绩计算器</title>
</head>
<body>
<form action="scoreCalculator.jsp" method="post">
学生分数:<input type="number" name="studentScore"><br>
<input type="submit" value="提交">
</form>
<hr>
<h2>计算结果:</h2>
<p>平均分: <span id="averageScore"></span></p>
<p>最高分: <span id="highestScore"></span></p>
<p>最低分: <span id="lowestScore"></span></p>
</body>
</html>
```
2. **JSP部分 (scoreCalculator.jsp)**:
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
double totalScore = 0;
int studentCount = 0;
double highestScore = Double.MIN_VALUE;
double lowestScore = Double.MAX_VALUE;
if (request.getParameter("studentScore") != null) {
double currentScore = Double.parseDouble(request.getParameter("studentScore"));
totalScore += currentScore;
studentCount++;
if (currentScore > highestScore) {
highestScore = currentScore;
}
if (currentScore < lowestScore) {
lowestScore = currentScore;
}
}
double averageScore = totalScore / studentCount;
out.println("<script>");
out.println("document.getElementById('averageScore').innerHTML = " + averageScore + ";");
out.println("document.getElementById('highestScore').innerHTML = " + highestScore + ";");
out.println("document.getElementById('lowestScore').innerHTML = " + lowestScore + ";");
out.println("</script>");
%>
```
在这个例子中,用户提交表单后,服务器端的JSP页面会获取并累加分数,并更新显示的结果。
阅读全文