国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Servlet的監(jiān)聽器

這篇具有很好參考價(jià)值的文章主要介紹了Servlet的監(jiān)聽器。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

Servlet常用的監(jiān)聽器
ServletContextAttributeListener 用來感知ServlerContext對(duì)象屬性變化,比如添加或刪除屬性變化
ServletContextListener 用來感知ServlerContext對(duì)象的創(chuàng)建和銷毀的
ServletRequestListener 可以用來監(jiān)聽感知ServletRequest對(duì)象的創(chuàng)建和銷毀的
ServletRequestAttributeListener 用來感知ServletRequest屬性變化,如何添加或刪除屬性還是替換
HttpSessionListener監(jiān)聽session的變化,常用于統(tǒng)計(jì)在線人數(shù)
HttpSessionAttributeListener用來監(jiān)聽Session屬性變化
HttpSessionBindingListener 用來監(jiān)聽把一個(gè)數(shù)據(jù)綁定到Session對(duì)象 感知監(jiān)聽器
HttpSessionActivationListener用來監(jiān)聽session綁定的對(duì)象鈍化(把對(duì)象持久化磁盤)
常用的有前6個(gè)

package com.sparrow.listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
 * @Author: 訴衷情の麻雀
 * @Description: 當(dāng)一個(gè)類實(shí)現(xiàn)了ServletContextListener 是一個(gè)監(jiān)聽器,該類可以監(jiān)聽的事件
 * 由該類實(shí)現(xiàn)的監(jiān)聽接口決定 該類可以監(jiān)聽ServletContext的創(chuàng)建和銷毀
 * 當(dāng)web應(yīng)用啟動(dòng)時(shí),就會(huì)產(chǎn)生ServletContextEvent事件,會(huì)調(diào)用監(jiān)聽器的對(duì)應(yīng)事件處理方法contextInitialized,同時(shí)會(huì)傳遞事件對(duì)象
 * 我們可以通過ServletContextEvent事件對(duì)象,來獲取需要的信息,然后再進(jìn)行業(yè)務(wù)處理
 * 需要在web.xml配置監(jiān)聽器這樣才能使tomcat知道,底層使用反射機(jī)制
 * @DateTime: 2023/7/5 9:57
 **/
public class SparrowServletContextListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ServletContext servletContext = sce.getServletContext();
        System.out.println("SparrowServletContextListener監(jiān)聽到" + servletContext+ " 被創(chuàng)建...");
        //如果我們獲取到ServletContext對(duì)象進(jìn)行業(yè)務(wù)處理
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        ServletContext servletContext = sce.getServletContext();
        System.out.println("SparrowServletContextListener監(jiān)聽到" + servletContext+ " 被銷毀...");
        //比如可以對(duì)servletContext數(shù)據(jù)進(jìn)行處理 或者日志的管理
        System.out.println("進(jìn)行處理");


    }
}

/**
 * @Author: 訴衷情の麻雀
 * @Description: TODO
 * @DateTime: 2023/7/5 10:10
 **/
public class SparrowServletContextAttributeListener implements ServletContextAttributeListener {
    @Override
    public void attributeAdded(ServletContextAttributeEvent scae) {
        System.out.println("SparrowServletContextAttributeListener監(jiān)聽到"+ scae.getName());
    }

    @Override
    public void attributeRemoved(ServletContextAttributeEvent scae) {
        System.out.println("SparrowServletContextAttributeListener監(jiān)聽到:" + scae.getValue());
    }


    @Override
    public void attributeReplaced(ServletContextAttributeEvent scae) {

    }
}

ServletContextAttributeListener屬性監(jiān)聽器

package com.sparrow.listener;

import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;

/**
 * @Author: 訴衷情の麻雀
 * @Description: TODO
 * @DateTime: 2023/7/5 10:10
 **/
public class SparrowServletContextAttributeListener implements ServletContextAttributeListener {
    @Override
    public void attributeAdded(ServletContextAttributeEvent scae) {
        System.out.println("SparrowServletContextAttributeListener監(jiān)聽到添加屬性:"+ scae.getName()+"=" + scae.getValue());
    }

    @Override
    public void attributeRemoved(ServletContextAttributeEvent scae) {
        System.out.println("SparrowServletContextAttributeListener監(jiān)聽到刪除屬性:" + scae.getName() + "="+ scae.getValue());
    }


