件,负责计算矩形的面积。 <%@ attribute name="sideA" required="true" %> <%@ attribute name="sideB" required="true" %> <%!
public String getArea(double a,double b) { if(a>0&;&;b>0) {
double area=a*b ;
return "
矩形的面积:"+area; } else
{ return("
"+a+","+b+"不能构成一个矩形,无法计算面积"); } } %>
<% out.println("
JSP页面传递过来的两条边:"+sideA+","+sideB); double a=Double.parseDouble(sideA); double b=Double.parseDouble(sideB);
out.println(getArea(a,b)); %>
Circle.tag:
这是一个Tag文件,负责计算园的面积。 <%@ attribute name="radius" required="true" %> <%!
public String getArea(double r) { if(r>0) {
double area=Math.PI*r*r ; return "
圆的面积:"+area; } else
{ return("
"+r+"不能构成一个圆,无法计算面积"); } } %>
<% out.println("
JSP页面传递过来的半径:"+radius); double r=Double.parseDouble(radius); out.println(getArea(r)); %>
7.
one.jsp:
<%@ page contentType="text/html;charset=GB2312" %> <%@ page import ="java.text.*" %>
<%@ taglib tagdir="/WEB-INF/tags" prefix="computer"%>
面积保留3位小数点: <%
NumberFormat f=NumberFormat.getInstance(); f.setMaximumFractionDigits(3); double result=area.doubleValue(); String str=f.format(result); out.println(str); %>
two.jsp:
<%@ page contentType="text/html;charset=GB2312" %> <%@ page import ="java.text.*" %>
<%@ taglib tagdir="/WEB-INF/tags" prefix="computer"%>
面积保留6位小数点: <%
NumberFormat f=NumberFormat.getInstance(); f.setMaximumFractionDigits(6); double result=area.doubleValue(); String str=f.format(result); out.println(str); %>
GetArea.tag:
<%@ attribute name="sideA" required="true" %> <%@ attribute name="sideB" required="true" %> <%@ attribute name="sideC" required="true" %>
<%@ variable name-given="area" variable-class="java.lang.Double" scope="AT_END" %> <%
double a=Double.parseDouble(sideA); double b=Double.parseDouble(sideB); double c=Double.parseDouble(sideC); if(a+b>c&;&;a+c>b&;&;c+b>a) { double p=(a+b+c)/2.0;
double result=Math.sqrt(p*(p-a)*(p-b)*(p-c)) ; jspContext.setAttribute("area",new Double(result)); } else
{ jspContext.setAttribute("area",new Double(-1)); } %>
8.
linxi8.jsp:
<%@ page contentType="text/html;Charset=GB2312" %> <%@ taglib tagdir="/WEB-INF/tags" prefix="ok"%>
Biaoge.tag:
<%@ attribute name="color" %> <%@ attribute name="name" %> <%@ attribute name="phone" %> <%@ attribute name="email" %>
<%=name%> | <%=phone%> | <%=email%> |
第4章 JSP内置对象
习题四解答
1.C。
2.将获取的字符串用ISO-8859-1进行编码,并将编码存放到一个字节数组中,然后再将这个数组转化为字符串对象。 3.inputString.jsp:
<%@ page contentType="text/html;chars