<%@ page contentType="text/html; Charset=gb2312" %> <%@ taglib tagdir="/WEB-INF/tags" prefix="computer" %> <html> <head> <title>JSP练习</title> </head> <body> <h3>以下是调用TAG文件的效果:</h3> <computer:Trangle sideA="3" sideB="4" sideC="5"/> </body> </html>
Trangle.tag
<p>这是一个TAG文件,负责计算三解角形的面积</p> <%@ attribute name="sideA" required="true"%> <%@ attribute name="sideB" required="true"%> <%@ attribute name="sideC" required="true"%> <%! public String getArea(double a, double b, double c){ if (a + b > c && a + c > b && b + c > a){ double p = (a + b + c) / 2.0; double area = Math.sqrt(p * (p - a) * (p - b) * (p - c)); return "<br>三角形的面积:" + area; } else{ return "<br>" + a + "," + b + "," + c + "不能构成一个三角形,无法计算面积"; } } %> <% out.println("<br>JSP页面传递过来的三条边:" + sideA + "," + sideB + "," + sideC); double a = Double.parseDouble(sideA); double b = Double.parseDouble(sideB); double c = Double.parseDouble(sideC); out.println(getArea(a, b, c)); %>