天天看点

JSP程序设计练习--第三章-课后习题7题3-7 Tag标记

JSP绋?搴?璁捐?$?涔?--绗?涓?绔?-璇惧??涔?棰?7

  • 棰?3-7 Tag??璁?/li>
    • 浠g??璁捐??/li>
      • index.jsp
      • index2.jsp
      • GetArea.tag
    • 杩?琛?????

棰?3-7 Tag??璁?/h1>

缂???涓?涓?Tag??浠?etArea.tag璐?璐f??轰?瑙?褰㈢???㈢Н锛?骞朵娇??ariable??浠よ???涓?瑙?褰㈢???㈢Н缁?璋??ㄨ??ag??浠剁??JSP椤甸????JSP椤甸?㈣?璐f?剧ずTag??浠惰????? 涓?瑙?褰㈢???㈢Н??JSP?ㄨ???ag??浠舵?讹?浣跨??ttribute??浠ゅ?涓?瑙?褰??杈圭???垮害浼? ??缁?Tag??浠躲??one.jsp??two.jsp?戒娇??ag??璁拌???etArea.tag??one.jsp灏?杩?????涓?瑙?褰㈢???㈢Н淇?????澶?3浣?灏??帮?two.jsp灏?杩?????涓?瑙?褰㈢???㈢Н淇?????澶?6浣?灏??般??

浠g??璁捐??/h2>

index.jsp

<%@ page contentType="text/html;charset=GB2312" language="java" %>
<%@taglib prefix="calculator" tagdir="/WEB-INF/tags" %>
<html>
<body bgcolor="aqua">
<table cellspacing="1" cellpadding="1" width="20%"  border="0">
    <tr valign="bottom">
        <td><a href="/index.jsp"><font size=3>index.jsp淇???涓?浣?灏???lt;/font> </a> </td>
        <td><a href="/index2.jsp"><font size=3>index2.jsp淇?????浣?灏???lt;/font> </a> </td>
    </tr>
</table>
<p>杈??ヤ?瑙?褰???¤竟???硷?璁$??涓?瑙?褰㈢???㈢Н
<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>
  <br><input type="submit" value="??浜? name="submit">
</form>
<%
  String a = request.getParameter("a");
  String b = request.getParameter("b");
  String c = request.getParameter("c");

  if (a!=null&&b!=null&&c!=null){
%>  <calculator:GetArea numberA="<%=a%>" numberB="<%=b%>" numberC="<%=c%>" d="one"/>
  <br><%=message%>
  <br><%=area%>
<%  }
%>
</body>
</html>
           

index2.jsp

<%@ page contentType="text/html;charset=GB2312" language="java" %>
<%@taglib prefix="calculator" tagdir="/WEB-INF/tags" %>
<html>

<body bgcolor="#7fffd4">
<table cellspacing="1" cellpadding="1" width="20%" border="0">
    <tr valign="bottom">
        <td><a href="/index.jsp"><font size=3>index.jsp淇???涓?浣?灏???lt;/font> </a> </td>
        <td><a href="/index2.jsp"><font size=3>index2.jsp淇?????浣?灏???lt;/font> </a> </td>
    </tr>
</table>
<p>杈??ヤ?瑙?褰???¤竟???硷?璁$??涓?瑙?褰㈢???㈢Н锛?
<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>
    <br><input type="submit" value="??浜? name="submit">
</form>
<%
    String a = request.getParameter("a");
    String b = request.getParameter("b");
    String c = request.getParameter("c");
    if (a!=null&&b!=null&&c!=null){
%>  <calculator:GetArea numberA="<%=a%>" numberB="<%=b%>" numberC="<%=c%>" d="two"/>
    <br><%=message%>
    <br><%=area%>
<%  }
%>
</body>
</html>
           

GetArea.tag

<%@ tag pageEncoding="GB2312" %>
<%@attribute name="numberA" required="true" %>
<%@attribute name="numberB" required="true" %>
<%@attribute name="numberC" required="true" %>
<%@attribute name="d" required="true" %>
<%@variable name-given="area" scope="AT_END" %>
<%@variable name-given="message" scope="AT_END" %>
<%!
    public double getArea(double a, double b, double c){
        if (a+b>c&&a+c>b&&b+c>a){
            double p = (a+b+c)/2.0;
            double area = Math.sqrt(p*(p-a)*(p-b)*(p-c));
            return area;
        }
        else
            return -1;
    }
%>
<%
    try{
        double a = Double.parseDouble(numberA);
        double b = Double.parseDouble(numberB);
        double c = Double.parseDouble(numberC);
        double result = 0;
        if (d.equals("one")){
            result = getArea(a,b,c);
            jspContext.setAttribute("area", String.format("%.3f", result));
            jspContext.setAttribute("message", "姹?寰??㈢Н淇???3浣?灏???);
        }
        else if (d.equals("two")){
            result = getArea(a,b,c);
            jspContext.setAttribute("area", String.format("%.6f", result));
            jspContext.setAttribute("message", "姹?寰??㈢Н淇???6浣?灏???);
        }
    }catch (Exception e){
        jspContext.setAttribute("area", new Double(-1.0));
        jspContext.setAttribute("message", e.toString());
    }
%>
           

杩?琛?????

  1. index.jsp
    JSP程序设计练习--第三章-课后习题7题3-7 Tag标记
    JSP程序设计练习--第三章-课后习题7题3-7 Tag标记
  2. index2.jsp
    JSP程序设计练习--第三章-课后习题7题3-7 Tag标记
    JSP程序设计练习--第三章-课后习题7题3-7 Tag标记