作业习题6

编写两个Tag文件Rect.tag和Circle.tag。Rect.tag负责计算并显示矩形的面积,Circle.tag负责计算并显示与的面积

JSP源码:

    pageEncoding="UTF-8"%>
<%@ taglib tagdir="/WEB-INF/tags" prefix="tsg" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
<p>请输入矩形的长和宽:
<form action="" method="post" name=form>
<input type="text" name="length" id="length">
<BR><input type="text" name="width" id="width">
<BR><Input type="submit"value="送出" name=submit1>
</form>
<tsg:Rect/>//调用tag文件
<p>请输入圆的半径:
<form action="" method="post" name=form>
<input type="text" name="r1" id="r1">
<BR><Input type="submit"value="送出" name=submit2>
</form>
<tsg:Circle/>//调用tag文件
</body>
</html>

tags

<%  String str =  request.getParameter("r1");//计算圆的面积
	if(str!=null){
	Double r=Double.parseDouble(str);
	Double area;
	area=3.14*r*r;
	out.println("圆的面积:"+area);
	%>
	<%
	}
	%>
	<%@ tag language="java" pageEncoding="UTF-8"%>
<%  String l =  request.getParameter("length"); //计算矩形的面积
	String h =  request.getParameter("width");
	if(l!=null&&h!=null){
	int m=Integer.parseInt(l);
	int n=Integer.parseInt(h);
	int area=0;
	area=m*n;
	out.println("矩形的面积:"+area);
	%>
	<%
		}
	%>

结果图

JSP作业三JSP作业三

习题七

编写一个Tag文件GetArea.tag负责求出三角形的面积,并使用variable指令返回三角形的面积给调用该文件的JSP页面。

one.jsp源码:

    pageEncoding="UTF-8"%>
<%@ taglib tagdir="/WEB-INF/tags" prefix="tra" %>
<%@page import="java.text.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
<p>请输入三角形三边长:
<form action="" method="post" name=form>
<input type="text" name="length1" id="length1">
<BR><input type="text" name="length2" id="length2">
<BR><input type="text" name="length3" id="length3">
<BR><Input type="submit"value="送出" name=submit1>
</form>
<%String l1=request.getParameter("length1");//取值并转化为字符串
 String l2=request.getParameter("length2");
 String l3=request.getParameter("length3");
 %>
<tra:GetArea b="<%=l2 %>" a="<%=l1 %>" c="<%=l3 %>"/>//通过attribute传参
 <% NumberFormat f=NumberFormat.getInstance();
 	f.setMaximumFractionDigits(3);//设置保留三位小数
 if(area1!=null)
	 {double d=area1.doubleValue();
 	String str=f.format(d);
 	out.println(str);}
 %>
</body>
</html>

two.jsp源码:

    pageEncoding="UTF-8"%>
<%@ taglib tagdir="/WEB-INF/tags" prefix="tra" %>
<%@page import="java.text.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
<p>请输入三角形三边长:
<form action="" method="post" name=form>
<input type="text" name="length1" id="length1">
<BR><input type="text" name="length2" id="length2">
<BR><input type="text" name="length3" id="length3">
<BR><Input type="submit"value="送出" name=submit1>
</form>
<%String l1=request.getParameter("length1");
 String l2=request.getParameter("length2");
 String l3=request.getParameter("length3");
 %>
<tra:GetArea b="<%=l2 %>" a="<%=l1 %>" c="<%=l3 %>"/>
 <% NumberFormat f=NumberFormat.getInstance();
 	f.setMaximumFractionDigits(6); //将参数改为6即可保留6位小数
 if(area1!=null)
	 {double d=area1.doubleValue();
 	String str=f.format(d);
 	out.println(str);}
 %>
</body>
</html>

tags

<%@tag import="java.util.*" %>
<%@attribute name="a" required="true" %>
<%@attribute name="b" required="true" %>
<%@attribute name="c" required="true" %>
<%@variable name-given="area1" variable-class="java.lang.Double" scope="AT_END"%>
<%	
		if(a!=null&&b!=null&&c!=null){
		double l1=Double.parseDouble(a);
		double l2=Double.parseDouble(b);
		double l3=Double.parseDouble(c);
		if(l1+l2>l3&&l2+l3>l1&&l1+l3>l2){
			double p=(l1+l2+l3)/2.0;
			double area=Math.sqrt(p*(p-l1)*(p-l2)*(p-l3));
			if(a!=null&&b!=null&&c!=null){
			jspContext.setAttribute("area1",new Double(area));
			}
		}
	}
%>	

结果图

JSP作业三JSP作业三

相关文章:

  • 2021-11-06
  • 2021-09-09
猜你喜欢
  • 2021-12-24
  • 2022-01-14
  • 2021-07-16
  • 2021-05-04
  • 2022-12-23
  • 2021-11-04
  • 2021-07-09
相关资源
相似解决方案