首页 > 代码库 > 使用MVC实现计算器功能

使用MVC实现计算器功能

一:View  cal.jsp

    

 1 <%@page language="java" contentType="text/html; charset=utf-8" 2     pageEncoding="utf-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 7 <title>采用三目运算符获取运算符</title> 8 </head> 9 <body>10     <form action="CalServlet" method="post" >11     操作数1:<input type="text" name="num1" value="${requestScope.newCal.num1}"><br>12     运算: <input type="radio" name="op" value="+"   ${requestScope.newCal.op=="+"? "checked": " "}>+13           <input type="radio" name="op" value="-"   ${requestScope.newCal.op=="-"? "checked" : " "}>-14           <input type="radio" name="op" value="*"   ${requestScope.newCal.op=="*"? "checked" : " "}>*15           <input type="radio" name="op" value="/"   ${requestScope.newCal.op=="/"? "checked" : " "}>/<br> 16     操作数2:<input type="text" name="num2" value="${requestScope.newCal.num2}"><br>17     <input type="submit" value="计算" ><br>18     结果:<input type="text" value="${requestScope.newResult}">19     </form>20 </body>21 </html>

二:Model  Calculator.java

 1 package nuc.sw.cal.bean; 2  3 public class Calculator { 4       private float num1; 5       private String op; 6       private float num2; 7     public float getNum1() { 8         return num1; 9     }10     public void setNum1(float num1) {11         this.num1 = num1;12     }13     public String getOp() {14         return op;15     }16     public void setOp(String op) {17         this.op = op;18     }19     public float getNum2() {20         return num2;21     }22     public void setNum2(float num2) {23         this.num2 = num2;24     }25       26 }

三:CalServlet.java

 1 package nuc.sw.cal.servlet; 2  3 import java.io.IOException; 4 import javax.servlet.ServletException; 5 import javax.servlet.http.HttpServlet; 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8  9 import nuc.sw.cal.bean.Calculator;10 11 /**12  * Servlet implementation class CalServlet13  */14 public class CalServlet extends HttpServlet {15     private static final long serialVersionUID = 1L;16        17     /**18      * @see HttpServlet#HttpServlet()19      */20     public CalServlet() {21         super();22         // TODO Auto-generated constructor stub23     }24 25     /**26      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)27      */28     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {29         // TODO Auto-generated method stub30         response.getWriter().append("Served at: ").append(request.getContextPath());31     }32 33     /**34      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)35      */36     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {37         // TODO Auto-generated method stub38         39         //form中method若由get改变为post,只需在doPost里调用doGet方法doGet(request, response);就可以,不需要重新写方法。40         //第一次请求为Get  之后为Post41         42         //接受表单提交的数据,并将数据封装在bean中43         Calculator cal=new Calculator();44 45         cal.setNum1(Float.parseFloat(request.getParameter("num1")));46         cal.setOp(request.getParameter("op"));47         cal.setNum2(Float.parseFloat(request.getParameter("num2")));48         49         float result=0;50         switch(cal.getOp().charAt(0)){51         //根据运算符做相应的操作52          case ‘+‘:53             result=cal.getNum1()+cal.getNum2();54             break;55          case ‘-‘:56             result=cal.getNum1()-cal.getNum2();57             break;58          case ‘*‘:59             result=cal.getNum1()*cal.getNum2();60             break;61          case ‘/‘:62             result=cal.getNum1()/cal.getNum2();63         }64         65         //将对象和结果保存到request域中66         //之后的模型中时常会用到传递 整个对象67         request.setAttribute("newCal",cal);68         request.setAttribute("newResult",result);69         //以服务器端跳转页面70         request.getRequestDispatcher("cal.jsp").forward(request, response);71         72     /*73     1.服务器端跳转              作用域为request  有返回结果而且地址栏改变,变为现页面            请求转发74       request.getRequestDispatcher("cal.jsp").forward(request, response);75     2.客户端跳转(2次请求)      作用域为session  无返回结果而且地址栏不变,仍是前一个页面  请求重定向 76       request.sendRedirect("cal.jsp");77      */78     }79 80 }

四:项目建立结果及运行结果:

      技术分享

     技术分享

 

     技术分享

     技术分享

     技术分享

 

使用MVC实现计算器功能