基于servlet的MVC模式------MVC模式设计四则运算小

当前位置 : 首页 > 网页制作 > CSS > 基于servlet的MVC模式------MVC模式设计四则运算小

基于servlet的MVC模式------MVC模式设计四则运算小

来源: 作者: 时间:2016-02-16 10:04
1,理解mvc设计模式的核心思想,视图,模型,控制器。掌握mvc模式在jsp中的具体体现。2,在mvc设计模式中,视图由jsp页面负责,模型由JavaBean负责,控制器由servlet负责。3,模型(Model)

1,理解mvc设计模式的核心思想,视图,模型,控制器。掌握mvc模式在中的具体体现。

2,在m设计模式中,视图由jsp页面负责,模型由Bean负责,控制器由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> 

<%@ 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文件


[java]
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; 

   

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控制器


[java]
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); 
    } 

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,显示结果


[html]
<%@ 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> 

<%@ 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>

Tag:
网友评论

<