Web开发第三周

习题6:

lianxi6.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib tagdir="/WEB-INF/tags" prefix="computer" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="" method=get>
<p>请输入矩形的长:</p><input type="text" name="long">
<p>请输入矩形的宽:</p><input type="text" name="width">
<p>请输入圆的半径:</p><input type="text" name="radius">
<input type="submit" value="提交">
</form>
<%
String a=request.getParameter("long");
String b=request.getParameter("width");
String c=request.getParameter("radius");
if(a==null||b==null||c==null||a==""||b==""||c==""){
    out.print("请填写完整");
}else{
%>    
    <computer:Rect longa="<%=a %>" widthb="<%= b %>"/>
    <computer:Circle radiusc="<%=c %>" />
    <p>矩形的面积为:<%=Rect_area %></p>
    <p>圆的面积为:<%=Circle_area %></p>
<% } %>
</body>
</html>

Rect.tag

<%@ tag language="java" pageEncoding="UTF-8"%>
<%@ attribute name="longa" required="true" %>
<%@ attribute name="widthb" required="true" %>
<%@ variable name-given="Rect_area" variable-class="java.lang.Double" scope="AT_END" %>
<%! public double getRectArea(double a,double b){
    return a*b;
}
%>
<% try{double a=Double.parseDouble(longa);
double b=Double.parseDouble(widthb);
double result=0;
result=getRectArea(a,b);
jspContext.setAttribute("Rect_area",result);
}
catch(Exception e){
jspContext.setAttribute("Rect_area",new Double(0));}
%>

Circle.tag

<%@ tag language="java" pageEncoding="UTF-8"%>
<%@ attribute name="radiusc" required="true" %>
<%@ variable name-given="Circle_area" variable-class="java.lang.Double" scope="AT_END" %>
<%! public double getCircleArea(double c){
    return Math.PI*c;
}
%>
<% try{double c=Double.parseDouble(radiusc);
double result=0;
result=getCircleArea(c);
jspContext.setAttribute("Circle_area",result);
}
catch(Exception e){
jspContext.setAttribute("Circle_area",new Double(0));}
%>

习题7

GetArea.tag

<%@ tag language="java" pageEncoding="UTF-8"%>
<%@ attribute name="theA" required="true" %>
<%@ attribute name="theB" required="true" %>
<%@ attribute name="theC" required="true" %>
<%@ variable name-given="Areass" variable-class="java.lang.Double" scope="AT_END" %>
<%!
public double getArea(double a,double b,double c){
if(a+b>c&&b+c>a&&c+a>b){
double p=(a+b+c)/2;
double result=Math.sqrt(p*(p-a)*(p-b)*(p-c));
return result;}
else{
    return -1;}
}%>
<% try{double a=Double.parseDouble(theA);
double b=Double.parseDouble(theB);
double c=Double.parseDouble(theC);
double area;
area=getArea(a,b,c);
jspContext.setAttribute("Areass",new Double(area));
}
catch(Exception e){
    jspContext.setAttribute("Areass",new Double(0.0));
}
%>

one.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ page import="java.text.*" %>
    <%@ taglib tagdir="/WEB-INF/tags" prefix="computer"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form>
<p>请输入三角形边长a:</p><input type="text" name="a">
<p>请输入三角形边长b:</p><input type="text" name="b">
<p>请输入三角形边长c:</p><input type="text" name="c">
<input type="submit" value="提交">
</form>
<%
NumberFormat f=NumberFormat.getInstance();
    f.setMaximumFractionDigits(3);
String jspa=request.getParameter("a");
String jspb=request.getParameter("b");
String jspc=request.getParameter("c");
if(jspa==""||jspb==""||jspc==""||jspa==null||jspb==null||jspc==null){
    out.print("请填写完整");
}
else{ %>
<computer:GetArea theA="<%= jspa %>" theB="<%= jspb %>" theC="<%= jspc %>"/>
<p>三角形面积为:<%=f.format(Areass)%></p>
<% }%>
</body>
</html>

two.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ page import="java.text.*" %>
    <%@ taglib tagdir="/WEB-INF/tags" prefix="computer"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form>
<p>请输入三角形边长a:</p><input type="text" name="a">
<p>请输入三角形边长b:</p><input type="text" name="b">
<p>请输入三角形边长c:</p><input type="text" name="c">
<input type="submit" value="提交">
</form>
<%
NumberFormat f=NumberFormat.getInstance();
    f.setMaximumFractionDigits(6);
String jspa=request.getParameter("a");
String jspb=request.getParameter("b");
String jspc=request.getParameter("c");
if(jspa==""||jspb==""||jspc==""||jspa==null||jspb==null||jspc==null){
    out.print("请填写完整");
}
else{ %>
<computer:GetArea theA="<%= jspa %>" theB="<%= jspb %>" theC="<%= jspc %>"/>
<p>三角形面积为:<%=f.format(Areass)%></p>
<% }%>
</body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值