index.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 HashMapactionMap; /** * @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 (); 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"; }}