总结

  • 本来想实现from表单在单选框被选取后产生变动的功能,但是不知道怎么做,赶作业就选择先直接给tag的参数赋值。
  • 使用idea过程中发现jspContext.setAttribute()函数爆红,原因是因为idea本身不会自动导入servlet-api.jarjsp-api.jar这两个包。需要手动添加。这两个包可以在tomcat的目录apache-tomcat-8.5.35\lib下找到。

JSP文件

lianxi6.jsp

<%@ page import="jdk.internal.util.xml.impl.Input" %><%--
  Created by IntelliJ IDEA.
  User: Atlantis
  Date: 2019-03-24
  Time: 19:04
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib tagdir="/WEB-INF/tags" prefix="RestTag"%>
<%@taglib prefix="CircleTag" tagdir="/WEB-INF/tags/tagsTwo" %>
<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%>
</body>
</html>

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

<%@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());
}
%>

运行结果

JSP作业之习题三第6题

相关文章:

  • 2021-10-25
  • 2021-06-03
  • 2021-08-18
  • 2021-05-01
  • 2022-01-20
  • 2021-12-12
  • 2021-11-20
猜你喜欢
  • 2021-11-04
  • 2021-07-09
  • 2021-06-13
  • 2021-12-28
  • 2021-10-16
  • 2021-06-03
  • 2022-01-20
相关资源
相似解决方案