    @Override
    public void attributeReplaced(ServletContextAttributeEvent scae) {
        System.out.println("SparrowServletContextAttributeListener監(jiān)聽到修改/替換屬性:" + scae.getName() + "="+ scae.getValue());
    }
}

配置一個(gè)HelloServlet

/**
 * @Author: 訴衷情の麻雀
 * @Description: TODO
 * @DateTime: 2023/7/18 19:59
 **/
public class HelloServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("helloServlet被調(diào)用了");
        ServletContext servletContext = req.getServletContext();
        servletContext.setAttribute("name", "訴衷情の麻雀");
        servletContext.setAttribute("name", "訴衷情の麻雀");
        servletContext.removeAttribute("name");
    }
}

   <servlet>
        <servlet-name>HelloServlet</servlet-name>
        <servlet-class>com.sparrow.servlet.HelloServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>HelloServlet</servlet-name>
        <url-pattern>/helloServlet</url-pattern>
    </servlet-mapping>

Servlet的監(jiān)聽器,JavaWeb,servlet,監(jiān)聽器,過濾器,java

HttpSessionListener

/**
 * @Author: 訴衷情の麻雀
 * @Description: TODO
 * @DateTime: 2023/7/5 10:25
 **/
public class SparrowHttpSessionListener implements HttpSessionListener {

    @Override
    public void sessionCreated(HttpSessionEvent se) {
        HttpSession session = se.getSession();
        //當(dāng)session創(chuàng)建時(shí)設(shè)置一個(gè)生命周期
        session.setMaxInactiveInterval(30);
        System.out.println("SparrowHttpSessionListener監(jiān)聽到session創(chuàng)建= " + session.getId());
    }


    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        HttpSession session = se.getSession();
        System.out.println("SparrowHttpSessionListener監(jiān)聽到session銷毀= " + session.getId());
    }
}

 @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //獲取session
        HttpSession session = req.getSession(); //如果沒有就創(chuàng)建

    }

配置監(jiān)聽器

 <!--配置監(jiān)聽器-->
    <listener>
        <listener-class>com.sparrow.listener.SparrowHttpSessionListener</listener-class>
    </listener>
    <listener>
        <listener-class>com.sparrow.listener.SparrowServletContextListener</listener-class>
    </listener>
    <listener>
        <listener-class>com.sparrow.listener.SparrowServletContextAttributeListener</listener-class>
    </listener>
    <listener>
        <listener-class>com.sparrow.listener.SparrowRequestListener</listener-class>
    </listener>

Servlet的監(jiān)聽器,JavaWeb,servlet,監(jiān)聽器,過濾器,java

HttpSessionAttributeListener

package com.sparrow.listener;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

/**
* @Author: 訴衷情の麻雀
* @Description: TODO
* @DateTime: 2023/7/18 20:26
**/
public class SparrowHttpSessionAttributeListener implements HttpSessionAttributeListener {
   @Override
   public void attributeAdded(HttpSessionBindingEvent se) {
       System.out.println("SparrowHttpSessionAttributeListener 監(jiān)聽到session添加屬性了" + se.getName() + "=" + se.getValue() );
   }

   @Override
   public void attributeRemoved(HttpSessionBindingEvent se) {
       System.out.println("SparrowHttpSessionAttributeListener 監(jiān)聽到session刪除屬性了" + se.getName());
   }

   @Override
   public void attributeReplaced(HttpSessionBindingEvent se) {
       System.out.println("SparrowHttpSessionAttributeListener 監(jiān)聽到session修改屬性了" + se.getName() + "=" + se.getValue() );

   }
}

Servlet的監(jiān)聽器,JavaWeb,servlet,監(jiān)聽器,過濾器,java

ServletRequestListener監(jiān)聽器

作用:監(jiān)聽Request對(duì)象的創(chuàng)建和銷毀
常用于監(jiān)控某個(gè)IP訪問網(wǎng)站頻率/用戶訪問的頁面、資源(日志)

public class SparrowRequestListener implements ServletRequestListener {
    @Override
    public void requestInitialized(ServletRequestEvent sre) {
        System.out.println("監(jiān)聽到request對(duì)象創(chuàng)建");
        ServletRequest servletRequest = sre.getServletRequest();
        System.out.println("訪問IP=" + servletRequest.getRemoteAddr());
        System.out.println("訪問的資源=" + ((HttpServletRequest) servletRequest).getRequestURL());

    }

