JSP 课后作业:编写 Tag 文件计算三角形面积

题目

书本 p59 第 7 题
编写一个 Tag 文件 GetArea.tag 负责求出三角形面积,并使用 variable 指令返回三角形的面积给调用该Tag 文件的 JSP 页面。JSP 页面负责显示 Tag 文件返回的三角形的面积。JSP 在调用 Tag 文件时,使用 attribute 指令将三角形三边的长度传递给 Tag 文件。one.jsp 和 two.jsp 都使用 Tag 标记调用GetArea.tag。one.jsp 将返回三角形的面积保留最多 3 位小数、two.jsp 将返回的三角形面积保留最多 6 位小数。

项目结构

10004286-2b12d0e060a7067c.png
image.png

代码

GetArea.tag

<%@ tag pageEncoding="utf-8" %>
<%@ attribute name="sideA" required="true" %>
<%@ attribute name="sideB" required="true" %>
<%@ attribute name="sideC" required="true" %>
<%@ variable name-given="area" variable-class="java.lang.Double" scope="AT_END" %>
<%
    double a = Double.parseDouble(sideA);
    double b = Double.parseDouble(sideB);
    double c = Double.parseDouble(sideC);
    if (a + b > c && a + c > b && c + b > a) {
        double p = (a + b + c) / 2.0;
        double area = Math.sqrt(p * (p - a) * (p - b) * (p - c));
        jspContext.setAttribute("area", new Double(area));
    } else {
        jspContext.setAttribute("area", new Double(-1));
    }
%>

one.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page import="java.text.*" %>
<%@ taglib tagdir="/WEB-INF/tags/3.7" prefix="computer" %>
<html>
<body>
<form action="" method=get name=form>
    <h3>请输入三角形三边长:</h3>
    <table>
        <tr>
            <td>a:</td>
            <td><input type="text" name="a"></td>
        </tr>
        <tr>
            <td>b:</td>
            <td><input type="text" name="b"></td>
        </tr>
        <tr>
            <td>c:</td>
            <td><input type="text" name="c"></td>
        </tr>
    </table>
    <br> <input type="submit" value="计算结果最多保留3位小数" name=submit>
    <%
        String a = request.getParameter("a");
        String b = request.getParameter("b");
        String c = request.getParameter("c");
        if (a == null || b == null || c == null) {
            a = "0";
            b = "0";
            c = "0";
        }
        if (a.length() > 0 && b.length() > 0 && c.length() > 0) {
    %>
    <computer:GetArea sideA="<%=a %>" sideB="<%=b %>" sideC="<%=c %>"/>
    <%
            NumberFormat f = NumberFormat.getInstance();
            f.setMaximumFractionDigits(3);
            double result = area.doubleValue();
            String str = f.format(result);
            out.println(str);
        }
    %>
</form>
</body>
</html>

two.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<%@page import="java.text.*" %>
<%@ taglib tagdir="/WEB-INF/tags/3.7" prefix="computer" %>
<html>
<body>
<form action="" method=get name=form>
    <h3>请输入三角形三边长:</h3>
    <table>
        <tr>
            <td>a:</td>
            <td><input type="text" name="a"></td>
        </tr>
        <tr>
            <td>b:</td>
            <td><input type="text" name="b"></td>
        </tr>
        <tr>
            <td>c:</td>
            <td><input type="text" name="c"></td>
        </tr>
    </table>
    <br> <input type="submit" value="计算结果最多保留6位小数" name=submit>
    <%
        String a = request.getParameter("a");
        String b = request.getParameter("b");
        String c = request.getParameter("c");
        if (a == null || b == null || c == null) {
            a = "0";
            b = "0";
            c = "0";
        }
        if (a.length() > 0 && b.length() > 0 && c.length() > 0) {
    %>
    <computer:GetArea sideA="<%=a %>" sideB="<%=b %>" sideC="<%=c %>"/>
    <%
            NumberFormat f = NumberFormat.getInstance();
            f.setMaximumFractionDigits(6);
            double result = area.doubleValue();
            String str = f.format(result);
            out.println(str);
        } %>
</form>
</body>
</html>

运行结果

10004286-f0ead9ccba8c8c7e.png
image.png
10004286-06c47c66719b4bb6.png
image.png
  • 9
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值