Using JSP to call JavaBean to implement a super simple web calculator example


Here is the code:

Copy codeThe code is as follows:
package com.amos.model;

import java.math.BigDecimal;

* @ClassName: Calculator
*@ Description: Calculator
* @author: amosli
* @email:[email protected]
* @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();
        case ‘-‘:
            this.result = a.subtract(b).toString();
        case ‘*’:
            this.result = a.multiply(b).toString();
        case ‘/’:
            if (b.doubleValue()==0) {
Throw new runtimeException (“dividend cannot be zero”);
            this.result = a.divide(b,10,BigDecimal.ROUND_HALF_DOWN).toString();


Copy codeThe code is as follows:
<%@ page language=”java” contentType=”text/html; charset=UTF-8″
<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “”>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
< title > JSP calculator
    <jsp:setProperty property=”*” name=”calcBean” />
The results are as follows
    <jsp:getProperty property=”firstnum” name=”calcBean” />
    <jsp:getProperty property=”operator” name=”calcBean” />
    <jsp:getProperty property=”secondnum” name=”calcBean” />
    =<jsp:getProperty property=”result” name=”calcBean” />
    <form action=”/jsp/calculator.jsp” method=”post”>
        <table style=”text-align: center;”>
< TD colSpan = “2” > simple calculator</td>
< td > first parameter</td>
                <td><input type=”text” name=”firstnum” /></td>
                <td><select name=”operator”>
                        <option value=”+”>+</option>
                        <option value=”-“>-</option>
                        <option value=”*”>*</option>
                        <option value=”/”>/</option>
< td > the second number</td>
                <td><input type=”text” name=”secondnum” /></td>
< button name = “Calc” type = “submit” > calculation < / button >

Code introduction

1) Note here that if you want to carry out precision operation, it is easy to distort with double + double, for example, a = 0.1, B = 0.02, a + B =?

Copy codeThe code is as follows:

This is because the bottom layer of the computer only knows 0 and 1, while double is 64 bit precision, so it is easy to be distorted in calculation

It is suggested to use BigDecimal or BigInteger class for operation, and call its original add, substract and other methods to realize addition and subtraction

2) JSP is mainly used for jsp:useBean Object instantiation, jsp:setProperty and jsp:getProperty Set value and value

3) About the configuration of error page content:

stay web.xml Add the following content to capture exception:

Copy codeThe code is as follows:

At the same time error.jsp The error message will be displayed on the page:

Copy codeThe code is as follows:
Sorry, something went wrong!!
    <%out.print(exception.getMessage()); %>

Supplement: JSP has two development modes:

After Sun company introduced JSP technology, it also recommended two web application development modes, one is JSP + JavaBean mode, the other is servlet + jsp + JavaBean mode.
1) Jsp + JavaBean mode, suitable for simple development, suitable for developing web applications with less complex business logic. In this mode, JavaBean is used to encapsulate business data. JSP is responsible for processing user requests and displaying data;
2) Servlet + jsp + JavaBean (MVC) mode is suitable for developing complex web applications. In this mode, servlet is responsible for processing user requests, JSP is responsible for data display, and JavaBean is responsible for encapsulating data. Servlet + JSP, JavaBean mode, the hierarchy between the various modules of the program is clear, this mode is recommended for web development.

The first development mode is adopted in this example

PS: Here we recommend two online calculators of our website, which are implemented by JS and have powerful functions. I believe it will be helpful for you to deeply understand the mathematical operation of JavaScript and web design

Online standard calculator:

Online scientific calculator:

Recommended Today

Layout of angular material (2): layout container

Layout container Layout and container Using thelayoutDirective to specify the layout direction for its child elements: arrange horizontally(layout=”row”)Or vertically(layout=”column”)。 Note that if thelayoutInstruction has no value, thenrowIs the default layout direction. row: items arranged horizontally.max-height = 100%andmax-widthIs the width of the item in the container. column: items arranged vertically.max-width = 100%andmax-heightIs the height of the […]