欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

使用jsp调用javabean实现超简单网页计算器示例

程序员文章站 2023-11-13 18:09:16
以下是代码: calculator.java 复制代码 代码如下: package com.amos.model; import jav...

使用jsp调用javabean实现超简单网页计算器示例
使用jsp调用javabean实现超简单网页计算器示例


使用jsp调用javabean实现超简单网页计算器示例
以下是代码:


calculator.java

复制代码 代码如下:

package com.amos.model;

import java.math.bigdecimal;

/**
* @classname: calculator
* @description: 计算器
* @author: amosli
* @email:amosli@infomorrow.com
* @date mar 20, 2014 1:04:59 am 
*/
public class calculator {
    private string firstnum="0";
    private string secondnum="0";
    private char operator='+';
    private string result;

    public string getresult() {
        return result;
    }

    public char getoperator() {
        return operator;
    }

    public void setoperator(char operator) {
        this.operator = operator;
    }

    public void setresult(string result) {
        this.result = result;
    }

    public string getfirstnum() {
        return firstnum;
    }

    public void setfirstnum(string firstnum) {
        this.firstnum = firstnum.trim();
    }

    public string getsecondnum() {
        return secondnum;
    }

    public void setsecondnum(string secondnum) {
        this.secondnum = secondnum.trim();
    }

    public void calculate() {
        bigdecimal a = new bigdecimal(this.firstnum);
        bigdecimal b = new bigdecimal(this.secondnum);
        switch (this.operator) {
        case '+':
            this.result = a.add(b).tostring();
            break;
        case '-':
            this.result = a.subtract(b).tostring();
            break;
        case '*':
            this.result = a.multiply(b).tostring();
            break;
        case '/':
            if (b.doublevalue()==0) {
                throw new runtimeexception("被除数不能为零");
            }
            this.result = a.divide(b,10,bigdecimal.round_half_down).tostring();
            break;
        default:
            break;
        }
    }
}

calculator.jsp

复制代码 代码如下:

<%@ page language="java" contenttype="text/html; charset=utf-8"
    pageencoding="utf-8"%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>jsp计算器</title>
</head>
<body>
    <jsp:usebean id="calcbean" class="com.amos.model.calculator"></jsp:usebean>
    <jsp:setproperty property="*" name="calcbean" />
    <%
        calcbean.calculate();
    %>
    <hr>
    计算结果是:
    <jsp:getproperty property="firstnum" name="calcbean" />
    <jsp:getproperty property="operator" name="calcbean" />
    <jsp:getproperty property="secondnum" name="calcbean" />
    =<jsp:getproperty property="result" name="calcbean" />
    <hr>
    <form action="/jsp/calculator.jsp" method="post">
        <table style="text-align: center;">
            <tr>
                <td colspan="2">简单的计算器</td>
            </tr>
            <tr>
                <td>第一个参数</td>
                <td><input type="text" name="firstnum" /></td>
            </tr>
            <tr>
                <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="secondnum" /></td>
            </tr>
            <tr>
                <td>
                    <button name="calc" type="submit">计算</button>
                </td>
            </tr>
        </table>
    </form>
</body>
</html>

代码介绍

1)这里注意如果要进行精度运算,用double + double很容易失真,比如,a=0.1,b=0.02 ,a+b=?

复制代码 代码如下:

0.1+0.02=0.12000000000000001

这里是因为计算机底层只认识0和1,而double是64位精度,所以在计算时,很容易失真.

建议做运算的时候使用bigdecimal或者biginteger类进行运算,调用其原有的add,substract等方法即可实现加减等操作.

2)jsp主要用到的就是jsp:usebean进行实例化对象,jsp:setproperty和jsp:getproperty进行设值和取值.

3)关于错误页面内容的配置:

在web.xml中添加如下内容,捕获exception:

复制代码 代码如下:

<error-page>
      <exception-type>java.lang.exception</exception-type>
      <location>/error/error.jsp</location>
  </error-page>

同时在error.jsp页面中将错误信息显示出来:

复制代码 代码如下:

sorry,出错了!!
    <%out.print(exception.getmessage()); %>

补充:jsp有两种开发模式:

sun公司推出jsp技术后,同时也推荐了两种web应用程序的开发模式,一种是jsp+javabean模式,一种是servlet+jsp+javabean模式。
1)jsp+javabean模式,适合简单开发,适合开发业务逻辑不太复杂的web应用程序,这种模式下,javabean用于封装业务数据,jsp即负责处理用户请求,又显示数据;
2)servlet+jsp+javabean(mvc)模式适合开发复杂的web应用,在这种模式下,servlet负责处理用户请求,jsp负责数据显示,javabean负责封装数据。 servlet+jsp、javabean模式程序各个模块之间层次清晰,web开发推荐采用此种模式。

本例中采用的即是第一种开发模式.

ps:这里再为大家推荐两款本站的在线计算器,都是采用js实现,且功能强大,相信对于大家深入了解javascript数学运算及web设计会有所帮助:

在线标准计算器:

在线科学计算器: