JavaWeb詳解之(MVC三層架構,過濾器filter,session監聽器)

MVC三層架構

1.什麼是MVC

MVC 是一種使用 MVC(Model View Controller 模型-視圖-控制器)設計創建 Web 應用程序的模式
  • Model(模型)
  • 業務處理 :業務邏輯(Service)
  • 數據持久層:CRUD (Dao)
  • View(視圖)
    • 展示數據
  • 提供鏈接發起Servlet請求 (a,form,img…)
  • Controller(控制器)
    • 接收用戶的請求 :(req:請求參數、Session信息….)
    • 交給業務層處理對應的代碼
  • 控制視圖的跳轉

2.MVC原理圖

在這裏插入圖片描述

3.MVC的優缺點

優點:                                    缺點:
     - 耦合性低                                  - 沒有明確的定義
     - 重用性高                                  - 不適合小型,中等規模的應用程序
     - 生命週期成本低                             - 增加系統結構和實現的複雜性
     - 部署快                                     -視圖與控制器間的過於緊密的連接
     - 可維護性高                                 - 視圖對模型數據的低效率訪問
     - 有利軟件工程化管理                          - 一般高級的界面工具或構造器不支持模式

過濾器(Filter )

1.什麼是Filter

Filter可認爲是Servlet的一種“變種”,它主要用於對用戶請求(HttpServletRequest)進行預處理,也可以對服務器響應(HttpServletResponse)進行後處理,是個典型的處理鏈。
它與Servlet的區別在於:它不能直接向用戶生成響應。
完整的流程是:Filter對用戶請求進行預處理,接着將請求交給Servlet進行處理並生成響應,最後Filter再對服務器響應進行後處理。

在這裏插入圖片描述

2.過濾器鏈

在一個Web應用中,也可以部署多個過濾器,這些過濾器組成了一個過濾器鏈。
過濾器鏈中的每個過濾器負責特定的操作和任務,客戶端的請求可以在這些過濾器之間進行傳遞,直到          達到目標資源。

一個由兩個Filter所組成的過濾器鏈的過濾過程:
在這裏插入圖片描述

注意
在客戶端的請求響應過程中,並不需要經過所有的過濾器鏈,而是根據過濾器鏈中每個過濾器的過濾條件來匹配需要過濾的資源。

3.Filter的開發步驟

  1. 導包
  2. 編寫過濾器:實現filter接口,重寫方法
public class CharacterEncodingFilter implements Filter {
    //初始化:web服務器啓動,就以及初始化了,隨時等待過濾對象出現!
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("CharacterEncodingFilter初始化");
    }
    //Chain : 鏈
    /*    1. 過濾中的所有代碼,在過濾特定請求的時候都會執行
          2. 必須要讓過濾器繼續同行
        chain.doFilter(request,response);     */
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        System.out.println("CharacterEncodingFilter執行前....");
        chain.doFilter(request,response); //讓我們的請求繼續走,如果不寫,程序到這裏就被攔截停止!
        System.out.println("CharacterEncodingFilter執行後....");
    }
    //銷燬:web服務器關閉的時候,過濾會銷燬
    public void destroy() {   System.out.println("CharacterEncodingFilter銷燬");    }
}
  1. 在web.xml中配置 Filter
<filter>
    <filter-name>CharacterEncodingFilter</filter-name>
    <filter-class>com.filter.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <!--只要是 /servlet的任何請求,會經過這個過濾器-->
    <url-pattern>/servlet/*</url-pattern>
    <!--<url-pattern>/*</url-pattern>-->  //所有請求
</filter-mapping>

監聽器(Listener)

在監聽器裏面實際上主要都是針對於屬性的監聽(request、session、application)。
在這裏我們主要講述session監聽

實現session監聽

  1. 實現接口
//統計網站在線人數 : 統計session
public class OnlineCountListener implements HttpSessionListener {
    //創建session監聽: 看你的一舉一動
    //一旦創建Session就會觸發一次這個事件!
    public void sessionCreated(HttpSessionEvent se) {
        ServletContext ctx = se.getSession().getServletContext();
        System.out.println(se.getSession().getId());
        Integer onlineCount = (Integer) ctx.getAttribute("OnlineCount");
        if (onlineCount==null){
            onlineCount = new Integer(1);
        }else {
            int count = onlineCount.intValue();
            onlineCount = new Integer(count+1);
        }
        ctx.setAttribute("OnlineCount",onlineCount);
    }
    //銷燬session監聽
    //一旦銷燬Session就會觸發一次這個事件!
    public void sessionDestroyed(HttpSessionEvent se) {
        ServletContext ctx = se.getSession().getServletContext();
        Integer onlineCount = (Integer) ctx.getAttribute("OnlineCount");
        if (onlineCount==null){
            onlineCount = new Integer(0);
        }else {
            int count = onlineCount.intValue();
            onlineCount = new Integer(count-1);
        }
        ctx.setAttribute("OnlineCount",onlineCount);
    }
    /*
    Session銷燬:
    1. 手動銷燬  getSession().invalidate();
    2. 自動銷燬  在web.xml設置時間
     */
}
  1. web.xml中註冊監聽器
<!--註冊監聽器-->
<listener>
    <listener-class>com.listener.OnlineCountListener</listener-class>
</listener>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章