真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

JavaEE基礎(chǔ)(05):過濾器、監(jiān)聽器、攔截器,應(yīng)用詳解-創(chuàng)新互聯(lián)

本文源碼:GitHub·點(diǎn)這里 || GitEE·點(diǎn)這里

疏勒網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián),疏勒網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為疏勒近千家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站制作要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的疏勒做網(wǎng)站的公司定做!

一、Listener監(jiān)聽器

1、概念簡(jiǎn)介

JavaWeb三大組件:Servlet,Listener,F(xiàn)ilter。監(jiān)聽器就是指在應(yīng)用程序中監(jiān)聽相關(guān)對(duì)象狀態(tài)變化的組件。

2、事件源對(duì)象

指被監(jiān)聽對(duì)象。

  • ServletContext

ServletContextListener生命周期監(jiān)聽,它有兩個(gè)方法,出生時(shí)調(diào)用contextInitialized(),銷毀時(shí)調(diào)用contextDestroyed();

ServletContextAttributeListener屬性監(jiān)聽,它有三個(gè)方法,添加屬性attributeAdded(),替換屬性attributeReplaced(),移除屬性時(shí)attributeRemoved()

  • HttpSession

HttpSessionListener生命周期監(jiān)聽:它有兩個(gè)方法,出生時(shí)調(diào)用sessionCreated(),銷毀時(shí)調(diào)用sessionDestroyed();

HttpSessioniAttributeListener屬性監(jiān)聽:它有三個(gè)方法,添加屬性attributeAdded(),替換屬性attributeReplaced(),移除屬性attributeRemoved()。

  • ServletRequest

ServletRequestListener生命周期監(jiān)聽:它有兩個(gè)方法,出生時(shí)調(diào)用requestInitialized(),銷毀時(shí)調(diào)用requestDestroyed()

ServletRequestAttributeListener屬性監(jiān)聽:它有三個(gè)方法,添加屬性attributeAdded(),替換屬性attributeReplaced(),移除屬性attributeRemoved()。

3、編碼案例

  • 相關(guān)監(jiān)聽器

TheContextListener

public class TheContextListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        System.out.println("初始化:TheContextListener");
        ServletContext servletContext = servletContextEvent.getServletContext() ;
        servletContext.setAttribute("author","cicada");
    }
    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        System.out.println("銷毀:TheContextListener");
    }
}

TheRequestListener

public class TheRequestListener implements ServletRequestListener {
    @Override
    public void requestDestroyed(ServletRequestEvent servletRequestEvent) {
        System.out.println("初始化:TheRequestListener");
    }
    @Override
    public void requestInitialized(ServletRequestEvent servletRequestEvent) {
        System.out.println("銷毀:TheRequestListener");
    }
}

TheSessionListener

public class TheSessionListener implements HttpSessionListener {
    @Override
    public void sessionCreated(HttpSessionEvent httpSessionEvent) {
        System.out.println("初始化:TheSessionListener");
    }
    @Override
    public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
        System.out.println("銷毀:TheSessionListener");
    }
}

RequestAttributeListener

public class RequestAttributeListener implements ServletRequestAttributeListener {
    @Override
    public void attributeAdded(ServletRequestAttributeEvent evt) {
        System.out.println("Request添加屬性:"+evt.getName()+";"+evt.getValue());
    }
    @Override
    public void attributeRemoved(ServletRequestAttributeEvent evt) {
        System.out.println("Request移除屬性:"+evt.getName()+";"+evt.getValue());
    }
    @Override
    public void attributeReplaced(ServletRequestAttributeEvent evt) {
        System.out.println("Request替換屬性:"+evt.getName()+";"+evt.getValue());
    }
}
  • web.xml配置文件


    com.node05.servlet.listener.TheContextListener


    com.node05.servlet.listener.TheSessionListener


    com.node05.servlet.listener.TheRequestListener


    com.node05.servlet.listener.RequestAttributeListener


    
    1
  • 測(cè)試接口
