JSP利用JavaBean的一个简单的计算器例子

这里有两个文件,首先是一个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);
        }

    }

}

 

 

接着是页面处理的文件(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 >
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值