最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JavaWeb的監(jiān)聽器和過濾器你了解嗎

 更新時(shí)間:2022年02月28日 16:28:56   作者:做最幸福的  
這篇文章主要為大家詳細(xì)介紹了JavaWeb的監(jiān)聽器和過濾器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助

1.監(jiān)聽器---->Context,Session

what is listener?

監(jiān)聽器是一個(gè)接口內(nèi)容由我們實(shí)現(xiàn),會(huì)在特定時(shí)間被調(diào)用,監(jiān)聽器用于監(jiān)聽web應(yīng)用中三大域?qū)ο?request,session,application),信息的創(chuàng)建,銷毀,增加,修改,刪除等動(dòng)作的發(fā)生,然后做出相應(yīng)的響應(yīng)處理。當(dāng)范圍對(duì)象的狀態(tài)發(fā)生變化的時(shí)候,服務(wù)器自動(dòng)調(diào)用監(jiān)聽器對(duì)象中的方法。常用于統(tǒng)計(jì)在線人數(shù)和在線用戶,系統(tǒng)加載時(shí)進(jìn)行信息初始化,統(tǒng)計(jì)網(wǎng)站的訪問量等。

ContextListener通過實(shí)現(xiàn)ServletContextListener來進(jìn)行全局監(jiān)聽

ContextListener可以通過記錄用戶訪問網(wǎng)站的次數(shù)思路:用戶通過訪問index.jsp,來獲取存放在監(jiān)聽器中的hashmap< String,Integer>,然后在index.jsp中進(jìn)行判斷。

ContextListener的代碼思路如下:

public class ContextListener1 implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        System.out.println("init");
        //創(chuàng)建map集合
        Map<String, Integer> map = new HashMap<String, Integer>();
        //    獲取全局對(duì)象
        ServletContext context = servletContextEvent.getServletContext();
        context.setAttribute("map", map);
        System.out.println(map.isEmpty());
        System.out.println(map);
    }
    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        System.out.println("destory");
    }
}

index.jsp的代碼思路如下:

<%
    //獲取用戶ip地址
    String ServerName = request.getServerName();
    //獲取全局對(duì)象
    Map<String, Integer> map = (Map<String, Integer>) application.getAttribute("map");
    if (map.containsKey(ServerName)) {
        map.put(ServerName, map.get(ServerName) + 1);
    } else {
        map.put(ServerName, 1);
    }
    int count = map.get(ServerName);
    int size = map.size();
%>
<h4>ip地址是:<%=ServerName%>,您是第<%=count%>位訪問的用戶,當(dāng)前服務(wù)器共被<%=size%>個(gè)用戶訪問過</h4>

2.監(jiān)聽器三大作用域

在這里插入圖片描述

在這里插入圖片描述

3.屬性監(jiān)聽器

屬性監(jiān)聽器主要監(jiān)聽屬性值的變化,例如request.setAttribute()等這些數(shù)據(jù)的變化。

package listener;
import javax.servlet.*;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
/**
 * @author wjs
 * @create 2022-02-27 15:09
 */
public class AttrListener implements ServletContextAttributeListener, ServletRequestAttributeListener, HttpSessionAttributeListener {
    @Override
    public void attributeAdded(ServletContextAttributeEvent servletContextAttributeEvent) {
    //    向全局作用域中添加值的監(jiān)聽器
    }
    @Override
    public void attributeRemoved(ServletContextAttributeEvent servletContextAttributeEvent) {
    //    向全局作用域刪除值的監(jiān)聽器
    }
    @Override
    public void attributeReplaced(ServletContextAttributeEvent servletContextAttributeEvent) {
    //    向全局域?qū)ο笮薷闹档谋O(jiān)聽器
    }
    @Override
    public void attributeAdded(ServletRequestAttributeEvent servletRequestAttributeEvent) {
    //    向request域中添加值的監(jiān)聽器
    }
    @Override
    public void attributeRemoved(ServletRequestAttributeEvent servletRequestAttributeEvent) {
    //    向request域中刪除值的監(jiān)聽器
    }
    @Override
    public void attributeReplaced(ServletRequestAttributeEvent servletRequestAttributeEvent) {
    //    向request域中修改值的監(jiān)聽器
    }
    @Override
    public void attributeAdded(HttpSessionBindingEvent httpSessionBindingEvent) {
    //    向session域中添加值的監(jiān)聽器
    }
    @Override
    public void attributeRemoved(HttpSessionBindingEvent httpSessionBindingEvent) {
    //    向session域中刪除值的監(jiān)聽器
    }
    @Override
    public void attributeReplaced(HttpSessionBindingEvent httpSessionBindingEvent) {
    //    向session域中修改值的監(jiān)聽器
    }
}

4.過濾器

在這里插入圖片描述

4.1過濾器的使用

1.編寫java	類實(shí)現(xiàn)Filter接口
2.重寫doFilter()方法
3.設(shè)置攔截的url

4.2過濾器的攔截路徑

/*:根目錄下所有請(qǐng)求都攔截

/*.do:所有帶.do的請(qǐng)求都攔截

/*.jsp

4.3過濾器的攔截順序

過濾器的攔截順序,取決于在配置文件web.xml的先后順序

4.4過濾器的四種攔截方式

在這里插入圖片描述
在這里插入圖片描述

總結(jié)

本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容! 

相關(guān)文章

最新評(píng)論

师宗县| 大安市| 沅陵县| 兴安县| 临西县| 洮南市| 临桂县| 甘洛县| 利川市| 阿巴嘎旗| 内黄县| 阜新市| 鹤岗市| 来凤县| 蒙自县| 玛多县| 富民县| 白朗县| 全椒县| 霸州市| 尉氏县| 富平县| 揭西县| 师宗县| 万载县| 东城区| 安国市| 遂平县| 特克斯县| 法库县| 浦东新区| 扶风县| 利辛县| 宁城县| 句容市| 从江县| 油尖旺区| 苏尼特左旗| 顺昌县| 平邑县| 龙南县|