1. 程式人生 > >MVC設計模式-學習筆記

MVC設計模式-學習筆記

MVC設計模式-學習筆記

1)如果嚴格按照MVC思想的話,瀏覽器首頁訪問的是控制器,然後再由控制器轉發到視圖
2)位於WEB-INF/目錄下的資源,客戶端無法直接訪問,只能通過服務端轉發進入
3)request對象也可用於資源的轉發
request
.getRequestDispatcher("/WEB-INF/success.html")
.forward(request,response);
4)通過正則表達式驗證中文:[\u4E00-\uFA29]+
5)對於重定向而言,前後的二個Request域對象不同,如果取值不到,返回"null",不會報錯

java代碼:

ModelBean

public class ModelBean {
    public boolean validate(String username){
        boolean flag = false;
        //username.matches("[a-zA-Z0-9]+")) 用戶名是數字與字母
        if(username!=null && username.matches("[\u4E00-\uFA29]+")){
            flag = true;
        }
        return flag;
    }
}

Demo8


import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.itcast.web.domain.ModelBean;

public class Demo8 extends HttpServlet {
    //NO1
    public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        //轉發到view.html頁面
        RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/view.html");
        rd.forward(request,response);
    }
    //NO2
    public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String username = request.getParameter("username");
        ModelBean mb = new ModelBean();
        boolean flag = mb.validate(username);
        if(flag){
            //將用戶名綁定到Request域對象中
            request.setAttribute("USERNAME",username);

            /*轉發到ListServlet.java
            request
                .getRequestDispatcher("/ListServlet")
                .forward(request,response);
            */  

            //重定向到ListServlet.java
            response.sendRedirect("/day07/ListServlet");

        }else{
            request
            .getRequestDispatcher("/WEB-INF/fail.html")
            .forward(request,response);
        }
    }
}

ListServlet

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ListServlet extends HttpServlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        String username = (String) request.getAttribute("USERNAME");
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().write("歡迎"+username+"光臨");
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        String username = (String) request.getAttribute("USERNAME");
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().write("歡迎"+username+"光臨");     
    }
}

html

view.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>用戶登錄</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <form action="/day07/Demo8" method="post">
        <table border="1" align="center">
            <caption>用戶登錄</caption>
            <tr>
                <th>用戶名</th>
                <td><input type="text" name="username"/></td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                    <input type="submit" value="提交"/>
                        ????
                    <input type="reset" value="重填"/>
                </td>
            </tr>   
        </table>
    </form>
  </body>
</html>

sucess.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>用戶登錄</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  </head>
  <body>
    登錄成功  
  </body>
</html>

fail.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>用戶登錄</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  </head>
  <body>
    登錄失敗  
  </body>
</html>

MVC設計模式-學習筆記