创建form.jsp和result.jsp,form.jsp用于提交三角形的边长,result.jsp用于获取提交的边长并计算输出三角形的面积。
时间: 2024-05-16 21:16:57 浏览: 14
form.jsp代码如下:
```
<html>
<head>
<title>计算三角形面积</title>
</head>
<body>
<h1>请输入三角形的边长</h1>
<form action="result.jsp" method="post">
<label>边长1:</label>
<input type="number" name="side1"><br>
<label>边长2:</label>
<input type="number" name="side2"><br>
<label>边长3:</label>
<input type="number" name="side3"><br><br>
<input type="submit" value="计算面积">
</form>
</body>
</html>
```
result.jsp代码如下:
```
<%@ 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>
<%-- 获取提交的三边 --%>
<% double a = Double.parseDouble(request.getParameter("side1"));
double b = Double.parseDouble(request.getParameter("side2"));
double c = Double.parseDouble(request.getParameter("side3"));
%>
<%-- 计算半周长和面积 --%>
<% double s = (a+b+c)/2;
double area = Math.sqrt(s*(s-a)*(s-b)*(s-c));
%>
<p>三边分别为:<%=a%>,<%=b%>,<%=c%></p>
<p>半周长为:<%=s%></p>
<p>面积为:<%=area%></p>
</body>
</html>
```
这里使用了JSP(Java Server Pages)技术,将结果页面中的Java代码嵌入到HTML中,可以方便地获取提交的数据并进行计算。在result.jsp中,我们首先使用request.getParameter()方法获取提交的三边,然后使用Math.sqrt()方法计算面积。最后将计算结果输出到HTML中。注意,这里使用了EL表达式(<%= ... %>)来将Java变量输出到HTML中。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)