inputNumber.jsp页面代码
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib tagdir="/WEB-INF/tags" prefix="computer" %>
<html>
<body>
<h2>输入运算数、选择运算符号:</h2>
<form action="" method="post">
<input type="text" name="num1" />
<select name="operator">
<option value="+">+
<option value="-">-
<option value="*">*
<option value="/">/
</select>
<input type="text" name="num2" /><br /><br />
<input type="submit" value="提交你的选择"/>
</form>
<%
request.setCharacterEncoding("utf-8");
String a = request.getParameter("num1");
String b = request.getParameter("num2");
String operator = request.getParameter(operator);
if(a==null)
a="";
if(b==null)
b="";
if(a.length()>0&&b.length()>0){
%>
<computer: Computer numberA="<%=a %>" numberB="<%=b %>" operator="<%=operator %>" />
计算结果:<%=a %><%=operator %><%=b %><%=result %>
<%}
%>
</body>
</html>
Computer.tag页面代码
<%@ tag pageEncoding="utf-8" %>
<%@ attribute name="numberA" required="true" %>
<%@ attribute name="numberB" required="true" %>
<%@ attribute name="operator" required="true" %>
<%@ variable name-given="result" scope="AT_END" %>
<%
try{
double a = Double.parseDouble(numberA);
double b = Double.parseDouble(numberB);
double r = 0;
if(operator.equals("+"))
r=a+b;
if(operator.equals("-"))
r=a-b;
if(operator.equals("*"))
r=a*b;
if(operator.equals("/"))
r=a/b;
jspContext.setAttribute("result", String.valueOf(r));
}catch(Exception e){
jspContext.setAttribute("result", "发生异常:"+e);
}
%>
qq_shoulder_23344064
相关分类