原文連接:http://www.yiidian.com/jsp/jsp-mvc-model.htmlhtml
MVC,表明模型(Model),視圖(View),控制器(Controller)。這是一種將業務邏輯,表現邏輯和數據分開的設計模式。java
控制器
充當視圖和模型之間的接口。控制器攔截全部傳入的請求。web
模型
表明應用程序的狀態,即數據。它還能夠具備業務邏輯。設計模式
視圖
即UI(用戶界面)。mvc
多個視圖共享一個模型,大大提升代碼的可重用性。
易於維護的大型應用程序app
在下面的示例中,咱們使用Servlet做爲控制器,使用JSP頁面做爲視圖組件,使用Java Bean類做爲模型。yii
<%@ page language="java" contentType="text/html;charset=utf-8" %> <html> <html> <head> <meta charset="utf-8"> <title>一點教程網-JSP的MVC模式</title> </head> <body> <form action="ControllerServlet" method="post"> 用戶名:<input type="text" name="name"><br> 密碼:<input type="password" name="password"><br> <input type="submit" value="登陸"> </form> </body> </html>
package com.yiidian; /** * 一點教程網 - http://www.yiidian.com */ public class User { private String name; private String password; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public boolean validate(){ if(password.equals("admin")){ return true; } else{ return false; } } }
package com.yiidian; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; /** * 一點教程網 - http://www.yiidian.com */ public class ControllerServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); PrintWriter out=response.getWriter(); String name=request.getParameter("name"); String password=request.getParameter("password"); User bean=new User(); bean.setName(name); bean.setPassword(password); request.setAttribute("bean",bean); boolean status=bean.validate(); if(status){ RequestDispatcher rd=request.getRequestDispatcher("login-success.jsp"); rd.forward(request, response); } else{ RequestDispatcher rd=request.getRequestDispatcher("login-error.jsp"); rd.forward(request, response); } } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } }
<%@ page import="com.yiidian.User" contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>一點教程網-JSP的MVC模式</title> </head> <body> <p>你已經成功登陸</p> <% User bean=(User) request.getAttribute("bean"); out.print("歡迎你, "+bean.getName()); %> </body> </html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>一點教程網-JSP的MVC模式</title> </head> <body> <p>抱歉! 用戶名或密碼錯誤</p> <form action="ControllerServlet" method="post"> 用戶名:<input type="text" name="name"><br> 密碼:<input type="password" name="password"><br> <input type="submit" value="登陸"> </form> </body> </html>
<?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_3_1.xsd" version="3.1"> <servlet> <servlet-name>ControllerServlet</servlet-name> <servlet-class>com.yiidian.ControllerServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>ControllerServlet</servlet-name> <url-pattern>/ControllerServlet</url-pattern> </servlet-mapping> </web-app>
輸入錯誤的密碼,顯示以下:jsp
輸入正確的密碼(admin),顯示以下:ide
歡迎關注個人公衆號::一點教程。得到獨家整理的學習資源和平常乾貨推送。
若是您對個人系列教程感興趣,也能夠關注個人網站:yiidian.compost