第十一章 Servlet MVC模式

内包含案例,基于jsp+servlet的:MVC模式计算器;MVC模式登陆

第十一章 Servlet MVC模式

模型-视图-控制器(model-view-controller),简称MVC。MVC是一种先进的设计模式,它的核心思想是有效地组合“视图”、“模型”和“控制器”。掌握MVC模式对于设计合理的Web应用框架有着十分重要的意义。

MVC是一种通过三个不同部分构造一个软件或组件的理想办法:

  • l  模型(model)用于存储数据的对象
  • l  视图(view)向控制器提交所需数据、显示模型中的数据
  • l  控制器(controller)负责具体的业务逻辑操作,即控制器根据视图提出的要求对数据做出处理,将有关结果存储到模型中,并负责让模型和视图进行必要的交互,当模型中的数据变化时,让视图更新显示。

从面向对象的角度看,MVC结构可以使程序更具有对象化特性,也更容易维护和扩展。

 

在JSP技术中,“视图”、“模型”和“控制器”的具体实现如下:

模型   一个或多个Javabean对象,用于存储数据,Javabean主要提供简单的setXXX方法和getXXX方法,在这些方法中不涉及对数据的具体处理细节,以便增强模型的通用性。

视图   一个或多个JSP页面,其作用主要是向控制器提交必要的数据和为模型提供数据显示,JSP页面主要使用HTML标记和Javabean标记来显示数据。

控制器   一个或多个servlet对象,根据视图提交的要求进行数据处理操作,并将有关的结果存储到Javabean中,然后servlet使用重定向方式请求视图中的某个JSP页面更新显示,即让该JSP页面通过使用Javabean标记显示控制器存储在Javabean中的数据。

 

JSP开发web项目的两种模型(Model1/Model2)

Model1JSP+JavaBeans模式):

  • 使用JSP+JavaBeans将页面显示和业务逻辑处理分开
  • JSP实现页面显示,响应请求并将结果返回给客户
  • JavaBean对象保存数据和实现业务逻辑

优点:实现了页面显示与业务逻辑的分离

缺点:需要在JSP页面控制流程转向并且调用JavaBean代码;业务逻辑复杂时,JSP编写变得复杂

 

案例

Jsp+javaBeans模式简单计算器:

Calculator.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" %>

"Content-Type" content="text/html; charset=UTF-8">

calculator

 

"calculator" class="com.silvan.pojo.CalculatorDemo1" scope="page">

"*" name="calculator"/>

 

计算结果是 :${calculator.firstNum } ${calculator.operator } ${calculator.secondNum } = ${calculator.result }


简单的计算器

"/lesson11/calculator.jsp" method="post" οnsubmit="checkForm();">

   

       

           

           

       

       

           

           

       

       

           

           

       

       

           

       

   

第一个参数

               

                "text" name="firstNum" id="first"/>

           

运算符

               

           

第二个参数

                "text" name="secondNum" id="second" />

           

"2">

                "submit" value="计算"/>

           

 

  CalculatorDemo1.java

package com.silvan.pojo;

/**

 * 計算類

 * @author Administrator

 */

public class CalculatorDemo1 {

    private String firstNum = "0"; // 参数一

    private String secondNum = "0"; // 参数二

    private String operator = "+"; // 运算符

    private double result; // 运算结果

 

    public String getFirstNum() {

        return firstNum;

    }

 

    public void setFirstNum(String firstNum) {

        this.firstNum = firstNum;

    }

 

    public String getSecondNum() {

        return secondNum;

    }

 

    public void setSecondNum(String secondNum) {

        this.secondNum = secondNum;

    }

 

    public String getOperator() {

        return operator;

    }

 

    public void setOperator(String operator) {

        this.operator = operator;

    }

 

    public double getResult() {

        // 将运算逻辑写到这里

        Double a = Double.parseDouble(firstNum);

        Double b = Double.parseDouble(secondNum);

        Double result = 0.0;

        if (operator.equals("+")) {

            result = a + b;

        } else if (operator.equals("-")) {

            result = a - b;

        } else if (operator.equals("*")) {

            result = a * b;

        } else if (operator.equals("/")) {

            result = a / b;

        }

        return result;

    }

 

