基于MVC架构登录案例分析研究

基于MVC架构登录案例分析研究

 

  要:应用程序中的一些复杂问题例如缓存问题、持久性问题和系统响应速度问题等可以借助MVC(Model-view-Controller)架构加以缓解。MVC要实现的目标是将软件用户界面和业务逻辑分离以使代码可扩展性、可复用性、可维护性、灵活性加强。本文在分析JSPServlet MVC 基本原理和交互过程的基础上,以简单的登录为例,利用Servlet+JSP 实现模块设计,MVC 架构实现展示层和逻辑层分离,实现高内聚低耦合。

关键词:登录;Servlet;JSP;MVC框架

Logon case study based on MVC architecture

Abstract: Some complex problems in the application, such as caching problems, persistence problems and system response speed problems, can be alleviated with the help of MVC(Model-View-Controller) architecture. The goal of MVC is to separate the software user interface from the business logic to make the code more extensible, reusable, maintainable, and flexible. Based on the analysis of JSP, Servlet and MVC basic principle and interaction process, this paper takes simple login as an example, USES Servlet+JSP to realize module design, MVC architecture to realize separation of presentation layer and logic layer, and achieve high cohesion and low coupling.

Key words: login; The Servlet. The JSP. The MVC framework

一、环境搭建

登录案例是基于B/S 结构(Browser/ Server)的Web 应用系统,系统设计过程中主要针对服务端进行开发。

服务端架构:MVC 模式(即Model、View、Controller),

语言:Java

开发工具:Eclipse

数据库:MySQL5.6

二、MVC运行模式介绍

MVC 是一种使用 MVC(Model、View、Controller 模型-视图-控制器)设计创建 Web 应用程序的模式,是一种框架模式,一个框架中往往含有一个或多个设计模式。

Model(模型)表示应用程序核心(比如数据库记录列表),通常模型对象负责在数据库中存取数据。

View(视图)显示数据(数据库记录),通常视图是依据模型数据创建的。

Controller(控制器)处理输入(写入数据库记录),通常控制器负责从视图读取数据,控制用户输入,并向模型发送数据。

MVC 分层有助于管理复杂的应用程序,因为您可以在一个时间内专门关注一个方面。例如,可以在不依赖业务逻辑的情况下专注于视图设计。同时也让应用程序的测试更加容易。

MVC 分层同时也简化了分组开发。不同的开发人员可同时开发视图、控制器逻辑和业务逻辑。

常见的MVC就是JSP + servlet + javabean+ dao的模式,运行流程见下图。

 

三、登录实例

项目目录如下:

 

 

 1 package org.abyss.dao;
 2 import java.sql.Connection;
 3 import java.sql.DriverManager;
 4 import java.sql.PreparedStatement;
 5 import java.sql.ResultSet;
 6 
 7 import org.abyss.entity.Login;
 8 
 9 //模型层,用于处理登录
10 public class LoginDao {
11 
12     public static int login(Login login){
13         Connection connection = null;
14         PreparedStatement pstmt = null;
15         ResultSet rs = null;
16 
17         try {
18 
19             String URL = "jdbc:mysql://localhost:3306/mydata?serverTimezone=GMT%2B8";
20 //mydata是数据库名
21             String USERNAME = “你的的数据库用户名,一般是root";
22             int result = -1;
23             String PWD = "填入数据库用户名密码";
24             Class.forName("com.mysql.cj.jdbc.Driver");
25             connection = DriverManager.getConnection(URL,USERNAME,PWD);
26             String sql = "select count(*) from login where uname=? and upwd=?";
27             pstmt = connection.prepareStatement(sql);
28             pstmt.setString(1, login.getUname());
29             pstmt.setString(2, login.getUpwd());
30             rs = pstmt.executeQuery();
31             if (rs.next()) {
32                 result = rs.getInt(1);
33                 System.out.println("result="+result);
34             }
35             if (result > 0) {
36                 
37                 return 1;
38 
39             } else if(result==0){
40                 return 0;
41             }else {
42                 return -1;
43             }
44 
45         } catch (Exception e) {
46             e.printStackTrace();
47         } finally {
48             try {
49                 if (pstmt != null)
50                     pstmt.close();
51                 if (connection != null)
52                     connection.close();
53                 if (rs != null)
54                     rs.close();
55             } catch (Exception e2) {
56 
57                 e2.printStackTrace();
58 
59             }
60         }
61         return 0;
62     }
63 }
LoginDao
package org.abyss.entity;

public class Login {
    private int id;
    private String uname;
    private String upwd;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getUname() {
        return uname;
    }
    public void setUname(String uname) {
        this.uname = uname;
    }
    public String getUpwd() {
        return upwd;
    }
    public void setUpwd(String upwd) {
        this.upwd = upwd;
    }
    public Login() {
    }
    public Login( String uname, String upwd) {
        
        this.uname = uname;
        this.upwd = upwd;
    }
    
}
Login
 1 package org.abyss.servlet;
 2 
 3 import java.io.IOException;
 4 import java.sql.SQLException;
 5 
 6 import javax.servlet.ServletException;
 7 import javax.servlet.http.HttpServlet;
 8 import javax.servlet.http.HttpServletRequest;
 9 import javax.servlet.http.HttpServletResponse;
10 
11 import org.abyss.dao.LoginDao;
12 import org.abyss.entity.Login;
13 import org.apache.jasper.tagplugins.jstl.core.Out;
14 
15 //控制器层
16 public class LoginServlet extends HttpServlet {
17     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
18         request.setCharacterEncoding("utf-8");
19         String uname=request.getParameter("uname");
20         String upwd=request.getParameter("upwd");
21         System.out.println(uname+upwd);
22         Login login=new Login(uname, upwd);
23         
24         
25             int result = LoginDao.login(login);
26             if(result>0) {
27                 response.sendRedirect("welcome.jsp");
28             }
29             else 
30                 response.sendRedirect("login.jsp");
31         
32         
33     
34     }
35 
36     
37     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
38         doGet(request, response);
39     }
40 
41 }
LoginServlet
 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html>
 4 <html>
 5 <head>
 6 <meta charset="UTF-8">
 7 <title>登录</title>
 8 </head>
 9 <body>
10     <form action="LoginServlet" method="post">
11         用户名:<input type="text" name="uname"><br/>
12         密码:<input type="password" name="upwd"><br/>
13         <input type="submit" value="登录">
14     
15     </form>
16 </body>
17 </html>
Login
 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html>
 4 <html>
 5 <head>
 6 <meta charset="UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10     登录成功!
11 </body>
12 </html>
welcome

四、简析

在MVC中,Model是被观察的对象,View是观察者,Model层一旦发生变化,View层即被通知更新,在本案例中,

(1)Model收到来自于login.jsp的请求

(2)model向数据库发送请求

(3)数据传回

(4)login.jsp观测到变化(核对id和密码是否合法)

(5)对用户作出回应

此流程中,jsp只做视图的展示,servlet请求控制,JavaBean进行实体类的封装 。高内聚低耦合,将视图和业务代码进行分离,结构清晰,分工明确,万丈高楼平地起,使用mvc模式开发的”地基”的明显易于日后的更新扩展。

五、结束语

本文使用的登录案例,采用MVC 技术,实现了视图层、逻辑层、数据层的分离,使得系统具有良好的扩展性,良好的实现了程序高内聚低耦合的原则。

原文地址:https://www.cnblogs.com/yeshenfeng/p/13112089.html