利用javabean实现一个超级简单的在线计算器

package javaBean;
import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * 
 * 功能:实现javaBean中的一个计算类。
 */
public class Calculate {
	private String firstName;//第一个参数
	private String secondName;//第二个参数
	private BigDecimal result;//计算结果
	private String operation;//运算符
	
	//构造方法
	public Calculate(String fn,String sn,String op){
		this.firstName = fn;
		this.operation = op;
		this.secondName = sn;
	}
	
	/*
	 * set和get方法
	 */
	public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	public String getSecondName() {
		return secondName;
	}
	public void setSecondName(String secondName) {
		this.secondName = secondName;
	}
	public BigDecimal getResult() {
		this.result = calculate();
		return result;
	}
	public String getOperation() {
		return operation;
	}
	public void setOperation(String operation) {
		this.operation = operation;
	}
	
	//计算结果的方法
	public BigDecimal calculate(){
		 if(this.operation.endsWith("+")){
			 return (new BigDecimal(this.firstName)).add(new BigDecimal(this.secondName));
		 }
		 else if(this.operation.endsWith("-")){
			 return (new BigDecimal(this.firstName)).subtract(new BigDecimal(this.secondName));
		 }
		 else if(this.operation.endsWith("*")){
			 return (new BigDecimal(this.firstName)).multiply(new BigDecimal(this.secondName));
		 }
		 else return new BigDecimal(this.firstName).divide(new BigDecimal(this.secondName),5, RoundingMode.HALF_UP);
	}
}

package severlet;

import java.io.IOException;
import java.io.PrintWriter;
import javaBean.Calculate;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DealWith extends HttpServlet {

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;utf-8");
		
		//获取jsp中计算器的每一个参数
		String firstValue = request.getParameter("firstNumber");
		String secondValue = request.getParameter("secondNumber");
		String operatorValue = request.getParameter("operator");
		
		//把参数封装到一个Calculate对象中
		Calculate c  = new Calculate(firstValue,secondValue,operatorValue);
		
		//把该对象保存到request域对象中
		request.setAttribute("calculate",c);
		
		//使用转发技术跳转到计算页面
		request.getRequestDispatcher("/result.jsp").forward(request,response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html");
		this.doGet(request, response);
	}
}

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
  <head>
    <title>calculator.htmltitle>  
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  head>
  <body>
  	<table>
  		<tr>
          <td>计算结果是:td>
          <td><input type="text">td>
        tr>
  	table>
    <form action="${pageContext.request.contextPath}/DealWith" method="post">
        <table border="1">
        <tr>简单的计算器tr>
        <tr>
          <td>第一个参数:td>
          <td><input  type="text" name="firstNumber">td>
        tr>
        <tr>
          <td>运算符:td>
          <td>
          td>
        tr>
  	table> 
        <form action="${pageContext.request.contextPath}/DealWith" method="post">
        <table border="1">
        <tr>简单的计算器tr>
        <tr>
          <td>第一个参数:td>
          <td><input  type="text" name="firstNumber">td>
        tr>
        <tr>
          <td>运算符:td>
          <td>