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

使用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 'https://www.jb51.net/':
            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></jsp:useBean>
    <jsp:setProperty property="*" />
    <%
        calcBean.calculate();
    %>
    <hr>
    计算结果是:
    <jsp:getProperty property="firstnum" />
    <jsp:getProperty property="operator" />
    <jsp:getProperty property="secondnum" />
    =<jsp:getProperty property="result" />
    <hr>
    <form action="/jsp/calculator.jsp" method="post">
        <table>
            <tr>
                <td colspan="2">简单的计算器</td>
            </tr>
            <tr>
                <td>第一个参数</td>
                <td><input type="text" /></td>
            </tr>
            <tr>
                <td><select>
                        <option value="+">+</option>
                        <option value="-">-</option>
                        <option value="*">*</option>
                        <option value="https://www.jb51.net/">/</option>
                </select></td>
            </tr>
            <tr>
                <td>第二个数</td>
                <td><input type="text" /></td>
            </tr>
            <tr>
                <td>
                    <button 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:

复制代码 代码如下:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wzfgfd.html