一个简单的JSP+JavaBean web计算器

JavaBean在Java Web开发中主要用来处理业务逻辑,JSP中的复杂数据操作通常会交给JavaBean来操作,JSP只从JavaBean中取出最终结果显示到客户端。这样可以很大程度上减轻JSP中大量使用Java脚本的负担。

我们看一个网页版计算器的例子,我们先写一个可以执行加减乘除运算的javaBean:

Calculator.java

package bean;

public class Calculator {
    private String firOpd;
    private String sndOpd;
    private double result;
    private String operator;

    public void setFirOpd(String firOpd) {
        this.firOpd = firOpd;
    }

    public String getFirOpd() {
        return firOpd;
    }

    public void setSndOpd(String sndOpd) {
        this.sndOpd = sndOpd;
    }

    public String getSndOpd() {
        return sndOpd;
    }

    public void setOperator(String operator) {
        this.operator = operator;
    }

    public String getOperator() {
        return operator;
    }

    public double getResult() {
        return result;
    }

    public void calculate() {
        double fir = Double.parseDouble(firOpd);
        double snd = Double.parseDouble(sndOpd);

        try {
            if(operator.equals("+"))
                result = fir + snd;
            else if(operator.equals("-"))
                result = fir - snd;
            else if(operator.equals("*"))
                result = fir * snd;
            else if(operator.equals("/"))
                result = fir / snd;
        }catch (ArithmeticException ae) {
            ae.printStackTrace();
        }
    }
}

接下来我们写一个jsp页面来调用这个javaBean:
cal.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<jsp:useBean id="cal" scope="request" class="bean.Calculator">
<jsp:setProperty name="cal" property="*"/>
</jsp:useBean>

<html>
  <head>
    <title>Web Calculator</title>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
  </head>

  <body>
    <%
      String CalRst = null;
      try {
          cal.calculate();
          CalRst = cal.getFirOpd() + cal.getOperator() + cal.getSndOpd() + "=" + cal.getResult();
      }catch (Exception e) {
          CalRst = e.getMessage();
      }
    %>

    <!--建立输入表单,提交给cal.jsp处理-->
    <form method=get action="cal.jsp">
    <table>
      <tr>
        <td>简单计算器</td>
      </tr>
      <tr>
        <td>Result : </td>
        <td><input type="text" name="result" value=<%=CalRst%> /></td>
      </tr>
      <tr>
        <td>FstOpd : </td>
        <td><input type="text" name="firOpd" /></td>
      </tr>
      <tr>
        <td>Operator : </td>
        <td><select name="operator">
              <option value="+">+</option>
              <option value="-">-</option>
              <option value="*">*</option>
              <option value="/">/</option>
            </select>
        </td>
      </tr>
      <tr>
        <td>SndOpd : </td>
        <td><input type="text" name="sndOpd" /></td>
      </tr>
      <tr>
        <td colspan="2" bgcolor="#eeeeee"><input type=submit value=Calculate /></td>
      </tr>
    </table>
    </form>
  </body>
</html>

我们先来看下cal.jsp这段代码,这里顺便补充说明JSP基本语法(三)中剩余的三个动作指令。

3行,JSP动作标签,格式为:

<jsp:useBean id="" scope="" class="" />

这个标签的用途为创建一个javaBean的实例并指定该实例的名称。创建该实例的类由class指定,该JavaBean的作用范围由scope指定,实例名由id指定。

在本例中,创建一个Calculator类实例,该实例名为”cal”,作用域为”request”,表明该JavaBean只能用于当前的用户请求中。

4行,JSP动作标签。格式为:

<jsp:setProperty name="" property="" value="" />
<jsp:setProperty name="" property="*" />

作用是给cal的属性赋值。如果property的值为*,表示用页面输入框内的值,给cal所有对应的属性赋值。本例中,4个输入框的name分别为result,firOpd,operator,sndOpd依次对应Calculator类中的4个同名变量。要注意,输入框的name和javaBean中的属性名要严格对应!如果property的值不为*,则要同时指明要赋的cal中的变量名和所要赋的值。如<jsp:setProperty name="cal" property="firOpd" value="10" />

14~22行为嵌入的java代码,调用cal的实例方法calculate()根据操作数和运算符计算出结果,见Calculator.java的37~53行。

24~56行创建一个输入表单,提交给cal.jsp处理。

执行方法:

  • 在Tomcat目录下的webapps目录下创建一个新的文件夹”Calculator”。
  • 在Calculator目录内创建cal.jsp,代码见上文。
  • 在Calculator目录下创建WEB-INF文件夹。
  • 进入WEB-INF文件夹创建classes文件夹。
  • 进入classes文件夹创建bean文件夹。
  • 进入bean文件夹,放入Calculator.java编译好的Calculator.class文件。
  • 启动Tomcat,在浏览器中输入localhost:8080/Calculator/cal.jsp即可。

进入页面如下:

    calculator

输入第一个和第二个操作数,选择任意操作符点击Calculate

    calculator1

得到以下结果:

    calculator2

该程序调试没有问题,有问题可留言我。

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值