    public void setResult(double result) {

        this.result = result;

    }

}

 

模型2(MVC模式也就是JSP+JavaBeans+servlet模式

  • 将模型1中JSP嵌入的流程控制和部分逻辑处理代码提取至一个单独的角色:控制器
  • 模型2是MVC架构模式在WEB开发中的应用

 

 

案例1

JSP中的MVC模式计算器

视图层Calculator.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" %>

"Content-Type" content="text/html; charset=UTF-8">

calculator

简单的计算器

"/lesson11/CalculatorServletDemo2" method="post" οnsubmit="checkForm();">

   

       

           

           

       

       

           

           

       

       

           

           

       

       

           

       

   

第一个参数

               

                "text" name="firstNum" id="first"/>

           

运算符

               

           

第二个参数

                "text" name="secondNum" id="second" />

           

"2">

                "submit" value="计算"/>

           

控制层CalculatorServletDemo2.java

package com.silvan.servlet;

 

import java.io.IOException;

 

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

import com.silvan.pojo.CalculatorDemo2;

 

public class CalculatorServletDemo2 extends HttpServlet {

 

                /**

                 * Constructor of the object.

                 */

                public CalculatorServletDemo2() {

                   super();

                }

 

                /**

                 * Destruction of the servlet.

                 */

                public void destroy() {

                   super.destroy(); // Just puts "destroy" string in log

                   // Put your code here

                }

 

                /**

                 * The doGet method of the servlet.

                 *

                 * This method is called when a form has its tag value method equals to get.

                 *

                 * @param request the request send by the client to the server

                 * @param response the response send by the server to the client

                 * @throws ServletException if an error occurred

                 * @throws IOException if an error occurred

                 */

                public void doGet(HttpServletRequest request, HttpServletResponse response)

                            throws ServletException, IOException {

                   //取出计算值

                   double firstNum = Double.parseDouble(request.getParameter("firstNum"));

                   double secondNum = Double.parseDouble(request.getParameter("secondNum"));

                   String operator = request.getParameter("operator");

                   double result = 0;

                   //计算结果

                   if (operator.equals("+")) {

                            result = firstNum + secondNum;

                   } else if (operator.equals("-")) {

                            result = firstNum - secondNum;

                   } else if (operator.equals("*")) {

                            result = firstNum * secondNum;

                   } else if (operator.equals("/")) {

                            result = firstNum / secondNum;

                   }

                   //将数据存入bean中

                   CalculatorDemo2 dataBean = new CalculatorDemo2();

                   dataBean.setFirstNum(firstNum);

                   dataBean.setSecondNum(secondNum);

                   dataBean.setOperator(operator);

                   dataBean.setResult(result);

                   request.setAttribute("bean", dataBean);

                   //跳转到结果显示界面

                   request.getRequestDispatcher("/demo2/showResult.jsp").forward(request, response);

                  

                }

 

                /**

                 * The doPost method of the servlet.

                 *

                 * This method is called when a form has its tag value method equals to post.

                 *

                 * @param request the request send by the client to the server

                 * @param response the response send by the server to the client

                 * @throws ServletException if an error occurred

                 * @throws IOException if an error occurred

                 */

                public void doPost(HttpServletRequest request, HttpServletResponse response)

                            throws ServletException, IOException {

                   doGet(request,  response);

                }

 

                /**

                 * Initialization of the servlet.

                 *

                 * @throws ServletException if an error occurs

                 */

                public void init() throws ServletException {

                   // Put your code here

                }

 

}

 

模型层CalculatorDemo2.java

package com.silvan.pojo;

public class CalculatorDemo2 {

    private double firstNum; // 参数一

    private double secondNum; // 参数二

    private String operator; // 运算符

    private double result; // 运算结果

 

    public double getFirstNum() {

        return firstNum;

    }

 

    public void setFirstNum(double firstNum) {

        this.firstNum = firstNum;

    }

 

    public double getSecondNum() {

        return secondNum;

    }

 

    public void setSecondNum(double secondNum) {

        this.secondNum = secondNum;

    }

 

    public String getOperator() {

        return operator;

    }

 

    public void setOperator(String operator) {

        this.operator = operator;

    }

 

    public double getResult() {

        return result;

    }

 

    public void setResult(double result) {

        this.result = result;

    }

 

}

 

 

视图层showResult.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" %>

"Content-Type" content="text/html; charset=UTF-8">

calculator

运算结果

"bean" type="com.silvan.pojo.CalculatorDemo2" scope="request">

"firstNum" name="bean"/>

"operator" name="bean"/>

"secondNum" name="bean"/>

=

"result" name="bean"/>

案例2

MVC模式实现登录

Login.jsp

   

登陆表单

   

${msg }

   

"/login/LoginServlet" method="post">

      

          

             

             

          

          

             

             

          

          

             

          

      

用户名 "text" name="username" />

             

密码 "password" name="password" />

             

"2">"submit" value="登陆" />

   

Web.xml

    LoginServlet

    com.silvan.servlet.LoginServlet

 

 

 

    LoginServlet

    /LoginServlet

 

LoginServlet.java

    public void doGet(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

 

        // 解决编码问题

        request.setCharacterEncoding("utf-8");

        // 接收表单参数

        String username = request.getParameter("username");

        String password = request.getParameter("password");

 

        // 将form 数据封装 javabean对象

        User user = new User();

        user.setUserName(username);

        user.setPassWord(password);

 

        // 传递数据javabean 给 处理javabean

        // 使用模型完成对用户验证 去创建一个UserModel对象,调用对应的方法

        UserService userService = new UserService();

 

        if (userService.checkUser(user)) {

            // 把用户名放入session,以备后用

            request.getSession().setAttribute("username", username);

            // 合法

            // 转向,跳转方法效率不高

            // response.sendRedirect("welcome.jsp");

            // 因为sendRedirect方法效率不高,在公司常常使用转发方法

            // getRequestDispatcher方法效率高,并且可以把request请求信息往下转发

            request.getRequestDispatcher("/index.jsp").forward(request,

                    response);

        } else {

            // 不合法

            request.setAttribute("msg", "用户名或密码错误!");

            request.getRequestDispatcher("/Login.jsp").forward(request,

                    response);

        }

 

    }

 

    public void doPost(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

        this.doGet(request, response);

       }

User.java

public class User {

    private String userName;

    private String passWord;

get/set……

}

LoginService.java

/**

 * 业务逻辑层,一般归类于模型层

 * @author Administrator

 *

 */

public class UserService {

    // 验证用户是否存在

    public boolean checkUser(User user) {

        String driver = "oracle.jdbc.driver.OracleDriver";

        String url = "jdbc:oracle:thin:@localhost:1521:XE";

        String username = "zhou";

        String password = "123456";

        boolean flag = false;

        Connection conn = null;

        PreparedStatement pstmt = null;

        ResultSet rs = null;

        try {

            //加载数据库连接驱动类

            Class.forName(driver);

            //获取数据库连接

            conn = DriverManager.getConnection(url,username,password);

            // 执行查询

            String sql = "select * from USER1 where username='"

                    + user.getUserName() + "'";

            // 创建prepareStatement对象

            pstmt = conn.prepareStatement(sql);

            //得到查询结果

            rs = pstmt.executeQuery();

            System.out.println("sql=" + sql);

            //处理查询结果

            if (rs.next()) {

                if (user.getPassWord().equals(rs.getString("pwd"))) {

                    // 登录成功

                    // 将得到的数据传给下一个页面

                    // 1,cookie 2,session 3 response.sendRedirect

                    flag = true;

                }

            }

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            //关闭数据库连接

            try {

                if (pstmt!=null){

                    pstmt.close();

                }

                if(conn!=null){

                    conn.close();

                }

                if(rs!=null){

                    rs.close();

                }

            } catch (SQLException e) {

                e.printStackTrace();

            }

        }

        return flag;

    }

}

Index.jsp

    ${username }登录成功!

 

转载于:https://www.cnblogs.com/zhouyeqin/p/6813191.html

你可能感兴趣的:(第十一章 Servlet MVC模式)