设计一个实现等差等比级数和的web应用程序 任务目的:掌握MVC模式三部分的设计,使用request bean存储数据,使用servlet处理数据,并将有关数据存储到request bean ,使用jsp页面显示request.bean数据 任务要求: (1)编写一个创建bean 的类, 该类可以储存等差(等比)级数的首项、公差(公比)和等差(等比)级数的和。 (2)编写一个JSP 页面inputNumber. jsp 。该页面提供form 表单,该form 表单提供用户输人等差(等比)级数的首和公差(公
时间: 2023-06-27 16:04:33 浏览: 267
比)的输入框。 (3)编写一个servlet,该servlet 接受来自inputNumber.jsp 页面的数据,并使用创建bean 的类计算等差(等比)级数的和,将有关数据存储到request bean 中。 (4)编写一个JSP 页面displayResult. jsp ,该页面显示request bean 中的数据。
首先,我们需要创建一个名为"ArithmeticSeriesBean"的Java类,该类将储存等差(等比)级数的首项、公差(公比)和等差(等比)级数的和。以下是该类的代码:
```java
public class ArithmeticSeriesBean {
private double firstTerm;
private double commonDifference;
private double seriesSum;
public double getFirstTerm() {
return firstTerm;
}
public void setFirstTerm(double firstTerm) {
this.firstTerm = firstTerm;
}
public double getCommonDifference() {
return commonDifference;
}
public void setCommonDifference(double commonDifference) {
this.commonDifference = commonDifference;
}
public double getSeriesSum() {
return seriesSum;
}
public void setSeriesSum(double seriesSum) {
this.seriesSum = seriesSum;
}
// 计算等差级数和
public void calculateArithmeticSeriesSum() {
seriesSum = (2 * firstTerm + (10 - 1) * commonDifference) * 10 / 2;
}
// 计算等比级数和
public void calculateGeometricSeriesSum() {
seriesSum = firstTerm * (1 - Math.pow(commonDifference, 10)) / (1 - commonDifference);
}
}
```
接下来,我们需要创建一个名为"InputNumber.jsp"的JSP页面,该页面提供form表单,该form表单提供用户输入等差(等比)级数的首项和公差(公比)的输入框。以下是该页面的代码:
```html
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>输入等差(等比)级数的首项和公差(公比)</title>
</head>
<body>
<h1>输入等差(等比)级数的首项和公差(公比)</h1>
<form action="ArithmeticSeriesServlet" method="post">
<label for="firstTerm">首项:</label><input type="number" name="firstTerm" id="firstTerm" required><br><br>
<label for="commonDifference">公差(公比):</label><input type="number" name="commonDifference" id="commonDifference" required><br><br>
<input type="submit" value="计算">
</form>
</body>
</html>
```
然后,我们需要创建一个名为"ArithmeticSeriesServlet"的servlet,该servlet接受来自InputNumber.jsp页面的数据,并使用创建bean的类计算等差(等比)级数的和,将有关数据存储到request bean中。以下是该servlet的代码:
```java
@WebServlet("/ArithmeticSeriesServlet")
public class ArithmeticSeriesServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
double firstTerm = Double.parseDouble(request.getParameter("firstTerm"));
double commonDifference = Double.parseDouble(request.getParameter("commonDifference"));
ArithmeticSeriesBean arithmeticSeriesBean = new ArithmeticSeriesBean();
arithmeticSeriesBean.setFirstTerm(firstTerm);
arithmeticSeriesBean.setCommonDifference(commonDifference);
if (commonDifference == 0) {
arithmeticSeriesBean.setSeriesSum(firstTerm * 10);
} else {
arithmeticSeriesBean.calculateArithmeticSeriesSum();
}
request.setAttribute("arithmeticSeriesBean", arithmeticSeriesBean);
RequestDispatcher dispatcher = request.getRequestDispatcher("DisplayResult.jsp");
dispatcher.forward(request, response);
}
}
```
最后,我们需要创建一个名为"DisplayResult.jsp"的JSP页面,该页面显示request bean中的数据。以下是该页面的代码:
```html
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>等差(等比)级数和的计算结果</title>
</head>
<body>
<h1>等差(等比)级数和的计算结果</h1>
<p>首项:<%= request.getAttribute("arithmeticSeriesBean").getFirstTerm() %></p>
<p>公差(公比):<%= request.getAttribute("arithmeticSeriesBean").getCommonDifference() %></p>
<p>等差(等比)级数和:<%= request.getAttribute("arithmeticSeriesBean").getSeriesSum() %></p>
</body>
</html>
```
现在我们已经完成了整个web应用程序的设计和开发。用户可以在InputNumber.jsp页面输入等差(等比)级数的首项和公差(公比),然后单击"计算"按钮。servlet将获取用户输入的值,并使用创建bean的类计算等差(等比)级数的和,并将有关数据存储到request bean中。最后,DisplayResult.jsp页面将显示request bean中的数据。
阅读全文