javabeanmvc模式基于servlet的MVC模式------MVC模式设计四则运算小web应用
本文个人在北京逛街的时候突然想到的...之前就有想写几篇关于javabeanmvc模式的笔记,所以回家到之后就奋笔疾书的写出来发表了
1,懂得mvc计划模式的核心思惟,视图,模型,控制器。把握mvc模式在jsp中的具体体现。
2,在mvc计划模式中,视图由jsp页面负责,模型由JavaBean负责,控制器由servlet负责。
3,模型(Model),一个或多个JavaBean对象,用于存数数据,JavaBean主要供给简略的setXXX()方法和getXXX()方法,在这些方法中不涉及对数据的具体处理细节。
4,视图(view),一个或者多个jsp页面,为模型供给数据展示,jsp页面主要应用HTML页面标记和JavaBean标记来表现数据
5,控制器(Controller):一个或者多个Servlet对象,根据视图提交的要求进行数据处理操作,并将有关结果存储到JavaBean当中,然后Servlet应用重定向方法请求视图中的某个jsp页面的更新表现,即让该jsp页面应用通过JavaBean标记表现控制器存储在JavaBean中的数据
下面是我计划的web应用,有两个jsp页面,inputNumber.jsp和showResult.jsp,一个javaBean和一个Servlet。jsp页面的inputNumber.jsp挺一个表单。,用户可以通过表单输入两个数和四则运算符号。提交给Servlet控制器,javaBean负责存储运算数,运算符号,运算结果。servlet负责四则运算,并将结果存储在javabean当中,并负责请求jsp页面showResult.jsp页面,表现javaBean中的数据。
主要用到的知识,转发。mvc计划思惟。
step1,
inputNumber.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> </head> <body> <form action="operationservlet" method="post"> 请输入数字1 <input type="text" name="number1"> 选择运算符 <input type="radio" name="operation" value="+" checked /> <input type="radio" name="operation" value="-"> <input type="radio" name="operation" value="*"> <input type="radio" name="operation" value="/"> 请输入数字2 <input type="text" name="number2"> <input type="submit" value="提交"> </form> </body> </html>
step2
javaBean文件
public class Operation { private int number1; private String operation; private int number2; private int result; public int getResult() { return result; } public void setResult(int result) { this.result = result; } public int getNumber1() { return number1; } public void setNumber1(int number1) { this.number1 = number1; } public String getOperation() { return operation; } public void setOperation(String operation) { this.operation = operation; } public int getNumber2() { return number2; } public void setNumber2(int number2) { this.number2 = number2; } }
step4
servlet控制器
import java.io.IOException; import java.io.PrintWriter; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import bean.Operation; public class operationServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); Operation oper=new Operation(); request.setAttribute("operation", oper); String number11=request.getParameter("number1"); String operation=request.getParameter("operation"); System.out.println("我爱你"+operation); String number22=request.getParameter("number2"); int number1=Integer.parseInt(number11); int number2=Integer.parseInt(number22); oper.setNumber1(number1); oper.setNumber2(number2); oper.setOperation(operation); if(operation.equals("+")){ oper.setResult(number2+number1); } else if(operation.equals("-")){ oper.setResult(number1-number2); } else if(operation.equals("*")){ oper.setResult(number1*number2); } else{ oper.setResult(number1/number2); } // response.sendRedirect("showResult.jsp"); RequestDispatcher dispatcher=request.getRequestDispatcher("showResult.jsp"); dispatcher.forward(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } }
step4,表现结果
<%@ page language="java" import="java.util.*,bean.Operation" pageEncoding="utf-8"%> <html> <head> </head> <body> <% Operation oper=(Operation)request.getAttribute("operation"); System.out.println(oper); out.println(oper.getResult()); %> </body> </html>
step5,web.xml略
文章结束给大家分享下程序员的一些笑话语录: AdobeFlash拖垮Windows拖垮IE!又拖垮Linux拖垮Ubuntu拖垮FirxEox!还拖垮BSD拖垮MacOS拖垮Safri!简直无所不拖!AdobeFlash滚出网路世界!不要以为市占有率高就可以持续出烂货产品!以后替代品多得是!
---------------------------------
原创文章 By
javabean和mvc模式
---------------------------------