<p>编写一个<span style="font-family: 'Times New Roman';">jsp</span><span style="font-family: 宋体;">页面,该页面提供一个表单,用户可以通过表单输入两个数和四则运算符号提交给该页面。用户提交表单后,</span><span style="font-family: 'Times New Roman';">jsp</span><span style="font-family: 宋体;">页面将计算任务交给一个</span><span style="font-family: 'Times New Roman';">bean</span><span style="font-family: 宋体;">去完成</span>。</p><p></p><p>1.创建<span style="font-family: 'Times New Roman';">bean</span><span style="font-family: 宋体;">的</span><span style="font-family: 'Times New Roman';">java</span><span style="font-family: 宋体;">源代码</span></p><p><span style="font-family: 宋体;">
</span></p>
package com.wxh;
public class CoumputerBean {
double numberOne,numberTwo,result;
String operator="+";
public double getNumberOne() {
return numberOne;
}
public void setNumberOne(double numberOne) {
this.numberOne = numberOne;
}
public double getNumberTwo() {
return numberTwo;
}
public void setNumberTwo(double numberTwo) {
this.numberTwo = numberTwo;
}
public double getResult() {
if(operator.equals("+")){
result=numberOne+numberTwo;
}else if(operator.equals("-")){
result=numberOne-numberTwo;
}else if(operator.equals("*")){
result=numberOne*numberTwo;
}else if(operator.equals("/")){
result=numberOne/numberTwo;
}
return result;
}
public void setResult(double result) {
this.result = result;
}
public String getOperator() {
return operator;
}
public void setOperator(String operator) {
this.operator = operator.trim();
}
}
2.创建使用bean的jsp页面
<%@ page import="com.wxh.*" %>
<body>
<jsp:useBean id="computer" class="com.wxh.CoumputerBean" scope="session" />
<jsp:setProperty name="computer" property="*"/>
<form method="post" action="">
<input type="text" name ="numberOne" value=<jsp:getProperty name="computer" property="numberOne"/>>
<select name="operator">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input type="text" name ="numberTwo"
value=<jsp:getProperty name="computer" property="numberTwo"/>>
=<jsp:getProperty name="computer" property="result"/>
<input type="submit" name="submit" value="提交你的选择"/>
</form>
</body>
注意事项:
1、在jsp页面开头导入javabean包。