MVC是三個單詞的縮寫:M,Model(模型);V,View( 視圖 ),C,Control(控制)。html
MVC模式的目的就是實現Web系統的職能分工,java
Model層:實現系統的業務邏輯,即javaBean部分程序員
View層:負責與用戶交互,即在界面上展現數據對象給用戶,即html,jspweb
Control層:Model與View之間溝通的橋樑,它能夠分派用戶的請求並選擇恰當的視圖以用於顯示,同時它也能夠解釋用戶的輸入並將它們映射爲模型層可執行的操做,固然就是Servlet的職責了設計模式
下面咱們用MVC設計模式來實現 簡單的用戶登陸過程服務器
一、控制器Servlet的實現app
系統中只有一個servlet即ControlServlet,全部頁面發起的以" *.do "的請求,都被web.xml配置給ControlServlet進行處理,在ControlServlet中根據‘ * ’的字符串(即解析用戶請求的路徑),調用ActionFactory生成的制定Action對象,在將處理後的URL轉發給用戶。jsp
ControlServlet.javapost
- package cn.netjava.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 cn.netjava.action.Action;
- import cn.netjava.action.ActionFactory;
-
- public class ControlServlet extends HttpServlet {
- private static final long serialVersionUID = 1L;
-
- protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
-
- String pathName =request.getServletPath();
-
- System.out.println("pathName:"+pathName);
-
- int index = pathName.indexOf(".");
- String ActionName = pathName.substring(1, index);
- System.out.println(ActionName);
-
- String ActionClassName = this.getInitParameter(ActionName);
-
- Action action = ActionFactory.getActionFactory().getAction(ActionClassName);
-
- String url = action.execute(request, response);
- if(url==null){
- request.getRequestDispatcher("error.jsp").forward(request, response);
- }else{
- request.getRequestDispatcher(url).forward(request, response);
- }
- }
-
- }
二、Action對象工廠類實現:ui
ActionFactory是一個單實例類(整個系統只須要使用其一個對象),它只提供一個Action對象,經過getAction(String ActionClassName) 的方法調用建立一個Action對象。這個方法在Control中被調用。代碼以下:
- package cn.netjava.action;
- public class ActionFactory {
-
-
- private ActionFactory(){
- }
-
- public static ActionFactory getActionFactory(){
- if(af == null){
- af = new ActionFactory();
- }
- return af;
- }
-
- public Action getAction(String ActionClassName){
- Action action = null;
- try{
- action = (Action) Class.forName(ActionClassName).newInstance();
- }catch(Exception e){
- e.printStackTrace();
- }
- return action;
- }
-
- private static ActionFactory af;
-
-
- }
三、Action接口類定義:
全部的事件處理(即Action)類都必須實現這個接口
- package cn.netjava.action;
-
- public interface Action {
-
- public String execute(javax.servlet.http.HttpServletRequest request,javax.servlet.http.HttpServletResponse response);
-
- }
四、web.xml中配置請求發送給控制器Servlet
最後,咱們只須要在wex.xml中對MVC結構的配置:
視圖頁面中的請求都是以<動做名字>.do結尾,當這個請求到達web服務器後,會被服務器轉向給控制器處理,控制器在根據解析出的動做名,調用對應的Action對象,處理結果,並輸出結果頁面,因此web.xml中必須有以下配置:
- <servlet>
- <servlet-name>controlServlet</servlet-name>
- <servlet-class>cn.netjava.servlet.ControlServlet</servlet-class>
- <init-param>
- <param-name>loginAction</param-name>
- <param-value>cn.netjava.action.LoginAction</param-value>
- </init-param>
- </servlet>
- <servlet-mapping>
- <servlet-name>controlServlet</servlet-name>
- <url-pattern>*.do</url-pattern>
- </servlet-mapping>
四、具體的Action類實現(即對登陸動做進行處理的類)
- package cn.netjava.action;
-
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- public class LoginAction implements Action {
-
- public String execute(HttpServletRequest request,
- HttpServletResponse response) {
-
- String userName = request.getParameter("userName");
- String userPwd = request.getParameter("userPwd");
- if (userName.equals("netjava") && userPwd.equals("netjava")) {
- request.setAttribute("userName", userName);
- return "main.jsp";
- } else {
- return "login.jsp";
- }
- }
-
- }
若是登陸成功,跳轉到 main.jsp頁面,不然,返回login,jsp頁面
如下是main.jsp和login.jsp頁面:
main.jsp
- <%@ page language="java" contentType="text/html; charset=utf-8"
- pageEncoding="utf-8"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>Insert title here</title>
- </head>
- <body>
- <h1 style="color:red"><%=request.getAttribute("userName") %>登陸成功</h1>
- </body>
- </html>
login.jsp
- <%@ page language="java" contentType="text/html; charset=utf-8"
- pageEncoding="utf-8"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>用戶登陸</title>
- </head>
- <body>
- <form action="loginAction.do" method="post">
- 用戶名:<input type="text" name="userName" id="userName"><br>
- 密碼:<input type="password" name="userPwd" id="userPwd"><br>
- <input type="submit" value="登陸"/>
- </form>
- </body>
- </html>
最後作個總結吧:之前咱們與服務器進行交互,可能jsp頁面和servlet中都將html和java代碼參雜在一塊兒,這會致使系統的系統維護困難、分工不清;例如在加有jsp代碼段的網頁中,程序員與美工之間的配合就很是困難!MVC結構的系統會從根本上強制咱們將web系統中的數據對象、業務邏輯、用戶界面三者分離,使得程序員(Java開發人員)集中精力於業務邏輯,界面程序員(HTML和JSP開發人員)集中精力於表現形式上。
出處:http://blog.csdn.net/liupeng900605/article/details/6985344