JSP实验(⼆)Tag标记及其使⽤
JSP实验(⼆)Tag标记及其使⽤
⽂章⽬录
⼀、相关知识点
1、Tag⽂件可以把⼀个对象返回给调⽤它的JSP页⾯。步骤如下:
(1)Tag⽂件使⽤variable指令:
<%@ variable name-given=“对象名字” variable-class=“对象的类型” scope=“有效范围” %>
给出返回的对象的名字、类型和有效范围。
(2)将返回的对象的引⽤和名字存储在内置对象jspContext中:
jspContext.setAttribute(“对象名字”,对象的引⽤));
2、Tag⽂件可以接收⼀个jsp传过来的对象步骤如下:
<%@ attribute name=“对象名字” required=“true” type=“对象类型” %>
在引⽤Tag的JSP中
<;前缀: Tag⽂件名字 对象名字=“对象的引⽤” />
<;前缀: Tag⽂件名字 对象名字=“对象的引⽤” >
标记体
</前缀: Tag⽂件名字 >
例如: tag中定义:<%@ attribute name=“length” required=“true” %>
JSP中代码: <beijing: AddSum length=“1000” />
⼆、实验⽬的及要求
本实验的⽬的是让学⽣灵活掌握在Tag标记中使⽤variable和attribute指令。
编写⼀个Tag⽂件GetArea.tag,负责计算三⾓形或梯形的⾯积,并将计算结果返回给调⽤该Tag⽂件的的JSP页⾯。编写⼀个JSP页⾯inputAndShow.jsp,该页⾯负责向Tag⽂件提交三⾓形三边的长度或梯形的上底、下底和⾼,并负责显⽰Tag⽂件返回的相应⾯积。
1.inputAndShow.jsp的具体要求
inputAndShow.jsp提供⼀个表单。⽤户可以在表单中分别输⼊三个数值、并选择这三个数值代表三⾓形三边的长度或梯形的上底、下底和⾼,然后提交给当前页⾯。inputAndShow.jsp通过Tag标记调⽤GetArea.tag⽂件,并向该Tag⽂件GetArea.tag传递三⾓形三边的长度和梯形的上底、下底和⾼。inputAndShow.jsp负责显⽰Tag⽂件GetArea.tag返回的⾯积以及相关信息。
2.GetArea.tag的具体要求
要求Tag⽂件GetArea使⽤attibute指令得到JSP页⾯传递过来三⾓形形三边的长度或梯形的上底、下底和⾼,使⽤variable指令返回相应的⾯积,以及字符串信息:“三⾓形的⾯积”或“梯形的⾯积”。
三、实验代码
1 inputandshow.jsp
<%--
Created by IntelliJ IDEA.
User: ruochen
Date:2020/10/29
Time:10:37
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java"%> <%@ taglib tagdir="/WEB-INF/tags" prefix="computer"%>
<html>
<head>
<title>Title</title>
</head>
<body>
<FORM action="" method=get name=form>
<table>
<tr><td>输⼊数值a:</td>
<td><INPUT type="text" name="a"></td>
</tr>
<tr><td>输⼊数值b:</td>
<td><INPUT type="text" name="b"></td>
</tr>
<tr><td>输⼊数值c:</td>
<td><INPUT type="text" name="c"></td>
</tr>
</table>
<INPUT type="radio" name="R" value="triangle">代表三⾓形
<INPUT type="radio" name="R" value="lader">代表梯形
<br><INPUT TYPE="submit" value="提交" name=submit>
</FORM>
<%  String Parameter("a");
String Parameter("b");
String Parameter("c");
String Parameter("R");
if(a==null||b==null||c==null){
a="0";
b="0";
c="0";
cd="0";
}
if(a.length()>0&&b.length()>0&&c.length()>0){
%><computer:getarea numberA="<%=a%>" numberB="<%=b%>"
numberC="<%=c%>" condition="<%=cd%>"/>
<br><%=message%>
<br><%=area %>
<%}
%>
</body>
variable怎么记</html>
2 getarea.tag
<%@ tag  pageEncoding="UTF-8"%>
<%@ attribute name="numberA" required="true"%>
<%@ attribute name="numberB" required="true"%>
<%@ attribute name="numberC" required="true"%>
<%@ attribute name="condition" required="true"%>
<%@ variable name-given="area" variable-class="java.lang.Double" scope="AT_END"%> <%@ variable name-given="message" scope="AT_END"%>
<%!
public double getTriangleArea(double a,double b,double c){
if(a+b>c&&a+c>b&&c+b>a){
double p=(a+b+c)/2.0;
double area=Math.sqrt(p*(p-a)*(p-b)*(p-c));
return area;
}
else
return-1;
}
public double getLaderArea(double above,double bottom,double h){
double area=(above+bottom)*h/2.0;
return area;
}
%>
<%try{double a=Double.parseDouble(numberA);
double b=Double.parseDouble(numberB);
double c=Double.parseDouble(numberC);
double result=0;
if(condition.equals("triangle")){
result=getTriangleArea(a,b,c);
jspContext.setAttribute("area",result);
jspContext.setAttribute("message","三⾓形的⾯积");
}
else if(condition.equals("lader")){
result=getLaderArea(a,b,c);
jspContext.setAttribute("area",result);
jspContext.setAttribute("message","梯形的⾯积");
}
}
catch(Exception e){
jspContext.setAttribute("area",-1.0);
jspContext.setAttribute("message",""+e.toString());
}
%>
3 效果展⽰
总结
输⼊数值a:
输⼊数值b:
输⼊数值c:
代表三⾓形 代表梯形
为html语法
通过attribute指令接收jsp传过来的数据
再在tag⽂件⾥处理数据并通过variable指令返回给jsp

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。