2021-07-24

Posted dvdwqq

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2021-07-24相关的知识,希望对你有一定的参考价值。

mvc三层架构

最直观的理解

MVC三层架构
M  业务数据层   DataBase   后厨
V   视图层   jsp js html   迎宾
C  控制层   servlet         老板娘

下面我用实例模拟mvc三层架构的实现

控制层:

package com.orcl.servlet;
import com.orcl.service.IUserService;
import com.orcl.service.UserServiceimpl;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import java.io.IOException;
import java.util.List;
import java.util.Map;
@WebServlet("/testServlet")//id 用前端访问后台
public class TestServlet extends HttpServlet {
    //    创建一个用户的服务
    IUserService userService = new UserServiceimpl();

    @Override
    public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String username = request.getParameter("userName");//接到前端key值
        String password = request.getParameter("passWord");//接到前端key值
//调用modle层进行处理
        List<Map<String, Object>> list = userService.loginCheck(username, password);
        if (list.size() == 0) {
            //跳转到错误页面
            request.getRequestDispatcher("error.jsp").forward(request, response);
        } else {

            //跳转到登录成功页面
            request.getRequestDispatcher("success.jsp").forward(request, response);
        }
    }
}

 视图层:

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
        <title>用户登录</title>

  </head>
  <br>
  <form action="testServlet" method="post">
        用户名:<input type="text" name="userName" />  </br></br>
        密码:<input type="password" name="passWord"  /> </br></br>
       <input type="submit" value="登录">
  </form>
  </body>
</html>

success.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
        <title>登录成功</title>

  </head>
  <br>
  <body>
  登陆成功
  </body>
</html>

 error.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
        <title>登陆失败</title>

  </head>
  <br>
  <body>
  登录失败
  </body>
</html>

 业务数据层:

package com.orcl.service;

import com.orcl.dao.DaoImpl;
import com.orcl.dao.IDao;

import java.io.IOException;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;
import java.util.Map;

public class UserServiceimpl implements IUserService {
    IDao dao=new DaoImpl();
    @Override
    public List<Map<String, Object>> loginCheck(String userName, String passWord) {
        String sql="select * from student where name=? and password=? ";
        int[] types=new int[]{Types.VARCHAR,Types.VARCHAR};
        Object []values=new Object[]{userName,passWord};
        List<Map<String,Object>> list=null;
        try {
           list=dao.executeQueryForList(sql,types,values);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return  list;
    }
}

 如图已经搭建好三层架构 我链接的mysql数据库

 项目演示:

运行TestServlet

在网页输出:

 输入

小乔

123456

点击登录

 输入错误数据:

小乔

123

点击登录

 

以上是关于2021-07-24的主要内容,如果未能解决你的问题,请参考以下文章

2021-07-24

2021-07-24

2021-07-24

2021-07-24 .NET高级班 118-直播项目专题(阿里云实现短信验证码发送)

[2021.07.24]《骑马与砍杀2:霸主》已更新v1.5.10Hotfix版本 - 整合版

微信小程序代码片段