这里有两个文件,首先是一个JavaBean的类代码(SimpleBean.java),代码如下:
package
com.JSP_P11.ch5;
public class SimpleBean
... {
String first;
String second;
String operator;
double result;
public void setFirst(String first)
...{
this.first=first;
}
public void setSecond(String second)
...{
this.second=second;
}
public void setOperator(String operator)
...{
this.operator=operator;
}
public String getFirst()
...{
return this.first;
}
public String getSecond()
...{
return this.second;
}
public String getOperator()
...{
return this.operator;
}
public double getResult()
...{
return this.result;
}
public void calculate()
...{
double one=Double.parseDouble(first);//类型转换也感觉没有C#直观
double two=Double.parseDouble(second);
try//C#中switch可以处理字符,而java不行了,有点不爽:(
...{
if(operator.equals("+")) result=one+two;
else if(operator.equals("-")) result=one-two;
else if(operator.equals("*")) result=one*two;
else if(operator.equals("/")) result=one/two;
}
catch(Exception e)
...{
System.out.print(e);
}
}
}
public class SimpleBean
... {
String first;
String second;
String operator;
double result;
public void setFirst(String first)
...{
this.first=first;
}
public void setSecond(String second)
...{
this.second=second;
}
public void setOperator(String operator)
...{
this.operator=operator;
}
public String getFirst()
...{
return this.first;
}
public String getSecond()
...{
return this.second;
}
public String getOperator()
...{
return this.operator;
}
public double getResult()
...{
return this.result;
}
public void calculate()
...{
double one=Double.parseDouble(first);//类型转换也感觉没有C#直观
double two=Double.parseDouble(second);
try//C#中switch可以处理字符,而java不行了,有点不爽:(
...{
if(operator.equals("+")) result=one+two;
else if(operator.equals("-")) result=one-two;
else if(operator.equals("*")) result=one*two;
else if(operator.equals("/")) result=one/two;
}
catch(Exception e)
...{
System.out.print(e);
}
}
}
接着是页面处理的文件(index.jsp):
<%
@ page language
=
"
java
"
import
=
"
java.applet.*
"
pageEncoding
=
"
GB2312
"
%>
<% @page import = " com.JSP_P11.ch5.SimpleBean; " %>
< jsp:useBean id = " calculator " scope = " request " class = " com.JSP_P11.ch5.SimpleBean " >
< jsp:setProperty name = " calculator " property = " * " />
</ jsp:useBean >
< html >
< head >
< title > 计算器 </ title >
</ head >
< body >
<%
try
... {
calculator.calculate();
out.print(calculator.getFirst()+calculator.getOperator()+calculator.getSecond()
+"="+calculator.getResult());
}
catch (Exception e)
... {
System.out.print(e);
}
%>
< hr >
< form name = " form1 " action = " index.jsp " >
< table width = " 75 " border = " 1 " bordercolor = " #003300 " >
< tr bgcolor = " #999999 " >
< td colspan = " 2 " > simple calculator </ td >
</ tr >
< tr >
< td > 第一个操作数 </ td >
< td >< input type = text name = " first " ></ td >
</ tr >
< tr >
< td > 操作符 </ td >
< td >< select name = " operator " >
< option value = " + " >+</ option >
< option value = " - " >-</ option >
< option value = " * " >*</ option >
< option value = " / " >/</ option >
</ select >
</ td >
</ tr >
< tr >
< td > 第二个操作数 </ td >
< td >< input type = text name = " second " ></ td >
</ tr >
< tr >
< td colspan = " 2 " bgcolor = " #cccccc " >< input type = submit value = " 计算 " ></ td >
</ tr >
</ table >
</ form >
</ body >
</ html >
<% @page import = " com.JSP_P11.ch5.SimpleBean; " %>
< jsp:useBean id = " calculator " scope = " request " class = " com.JSP_P11.ch5.SimpleBean " >
< jsp:setProperty name = " calculator " property = " * " />
</ jsp:useBean >
< html >
< head >
< title > 计算器 </ title >
</ head >
< body >
<%
try
... {
calculator.calculate();
out.print(calculator.getFirst()+calculator.getOperator()+calculator.getSecond()
+"="+calculator.getResult());
}
catch (Exception e)
... {
System.out.print(e);
}
%>
< hr >
< form name = " form1 " action = " index.jsp " >
< table width = " 75 " border = " 1 " bordercolor = " #003300 " >
< tr bgcolor = " #999999 " >
< td colspan = " 2 " > simple calculator </ td >
</ tr >
< tr >
< td > 第一个操作数 </ td >
< td >< input type = text name = " first " ></ td >
</ tr >
< tr >
< td > 操作符 </ td >
< td >< select name = " operator " >
< option value = " + " >+</ option >
< option value = " - " >-</ option >
< option value = " * " >*</ option >
< option value = " / " >/</ option >
</ select >
</ td >
</ tr >
< tr >
< td > 第二个操作数 </ td >
< td >< input type = text name = " second " ></ td >
</ tr >
< tr >
< td colspan = " 2 " bgcolor = " #cccccc " >< input type = submit value = " 计算 " ></ td >
</ tr >
</ table >
</ form >
</ body >
</ html >