1、实现javabean
- package cn.csdn.web.domain;
- import java.math.BigDecimal;
- public class Calculate {
- private Double firstNum;
- private char operator;
- private Double secondNum;
- private Double result;
- public Calculate() {
- super();
- }
- public Double getFirstNum() {
- return firstNum;
- }
- public void setFirstNum(Double firstNum) {
- this.firstNum = firstNum;
- }
- public char getOperator() {
- return operator;
- }
- public void setOperator(char operator) {
- this.operator = operator;
- }
- public Double getSecondNum() {
- return secondNum;
- }
- public void setSecondNum(Double secondNum) {
- this.secondNum = secondNum;
- }
- public Double getResult() {
- return result;
- }
- public void setResult(Double result) {
- this.result = result;
- }
- public Double calculate() {
- switch (this.operator) {
- case '+':
- this.result = this.firstNum + this.secondNum;
- break;
- case '-':
- this.result = this.firstNum - this.secondNum;
- break;
- case '*':
- this.result = this.firstNum * this.secondNum;
- break;
- case '/':
- if (this.secondNum == 0) {
- System.out.println("除数不能为零");
- } else {
- this.result = this.firstNum / this.secondNum;
- BigDecimal bigDecimal = new BigDecimal(this.result);
- bigDecimal = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP);
- this.result = bigDecimal.doubleValue();
- }
- break;
- default:
- System.out.println("无法判断");
- break;
- }
- return result;
- }
- }
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme() + "://"
- + request.getServerName() + ":" + request.getServerPort()
- + path + "/";
- %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
- <style>
- table {
- border-collapse: collapse;
- }
- </style>
- <title>My JSP 'calculate.jsp' starting page</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- <!--
- <link rel="stylesheet" type="text/css" href="styles.css">
- -->
- </head>
- <body>
- <div align="center">
- <h1>
- 计算哭器简单实现
- </h1>
- <hr color="red">
- <div>
- <!-- 创建一个javabean组件 -->
- <jsp:useBean id="calculate" class="cn.csdn.web.domain.Calculate"></jsp:useBean>
- <jsp:setProperty property="*" name="calculate" />
- <%
- calculate.calculate();
- %>
- 计算结果是:
- <jsp:getProperty property="firstNum" name="calculate" />
- <jsp:getProperty property="operator" name="calculate" />
- <jsp:getProperty property="secondNum" name="calculate" />=
- <jsp:getProperty property="result" name="calculate" />
- </div>
- <form action="./calculate.jsp" method="post">
- <table border="1px">
- <caption>
- 计算器
- </caption>
- <tr>
- <td colspan="2"></td>
- </tr>
- <tr>
- <td>
- 第一个参数:
- </td>
- <td>
- <input type="text" name="firstNum" />
- </td>
- </tr>
- <tr>
- <td>
- 运算符
- </td>
- <td>
- <select name="operator">
- <option selected="selected">
- +
- </option>
- <option>
- -
- </option>
- <option>
- *
- </option>
- <option>
- /
- </option>
- </select>
- </td>
- </tr>
- <tr>
- <td>
- 第二个参数:
- </td>
- <td>
- <input type="text" name="secondNum" />
- </td>
- </tr>
- <tr>
- <td colspan="2">
- <input type="submit" value="计算" />
- </td>
- </tr>
- </table>
- </form>
- </div>
- </body>
- </html>
来自任海丽