编写一个JSP页面,该页面提供一个表单,用户可以通过表单输入梯形的上底、下底和高的值,并提交给本JSP页面,该JSP页面将计算梯形面积的任务交给一个page bean去完成。JSP页面使用getProperty动作标记显示page bean中的数据,例如梯形的面积
时间: 2023-11-22 19:52:29 浏览: 129
以下是一个简单的实现,包含JSP页面和page bean:
梯形计算器页面(trapezoid.jsp):
```
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>梯形计算器</title>
</head>
<body>
<h1>梯形计算器</h1>
<form method="post" action="${pageContext.request.contextPath}/TrapezoidCalculator">
<label for="upperBase">上底:</label>
<input type="number" name="upperBase" id="upperBase" required><br>
<label for="lowerBase">下底:</label>
<input type="number" name="lowerBase" id="lowerBase" required><br>
<label for="height">高:</label>
<input type="number" name="height" id="height" required><br>
<button type="submit">计算</button>
</form>
<c:if test="${not empty calculator}">
<p>梯形面积为<c:out value="${calculator.area}" /></p>
</c:if>
</body>
</html>
```
梯形计算器page bean(TrapezoidCalculator.java):
```
package com.example;
public class TrapezoidCalculator {
private double upperBase;
private double lowerBase;
private double height;
private double area;
public void calculate() {
area = (upperBase + lowerBase) * height / 2;
}
public double getUpperBase() {
return upperBase;
}
public void setUpperBase(double upperBase) {
this.upperBase = upperBase;
}
public double getLowerBase() {
return lowerBase;
}
public void setLowerBase(double lowerBase) {
this.lowerBase = lowerBase;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getArea() {
return area;
}
}
```
梯形计算器Servlet(TrapezoidCalculatorServlet.java):
```
package com.example;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/TrapezoidCalculator")
public class TrapezoidCalculatorServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
TrapezoidCalculator calculator = new TrapezoidCalculator();
calculator.setUpperBase(Double.parseDouble(request.getParameter("upperBase")));
calculator.setLowerBase(Double.parseDouble(request.getParameter("lowerBase")));
calculator.setHeight(Double.parseDouble(request.getParameter("height")));
calculator.calculate();
request.setAttribute("calculator", calculator);
request.getRequestDispatcher("trapezoid.jsp").forward(request, response);
}
}
```
在Web应用程序的WEB-INF目录下,需要创建一个web.xml文件,以将Servlet映射到URL:
```
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_4_0.xsd"
id="WebApp_ID" version="4.0">
<display-name>trapezoid-calculator</display-name>
<welcome-file-list>
<welcome-file>trapezoid.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>TrapezoidCalculatorServlet</servlet-name>
<servlet-class>com.example.TrapezoidCalculatorServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TrapezoidCalculatorServlet</servlet-name>
<url-pattern>/TrapezoidCalculator</url-pattern>
</servlet-mapping>
</web-app>
```
这样,当用户在浏览器中访问http://localhost:8080/trapezoid-calculator/时,将显示梯形计算器页面。用户可以输入梯形的上底、下底和高,提交表单后,页面将显示梯形的面积。
阅读全文