CalculatorBean————————————————————————————————————————————
public class CalculatorBean {
private String firstNum;
private String sencondNum;
private String operator;
private Double result=0.0;
public Double getResult() {
return result;
}
public void setResult(Double result) {
this.result = result;
}
public String getFirstNum() {
return firstNum;
}
public void setFirstNum(String firstNum) {
this.firstNum = firstNum;
}
public String getSencondNum() {
return sencondNum;
}
public void setSencondNum(String sencondNum) {
this.sencondNum = sencondNum;
}
public String getOperator() {
return operator;
}
public void setOperator(String operator) {
this.operator = operator;
}
public void calculate(){
Double fisrt =Double.valueOf(firstNum);
Double sencond =Double.valueOf(sencondNum);
if("+".equals(operator)){
this.result=fisrt+sencond;
}else if("-".equals(operator)){
this.result=fisrt-sencond;
}else if("*".equals(operator)){
this.result=fisrt*sencond;
}else if("/".equals(operator)){
this.result=fisrt/sencond;
}
}
}
——————————————————————————————————————————————————————
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.lovo.javaBean.CalculatorBean;
/**
* Servlet implementation class CalculatorServlet
*/
@WebServlet("/CalculatorServlet.do")
public class CalculatorServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public CalculatorServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
//获取表单数据
String num1=request.getParameter("num1");
String num2=request.getParameter("num2");
String zifu= request.getParameter("zifu");
CalculatorBean calculatorBean= new CalculatorBean();
calculatorBean.setFirstNum(num1);
calculatorBean.setSencondNum(num2);
calculatorBean.setOperator(zifu);
calculatorBean.calculate();
//将JavaBean对象存放到Session对象中
HttpSession session = request.getSession();
session.setAttribute("calculatorBean", calculatorBean);
//控制页面转发
request.getRequestDispatcher("newCal.jsp").forward(request, response);
}
}
————————————————————————————————————————————————————
<%@page import="com.lovo.javaBean.CalculatorBean"%>
<%@ page language= "java" contentType="text/html;charset=UTF-8" %>
<html>
<head>
<meta charset="utf-8">
<title>计算器</title>
</head>
<body>
<hr>
<%
CalculatorBean calculatorBean = (CalculatorBean)session.getAttribute("calculatorBean");
if(calculatorBean!=null){
out.println(calculatorBean.getFirstNum()+calculatorBean.getOperator()+calculatorBean.getSencondNum()+"="+calculatorBean.getResult());
}
%>
<form action="CalculatorServlet.do" method="post">
<table border = 1 align="center">
<tr>
<td>操作数1:<input type="text" name="num1"></td>
</tr>
<tr>
<td>字符:
<select name="zifu" id="">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
</td>
</tr>
<tr>
<td>操作数2:<input type="text" name="num2"></td>
</tr>
<tr>
<td colspan="2"> <input type="submit" value="结果"></td>
</tr>
</table>
</form>
</body>
</html>