习题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>