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
點擊登錄

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/290068.html
標籤:java
