博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
传统MVC实例
阅读量:6230 次
发布时间:2019-06-21

本文共 7499 字,大约阅读时间需要 24 分钟。

 

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 HashMap
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
(); 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";    }}

 

 

 

转载于:https://www.cnblogs.com/xiaoxian1369/archive/2012/08/30/2662988.html

你可能感兴趣的文章
Python random模块
查看>>
nagios 详细部署操作(二)
查看>>
流程式编程
查看>>
小蚂蚁学习APP接口开发(5)—— APP接口实例——单例模式连接数据库
查看>>
windows7怎么设置并链接“L2TP ***”
查看>>
大学学生会的腐败怪象
查看>>
LAMP平台详述
查看>>
我的友情链接
查看>>
AsyncTask研究
查看>>
Oracle监听器启动出错:本地计算机上的OracleOraDb10g_home1TNSListener服务启动后又停止了解决方案...
查看>>
ibatis运行的SQL语句的输出——通过配置log4j
查看>>
maven常见问题问答(超全面)
查看>>
JSP中获取各种路径的方法
查看>>
linux 特殊权限 之 SUID 实例
查看>>
linux操作命令
查看>>
Capture Nx
查看>>
RedHat/CentOS命令记录
查看>>
git 学习
查看>>
MySQL基于LVM快照的备份恢复
查看>>
庞升东:个人网站年广告销售收入可超千万
查看>>