public class ListenerServletImpl extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        // 1、獲取TheContextListener初始化數(shù)據(jù)
        ServletContext servletContext = this.getServletContext() ;
        String author = String.valueOf(servletContext.getAttribute("author")) ;
        System.out.println("TheContextListener Author:"+author);
        // 2、Request屬性設(shè)置
        request.setAttribute("mood","smile");
        request.setAttribute("mood","agitated");
        // 3、Session創(chuàng)建,1分鐘失效,調(diào)用銷毀
        HttpSession session = request.getSession(true) ;
        session.setAttribute("casually","casually");
        response.getWriter().print("Hello:Listener");
    }
}

二、Filter過濾器

1、過濾器簡(jiǎn)介

客戶端請(qǐng)求Servlet時(shí),先執(zhí)行相關(guān)Filter,如果Filter通過,則繼承執(zhí)行請(qǐng)求的Servlet;如果Filter不通過,則不會(huì)執(zhí)行用戶請(qǐng)求的Servlet。過濾器可以動(dòng)態(tài)地?cái)r截請(qǐng)求和響應(yīng)。

2、Filter接口

Filter接口定義了三個(gè)核心方法。

  • init()

應(yīng)用程序啟動(dòng)時(shí),服務(wù)器實(shí)例化Filter對(duì)象,并調(diào)用其init方法,讀取web.xml配置,完成對(duì)象的初始化加載。

  • doFilter()

實(shí)際的過濾操作,請(qǐng)求達(dá)到服務(wù)器時(shí),Servlet容器將先調(diào)用過濾器的doFilter方法。

  • destroy()

容器在銷毀過濾器前調(diào)用該方法,釋放過濾器占用的資源。

3、編碼案例

  • 編寫過濾器
public class ThePrintLogFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        String myName = filterConfig.getInitParameter("myName") ;
        System.out.println("myName:"+myName);
    }
    @Override
    public void doFilter(ServletRequest servletRequest,
                         ServletResponse servletResponse,
                         FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest)servletRequest ;
        HttpServletResponse response = (HttpServletResponse)servletResponse ;
        String name = request.getParameter("name") ;
        if (!name.equals("cicada")){
            response.getWriter().print("User Error !");
            return ;
        }
        chain.doFilter(servletRequest,servletResponse);
    }
    @Override
    public void destroy() {
        System.out.println("ThePrintLogFilter destroy()");
    }
}
  • web.xml配置文件


    thePrintLogFilter
    com.node05.servlet.filter.ThePrintLogFilter
    
        myName
        cicada
    


    thePrintLogFilter
    /filterServletImpl
  • 測(cè)試接口
public class FilterServletImpl extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().print("Hello:Filter");
    }
}

三、Interceptor攔截器

Spring框架中的攔截器Interceptor類似于Servlet中的過濾器Filter,主要用于攔截用戶請(qǐng)求并作相應(yīng)的處理。例如通過攔截器可以進(jìn)行權(quán)限驗(yàn)證、記錄請(qǐng)求信息的日志、判斷用戶是否登錄等。請(qǐng)求轉(zhuǎn)發(fā)不執(zhí)行攔截、過濾;重定向執(zhí)行攔截和過濾。

四、源代碼地址

GitHub·地址
https://github.com/cicadasmile/java-base-parent
GitEE·地址
https://gitee.com/cicadasmile/java-base-parent

JavaEE基礎(chǔ)(05):過濾器、監(jiān)聽器、攔截器,應(yīng)用詳解

創(chuàng)新互聯(lián)www.cdcxhl.cn,專業(yè)提供香港、美國(guó)云服務(wù)器,動(dòng)態(tài)BGP最優(yōu)骨干路由自動(dòng)選擇,持續(xù)穩(wěn)定高效的網(wǎng)絡(luò)助力業(yè)務(wù)部署。公司持有工信部辦法的idc、isp許可證, 機(jī)房獨(dú)有T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確進(jìn)行流量調(diào)度,確保服務(wù)器高可用性。佳節(jié)活動(dòng)現(xiàn)已開啟,新人活動(dòng)云服務(wù)器買多久送多久。


網(wǎng)頁(yè)名稱:JavaEE基礎(chǔ)(05):過濾器、監(jiān)聽器、攔截器,應(yīng)用詳解-創(chuàng)新互聯(lián)
URL網(wǎng)址:http://weahome.cn/article/dscihe.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部