JSP文件
lianxi6.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib tagdir="/WEB-INF/tags" prefix="RestTag"%>
<%@taglib tagdir="/WEB-INF/tags"prefix="CircleTag" %>
<html>
<head>
<title>练习六</title>
</head>
<body>
<h3>以下是调用Rest.tag文件的效果:</h3>
<RestTag:Rest numberA="5" numberB="6"/>
<%=RectMessage%>
<%=RectArea%>
<h3>以下是调用Circle.tag文件的效果:</h3>
<CircleTag:Circle number="5"/>
<%=CircleMessage%>
<%=CircleArea%>
<br>
</body>
tag文件
Rest.tag
<%@tag pageEncoding="utf-8" %>
<%@attribute name="numberA" required="true" %>
<%@attribute name="numberB" required="true" %>
<%@variable name-given="RectArea" variable-class="java.lang.Double" scope="AT_END" %>
<%@variable name-given="RectMessage" scope="AT_END" %>
<%!
public double getRectArea(double a,double b){
if (a!=0&&b!=0){
double area =a*b;
return area;
}else
return -1;
}
%>
<% try{
double a=Double.parseDouble(numberA);
double b=Double.parseDouble(numberB);
double result =0;
result = getRectArea(a, b);
jspContext.setAttribute("RectArea",new Double(result));
jspContext.setAttribute("RectMessage","矩形的面积");
}catch (Exception e){
jspContext.setAttribute("RectArea",new Double(-1.0));
jspContext.setAttribute("RectMessage",""+e.toString());
}
%>
Circle文件
<%@tag pageEncoding="utf-8" %>
<%@attribute name="number" required="true" %>
<%@variable name-given="CircleArea" variable-class="java.lang.Double" scope="AT_END" %>
<%@variable name-given="CircleMessage" scope="AT_END" %>
<%!
static double PI = 3.14;
public double getCircleArea(double a){
if (a!=0){
double area =a*a*PI;
return area;
}else
return -1;
}
%>
<% try{
double a=Double.parseDouble(number);
double result =0;
result = getCircleArea(a);
jspContext.setAttribute("CircleArea",new Double(result));
jspContext.setAttribute("CircleMessage","圆的面积");
}catch (Exception e){
jspContext.setAttribute("CircleArea",new Double(-1.0));
jspContext.setAttribute("CircleMessage",""+e.toString());
}
%>
运行结果