package javaBean;
import java.math.BigDecimal;
import java.math.RoundingMode;
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;
}
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");
String firstValue = request.getParameter("firstNumber");
String secondValue = request.getParameter("secondNumber");
String operatorValue = request.getParameter("operator");
Calculate c = new Calculate(firstValue,secondValue,operatorValue);
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.html</title>
<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>
<select name = "operator">
<option>+</option>
<option>-</option>
<option>*</option>
<option>/</option>
</select>
</td>
</tr>
<tr>
<td>第二个参数:</td>
<td><input type="text" name="secondNumber"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="计算"></td>
</tr>
</table>
</form>
</body>
</html>
<%@ page language="java" import="java.util.*" import="javaBean.Calculate" pageEncoding="utf-8"%>
<html>
<head>
<title>calculator.html</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
<%
Calculate c = (Calculate)request.getAttribute("calculate");
String s1 = c.getFirstName();
String s2 = c.getOperation();
String s3 = c.getSecondName();
String s4 = c.getResult()+"";
String s = s1 + s2 +s3 +"="+s4;
%>
<table>
<tr>
<td>计算结果是:</td>
<td><input type="text" value = "<%=s%>"></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>
<select name = "operator">
<option>+</option>
<option>-</option>
<option>*</option>
<option>/</option>
</select>
</td>
</tr>
<tr>
<td>第二个参数:</td>
<td><input type="text" name="secondNumber"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="计算"></td>
</tr>
</table>
</form>
</body>
</html>