index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>计算器</title> <script type="text/javascript" src="index.js" charset="gbk"></script> <script type="text/javascript"> function checkNum(tag) { var value = tag.value; var len = value.length; for ( var i = 0; i < len; i++) { if (value.charAt(i) < '0' || value.charAt(i) > '9') { tag.value = ""; tag.blur(); tag.focus(); break; } } } </script> </head> <body> <br /> <h1>算术计算器</h1> <form action="#" method="post" id="calcform"> <table> <tbody> <tr> <td>第一个参数:</td> <td><input type="text" name="num1" onblur="checkNum(this)" /> </td> </tr> <tr> <td>第二个参数:</td> <td><input type="text" name="num2" onblur="checkNum(this)" /> </td> </tr> <tr> <td>操作</td> <td><input type="button" value="加" onclick="add()" /> <input type="button" value="减" onclick="subtract()" /> <input type="button" value="乘" onclick="multiply()" /> <input type="button" value="除" onclick="divide()" /></td> </tr> </tbody> </table> </form> </body> </html>
index.js
/** * */ function add() { with (document.forms['calcform']) { action = 'add.action'; submit(); } } function subtract() { with (document.forms['calcform']) { action = 'subtract.action'; submit(); } } function multiply() { with (document.forms['calcform']) { action = 'multiply.action'; submit(); } } function divide() { with (document.forms['calcform']) { action = 'divide.action'; submit(); } }
Controller.java
package com.wangzhu.servlet; import java.io.IOException; import java.util.HashMap; import javax.servlet.ServletConfig; 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 com.wangzhu.action.Action; import com.wangzhu.action.inter.AddAction; import com.wangzhu.action.inter.DivideAction; import com.wangzhu.action.inter.MultiplyAction; import com.wangzhu.action.inter.SubtractAction; /** * Servlet implementation class Controller */ @WebServlet("/Controller") public class Controller extends HttpServlet { private static final long serialVersionUID = 1L; private HashMap<String, Action> actionMap; /** * @see HttpServlet#HttpServlet() */ public Controller() { super(); // TODO Auto-generated constructor stub } /** * @see Servlet#init(ServletConfig) */ public void init(ServletConfig config) throws ServletException { actionMap = new HashMap<String, Action>(); actionMap.put("add", new AddAction()); actionMap.put("subtract", new SubtractAction()); actionMap.put("multiply", new MultiplyAction()); actionMap.put("divide", new DivideAction()); } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse * response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String path = request.getServletPath(); Action action = determinActionByPath(path); String resultView = action.execute(request, response); if (null != resultView) { request.getRequestDispatcher(resultView).forward(request, response); } } private Action determinActionByPath(String path) { String actionName = path.substring(path.lastIndexOf("/") + 1, path.length() - 7); Action action = actionMap.get(actionName); return action; } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse * response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } }
Calculator.java
package com.wangzhu.calc; public class Calculator { public double add(double a, double b) { return a + b; } public double subtract(double a, double b) { return a - b; } public double multiply(double a, double b) { return a * b; } public double divide(double a, double b) { return a / b; } }
action.java
package com.wangzhu.action; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public interface Action { String execute(HttpServletRequest request, HttpServletResponse response); }
AddAction.java
package com.wangzhu.action.inter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.wangzhu.action.Action; import com.wangzhu.calc.Calculator; public class AddAction implements Action { Calculator calc = new Calculator(); @Override public String execute(HttpServletRequest request, HttpServletResponse response) { String stra = request.getParameter("num1"); String strb = request.getParameter("num2"); double numa = 0, numb = 0; if (null != stra && null != strb && !"".equals(stra) && !"".equals(strb)) { numa = Double.parseDouble(stra); numb = Double.parseDouble(strb); } request.setAttribute("result", calc.add(numa, numb)); request.setAttribute("msg", "+"); return "result.jsp"; } }
SubtractAction.java
package com.wangzhu.action.inter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.wangzhu.action.Action; import com.wangzhu.calc.Calculator; public class SubtractAction implements Action { Calculator calc = new Calculator(); @Override public String execute(HttpServletRequest request, HttpServletResponse response) { String stra = request.getParameter("num1"); String strb = request.getParameter("num2"); double numa = 0, numb = 0; if (null != stra && null != strb && !"".equals(stra) && !"".equals(strb)) { numa = Double.parseDouble(stra); numb = Double.parseDouble(strb); } request.setAttribute("result", calc.subtract(numa, numb)); request.setAttribute("msg", "-"); return "result.jsp"; } }
MultiplyAction.java
package com.wangzhu.action.inter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.wangzhu.action.Action; import com.wangzhu.calc.Calculator; public class MultiplyAction implements Action { Calculator calc = new Calculator(); @Override public String execute(HttpServletRequest request, HttpServletResponse response) { String stra = request.getParameter("num1"); String strb = request.getParameter("num2"); double numa = 0, numb = 0; if (null != stra && null != strb && !"".equals(stra) && !"".equals(strb)) { numa = Double.parseDouble(stra); numb = Double.parseDouble(strb); } request.setAttribute("result", calc.multiply(numa, numb)); request.setAttribute("msg", "*"); return "result.jsp"; } }
DivideAction.java
package com.wangzhu.action.inter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.wangzhu.action.Action; import com.wangzhu.calc.Calculator; public class DivideAction implements Action { Calculator calc = new Calculator(); @Override public String execute(HttpServletRequest request, HttpServletResponse response) { String stra = request.getParameter("num1"); String strb = request.getParameter("num2"); double numa = 0, numb = 0; if (null != stra && null != strb && !"".equals(stra) && !"".equals(strb)) { numa = Double.parseDouble(stra); numb = Double.parseDouble(strb); } request.setAttribute("result", calc.divide(numa, numb)); request.setAttribute("msg", "/"); return "result.jsp"; } }