    @Override
    public void requestDestroyed(ServletRequestEvent sre) {
        System.out.println("監(jiān)聽到request對(duì)象被銷毀");
    }
}

Servlet的監(jiān)聽器,JavaWeb,servlet,監(jiān)聽器,過濾器,java

ServletRequestAttributeListener監(jiān)聽器

作用:監(jiān)聽Request屬性變化文章來源地址http://www.zghlxwxcb.cn/news/detail-581574.html

public class SparrowRequestAttributeListener implements ServletRequestAttributeListener {
    @Override
    public void attributeAdded(ServletRequestAttributeEvent srae) {
        
    }

    @Override
    public void attributeRemoved(ServletRequestAttributeEvent srae) {
        ServletRequestAttributeListener.super.attributeRemoved(srae);
    }

    @Override
    public void attributeReplaced(ServletRequestAttributeEvent srae) {
        ServletRequestAttributeListener.super.attributeReplaced(srae);
    }
}

到了這里,關(guān)于Servlet的監(jiān)聽器的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • Spring中最簡(jiǎn)單的過濾器和監(jiān)聽器

    Spring中最簡(jiǎn)單的過濾器和監(jiān)聽器

    ? ??????Filter也稱之為過濾器,它是Servlet技術(shù)中最實(shí)用的技術(shù),Web開發(fā)人員通過Filter技術(shù),對(duì)web服務(wù)器管理的所有web資源:例如Jsp, Servlet, 靜態(tài)圖片文件或靜態(tài) html 文件等進(jìn)行攔截,從而實(shí)現(xiàn)一些特殊的功能。例如實(shí)現(xiàn)URL級(jí)別的權(quán)限訪問控制、過濾敏感詞匯、壓縮響應(yīng)信息

    2024年02月14日
    瀏覽(20)
  • 高手速成 | 過濾器、監(jiān)聽器的創(chuàng)建與配置

    高手速成 | 過濾器、監(jiān)聽器的創(chuàng)建與配置

    ? ?本節(jié)講解過濾器、監(jiān)聽器的創(chuàng)建以及監(jiān)聽事件配置示例。 【例1】創(chuàng)建過濾器及配置過濾規(guī)則。 (1) 在Eclipse中新建一個(gè)Web項(xiàng)目,取名為Chapt_09。在src目錄下,新建一個(gè)名為com.test.filter的包。選中該包并按Ctrl+N組合鍵,在彈出的菜單中選擇Web→Filter。選擇創(chuàng)建過濾器,如圖

    2024年02月06日
    瀏覽(96)
  • Springboot中使用攔截器、過濾器、監(jiān)聽器

    Springboot中使用攔截器、過濾器、監(jiān)聽器

    Javaweb三大組件:servlet、Filter(過濾器)、?Listener(監(jiān)聽器) SpringBoot特有組件:Interceptor(攔截器) 過濾器、攔截器、監(jiān)聽器、AOP(后續(xù)文章介紹)、全局異常處理器(后續(xù)文章介紹)是搭建系統(tǒng)框架時(shí),經(jīng)常用到的部分,全局異常處理器的作用很明顯,就是處理接口執(zhí)行

    2024年02月03日
    瀏覽(25)
  • SpringBoot2.0(過濾器,監(jiān)聽器,攔截器)

    SpringBoot2.0(過濾器,監(jiān)聽器,攔截器)

    使用Servlet3.0的注解進(jìn)行配置 啟動(dòng)類里面增加 @ServletComponentScan ,進(jìn)行掃描 新建一個(gè)Filter類,implements Filter ,并實(shí)現(xiàn)對(duì)應(yīng)接口 @WebFilter 標(biāo)記一個(gè)類為Filter,被spring進(jìn)行掃描 urlPatterns:攔截規(guī)則,支持正則 控制chain.doFilter的方法的調(diào)用,來實(shí)現(xiàn)是否通過放行, 不放行的話,web應(yīng)用

    2024年02月07日
    瀏覽(19)
  • springbboot攔截器,過濾器,監(jiān)聽器及執(zhí)行流程

    springbboot攔截器,過濾器,監(jiān)聽器及執(zhí)行流程

    過濾器是在請(qǐng)求進(jìn)入容器后,但請(qǐng)求進(jìn)入servlet之前進(jìn)行預(yù)處理的。請(qǐng)求結(jié)束返回也是,是在servlet處理完后,返回給前端之前 請(qǐng)求按照上圖進(jìn)入conteoller后執(zhí)行完再返回 過濾器是Servlet規(guī)范中定義的一種組件,可以用于在請(qǐng)求進(jìn)入Web應(yīng)用程序之前或響應(yīng)離開Web應(yīng)用程序之前對(duì)請(qǐng)

    2024年02月13日
    瀏覽(20)
  • 033-安全開發(fā)-JavaEE應(yīng)用&SQL預(yù)編譯&Filter過濾器&Listener監(jiān)聽器&訪問控制

    033-安全開發(fā)-JavaEE應(yīng)用&SQL預(yù)編譯&Filter過濾器&Listener監(jiān)聽器&訪問控制

    1、JavaEE-JDBC-SQL預(yù)編譯 2、JavaEE-HTTP-Filter過濾器 3、JavaEE-對(duì)象域-Listen監(jiān)聽器 演示案例: ?JavaEE-預(yù)編譯-SQL ?JavaEE-過濾器-Filter ?JavaEE-監(jiān)聽器-Listen 提前編譯好執(zhí)行邏輯,你注入的語句不會(huì)改變?cè)羞壿嫞?預(yù)編譯寫法: safesql 是一個(gè)預(yù)編譯的 SQL 查詢語句,其中 ? 是一個(gè)占位符

    2024年02月22日
    瀏覽(18)
  • web3j的基礎(chǔ)用法-6合約的監(jiān)聽器事件Event和過濾器EthFilter,以及NullPointed,調(diào)用失敗導(dǎo)致的bug解決

    web3j的基礎(chǔ)用法-6合約的監(jiān)聽器事件Event和過濾器EthFilter,以及NullPointed,調(diào)用失敗導(dǎo)致的bug解決

    本篇以Uniswap為例(https://uniswap.org/) 合約地址 :0x1f9840a85d5af5bf1d1762f925bdaddc4201f984 (Uni) 監(jiān)聽合約Tranfer事件 調(diào)用代碼 核心代碼實(shí)現(xiàn)在這里 之前實(shí)驗(yàn)全量區(qū)塊,導(dǎo)致請(qǐng)求多次失敗,是由于個(gè)人RPC節(jié)點(diǎn)的請(qǐng)求和數(shù)據(jù)有限,為了測(cè)試出結(jié)果,從13763721L block到當(dāng)前,結(jié)果毫秒級(jí)返

    2024年02月11日
    瀏覽(29)
  • 【JavaWeb】9—監(jiān)聽器

    【JavaWeb】9—監(jiān)聽器

    ?????? Github主頁??https://github.com/A-BigTree 筆記鏈接??https://github.com/A-BigTree/Code_Learning ?????? 如果可以,麻煩各位看官順手點(diǎn)個(gè)star~?? 如果文章對(duì)你有所幫助,可以點(diǎn)贊??收藏?支持一下博主~?? 可見Java設(shè)計(jì)模式中的觀察者模式。 觀察者:監(jiān)控『被觀察者』

    2023年04月09日
    瀏覽(33)
  • javaweb監(jiān)聽器和juery技術(shù)

    javaweb監(jiān)聽器和juery技術(shù)

    為其添加過濾器 最終到達(dá)的頁面

    2024年02月13日
    瀏覽(16)
  • HttpSessionListener監(jiān)聽器和HttpSessionAttributeListener監(jiān)聽器

    1.作用:監(jiān)聽Session創(chuàng)建或銷毀,即生命周期監(jiān)聽 2.相關(guān)方法: 3.使用場(chǎng)景: 和前面的ServletContextListener等一樣,可以用于監(jiān)控用戶上線和離線 4.代碼 HttpSessionListener監(jiān)聽器 實(shí)現(xiàn)類 HttpSessionAttributeListener監(jiān)聽器 1.作用:監(jiān)聽Session屬性的變化,使用少 2.相關(guān)方法: 3.代碼 監(jiān)聽器 實(shí)

    2024年02月04日
    瀏覽(20)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包