欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

淺談java監(jiān)聽器的作用

 更新時(shí)間:2017年09月05日 17:14:27   作者:wwfy  
這篇文章主要介紹了淺談java監(jiān)聽器的作用,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

監(jiān)聽器是JAVA Web開發(fā)中很重要的內(nèi)容,其中涉及到的知識,可以參考下面導(dǎo)圖:

Web監(jiān)聽器

1 什么是web監(jiān)聽器?

web監(jiān)聽器是一種Servlet中的特殊的類,它們能幫助開發(fā)者監(jiān)聽web中的特定事件,比如ServletContext,HttpSession,ServletRequest的創(chuàng)建和銷毀;變量的創(chuàng)建、銷毀和修改等??梢栽谀承﹦?dòng)作前后增加處理,實(shí)現(xiàn)監(jiān)控。

2 監(jiān)聽器常用的用途

  • 通常使用Web監(jiān)聽器做以下的內(nèi)容:
  • 統(tǒng)計(jì)在線人數(shù),利用HttpSessionLisener
  • 加載初始化信息:利用ServletContextListener
  • 統(tǒng)計(jì)網(wǎng)站訪問量
  • 實(shí)現(xiàn)訪問監(jiān)控

3 接下里看看一個(gè)監(jiān)聽器的創(chuàng)建以及執(zhí)行過程

首先需要?jiǎng)?chuàng)建一個(gè)監(jiān)聽器,實(shí)現(xiàn)某種接口,例如我想實(shí)現(xiàn)一個(gè)對在線人數(shù)的監(jiān)控,可以創(chuàng)建如下的監(jiān)聽器:

public class MyListener implements HttpSessionListener{
  private int userNumber = 0;
  public void sessionCreated(HttpSessionEvent arg0) {
    userNumber++;
    arg0.getSession().setAttribute("userNumber", userNumber);
  }
  public void sessionDestroyed(HttpSessionEvent arg0) {
    userNumber--;
    arg0.getSession().setAttribute("userNumber", userNumber);
  }
}

然后在web.xml中配置該監(jiān)聽器,在web-app中添加:

 <listener>
   <listener-class>com.test.MyListener</listener-class>
 </listener>

在JSP中添加訪問人數(shù):

<body>
  在線人數(shù):<%=session.getAttribute("userNumber") %><br/>
</body>

當(dāng)我使用我的瀏覽器訪問時(shí),執(zhí)行結(jié)果如下:

 

當(dāng)打開另一個(gè)瀏覽器訪問時(shí):

 

由于打開另一個(gè)瀏覽器訪問,相當(dāng)于另一個(gè)會(huì)話,因此在線人數(shù)會(huì)增加。

對于3.0版本的Servlet來說,還支持使用注解的方式進(jìn)行配置。

那么接下來看看都有哪些監(jiān)聽器以及方法吧!

監(jiān)聽器的分類

1 按照監(jiān)聽的對象劃分:

按照監(jiān)聽對象的不同可以劃分為三種:

ServletContext監(jiān)控:對應(yīng)監(jiān)控application內(nèi)置對象的創(chuàng)建和銷毀。

當(dāng)web容器開啟時(shí),執(zhí)行contextInitialized方法;當(dāng)容器關(guān)閉或重啟時(shí),執(zhí)行contextDestroyed方法。

實(shí)現(xiàn)方式:直接實(shí)現(xiàn)ServletContextListener接口:

public class MyServletContextListener implements ServletContextListener{
  public void contextDestroyed(ServletContextEvent sce) {

  }
  public void contextInitialized(ServletContextEvent sce) {

  }
}

HttpSession監(jiān)控:對應(yīng)監(jiān)控session內(nèi)置對象的創(chuàng)建和銷毀。

當(dāng)打開一個(gè)新的頁面時(shí),開啟一個(gè)session會(huì)話,執(zhí)行sessionCreated方法;當(dāng)頁面關(guān)閉session過期時(shí),或者容器關(guān)閉銷毀時(shí),執(zhí)行sessionDestroyed方法。

實(shí)現(xiàn)方式:直接實(shí)現(xiàn)HttpSessionListener接口:

public class MyHttpSessionListener implements HttpSessionListener{
  public void sessionCreated(HttpSessionEvent arg0) {

  }
  public void sessionDestroyed(HttpSessionEvent arg0) {

  }
}

ServletRequest監(jiān)控:對應(yīng)監(jiān)控request內(nèi)置對象的創(chuàng)建和銷毀。

當(dāng)訪問某個(gè)頁面時(shí),出發(fā)一個(gè)request請求,執(zhí)行requestInitialized方法;當(dāng)頁面關(guān)閉時(shí),執(zhí)行requestDestroyed方法。

實(shí)現(xiàn)方式,直接實(shí)現(xiàn)ServletRequestListener接口:

public class MyServletRequestListener implements ServletRequestListener{
  public void requestDestroyed(ServletRequestEvent arg0) {

  }
  public void requestInitialized(ServletRequestEvent arg0) {

  }
}

2 按照監(jiān)聽事件劃分:

2.1 監(jiān)聽事件自身的創(chuàng)建和銷毀:同上面的按對象劃分。

2.2 監(jiān)聽屬性的新增、刪除和修改:

監(jiān)聽屬性的新增、刪除和修改也是劃分成三種,分別針對于ServletContext、HttpSession、ServletRequest對象:

ServletContext,實(shí)現(xiàn)ServletContextAttributeListener接口:

通過調(diào)用ServletContextAttribtueEvent的getName方法可以得到屬性的名稱。

public class MyServletContextAttrListener implements ServletContextAttributeListener{

  public void attributeAdded(ServletContextAttributeEvent hsbe) {
    System.out.println("In servletContext added :name = "+hsbe.getName());
  }

  public void attributeRemoved(ServletContextAttributeEvent hsbe) {
    System.out.println("In servletContext removed :name = "+hsbe.getName());
  }

  public void attributeReplaced(ServletContextAttributeEvent hsbe) {
    System.out.println("In servletContext replaced :name = "+hsbe.getName());
  }

}

HttpSession,實(shí)現(xiàn)HttpSessionAttributeListener接口:

public class MyHttpSessionAttrListener implements HttpSessionAttributeListener{

  public void attributeAdded(HttpSessionBindingEvent hsbe) {
    System.out.println("In httpsession added:name = "+hsbe.getName());
  }

  public void attributeRemoved(HttpSessionBindingEvent hsbe) {
    System.out.println("In httpsession removed:name = "+hsbe.getName());
  }

  public void attributeReplaced(HttpSessionBindingEvent hsbe) {
    System.out.println("In httpsession replaced:name = "+hsbe.getName());
  }

}

ServletRequest,實(shí)現(xiàn)ServletRequestAttributeListener接口:

public class MyServletRequestAttrListener implements ServletRequestAttributeListener{

  public void attributeAdded(ServletRequestAttributeEvent hsbe) {
    System.out.println("In servletrequest added :name = "+hsbe.getName());
  }

  public void attributeRemoved(ServletRequestAttributeEvent hsbe) {
    System.out.println("In servletrequest removed :name = "+hsbe.getName());
  }

  public void attributeReplaced(ServletRequestAttributeEvent hsbe) {
    System.out.println("In servletrequest replaced :name = "+hsbe.getName());
  }

}

2.3 監(jiān)聽對象的狀態(tài):

針對某些POJO類,可以通過實(shí)現(xiàn)HttpSessionBindingListener接口,監(jiān)聽POJO類對象的事件。例如:

public class User implements HttpSessionBindingListener,Serializable{

  private String username;
  private String password;
  
  public String getUsername() {
    return username;
  }

  public void setUsername(String username) {
    this.username = username;
  }

  public String getPassword() {
    return password;
  }

  public void setPassword(String password) {
    this.password = password;
  }

  public void valueBound(HttpSessionBindingEvent hsbe) {
    System.out.println("valueBound name: "+hsbe.getName());
  }

  public void valueUnbound(HttpSessionBindingEvent hsbe) {
    System.out.println("valueUnbound name: "+hsbe.getName());
  }
  
}

Session數(shù)據(jù)的鈍化與活化:

由于session中保存大量訪問網(wǎng)站相關(guān)的重要信息,因此過多的session數(shù)據(jù)就會(huì)服務(wù)器性能的下降,占用過多的內(nèi)存。因此類似數(shù)據(jù)庫對象的持久化,web容器也會(huì)把不常使用的session數(shù)據(jù)持久化到本地文件或者數(shù)據(jù)中。這些都是有web容器自己完成,不需要用戶設(shè)定。

不用的session數(shù)據(jù)序列化到本地文件中的過程,就是鈍化;

當(dāng)再次訪問需要到該session的內(nèi)容時(shí),就會(huì)讀取本地文件,再次放入內(nèi)存中,這個(gè)過程就是活化。

類似的,只要實(shí)現(xiàn)HttpSeesionActivationListener接口就是實(shí)現(xiàn)鈍化與活化事件的監(jiān)聽:

public class User implements HttpSessionBindingListener,
HttpSessionActivationListener,Serializable{

  private String username;
  private String password;
  
  public String getUsername() {
    return username;
  }

  public void setUsername(String username) {
    this.username = username;
  }

  public String getPassword() {
    return password;
  }

  public void setPassword(String password) {
    this.password = password;
  }

  public void valueBound(HttpSessionBindingEvent hsbe) {
    System.out.println("valueBound name: "+hsbe.getName());
  }

  public void valueUnbound(HttpSessionBindingEvent hsbe) {
    System.out.println("valueUnbound name: "+hsbe.getName());
  }

  public void sessionDidActivate(HttpSessionEvent hsbe) {
    System.out.println("sessionDidActivate name: "+hsbe.getSource());
  }

  public void sessionWillPassivate(HttpSessionEvent hsbe) {
    System.out.println("sessionWillPassivate name: "+hsbe.getSource());
  }
  
}

Servlet版本與Tomcat版本

首先看一下Tomcat官網(wǎng)給出的匹配:

 

如果版本不匹配,那么tomcat是不能發(fā)布該工程的,首先看一下版本不匹配時(shí),會(huì)發(fā)生什么!

我試圖創(chuàng)建一個(gè)web工程,并且選取了Servlet3.0版本:

 

然后我想要在tomcat6中發(fā)布,可以看到報(bào)錯(cuò)了!

JDK版本不對....這是在平時(shí)開發(fā)如果對Servlet不熟悉的web新手,常犯的錯(cuò)誤。

 

解決方法:

1 在創(chuàng)建時(shí),直接發(fā)布到Tomcat容器中,此時(shí)Servlet僅僅會(huì)列出Tomcat支持的版本:

 

2 修改工程Servlet版本配置信息,文件為:工作目錄\SessionExample\.settings\org.eclipse.wst.common.project.facet.core.xml

<?xml version="1.0" encoding="UTF-8"?>
<faceted-project>
 <runtime name="Apache Tomcat v6.0"/>
 <fixed facet="java"/>
 <fixed facet="wst.jsdt.web"/>
 <fixed facet="jst.web"/>
 <installed facet="java" version="1.7"/>
 <installed facet="jst.web" version="2.5"/>
 <installed facet="wst.jsdt.web" version="1.0"/>
</faceted-project>

getAttribute與getParameter的區(qū)別

這部分是對JSP的擴(kuò)展,經(jīng)常在JSP或者Servlet中獲取數(shù)據(jù),那么getAttribute與getParameter有什么區(qū)別呢?

1 從獲取到數(shù)據(jù)的來源來說:

getAttribtue獲取到的是web容器中的值,比如:

我們在Servlet中通過setAttribute設(shè)定某個(gè)值,這個(gè)值存在于容器中,就可以通過getAttribute方法獲?。?/p>

getParameter獲取到的是通過http傳來的值,比如這樣一個(gè)http請求:

http:localhost:8080/test/test.html?username=xingoo

還有其他的GET和POST方式,都可以通過getParameter來獲取。

2 從獲取到的數(shù)據(jù)類型來說:

getAttribute返回的是一個(gè)對象,Object。

getParameter返回的是,前面頁面中某個(gè)表單或者h(yuǎn)ttp后面參數(shù)傳遞的值,是個(gè)字符串。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • spring boot項(xiàng)目打包成war在tomcat運(yùn)行的全步驟

    spring boot項(xiàng)目打包成war在tomcat運(yùn)行的全步驟

    這篇文章主要給大家介紹了關(guān)于spring boot項(xiàng)目打包成war在tomcat運(yùn)行的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用spring boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • IDEA2023創(chuàng)建MavenWeb項(xiàng)目并搭建Servlet工程的全過程

    IDEA2023創(chuàng)建MavenWeb項(xiàng)目并搭建Servlet工程的全過程

    Maven提供了大量不同類型的Archetype模板,通過它們可以幫助用戶快速的創(chuàng)建Java項(xiàng)目,這篇文章主要給大家介紹了關(guān)于IDEA2023創(chuàng)建MavenWeb項(xiàng)目并搭建Servlet工程的相關(guān)資料,需要的朋友可以參考下
    2023-10-10
  • Java 將PPT幻燈片轉(zhuǎn)為HTML文件的實(shí)現(xiàn)思路

    Java 將PPT幻燈片轉(zhuǎn)為HTML文件的實(shí)現(xiàn)思路

    本文以Java程序代碼為例展示如何通過格式轉(zhuǎn)換的方式將PPT幻燈片文檔轉(zhuǎn)為HTML文件,本文通過實(shí)例代碼圖文相結(jié)合給大家分享實(shí)現(xiàn)思路,需要的朋友參考下吧
    2021-06-06
  • Java實(shí)戰(zhàn)角色權(quán)限后臺腳手架系統(tǒng)的實(shí)現(xiàn)流程

    Java實(shí)戰(zhàn)角色權(quán)限后臺腳手架系統(tǒng)的實(shí)現(xiàn)流程

    只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+Springboot+Maven+myBaits-Plus+Vue+Element-UI+Mysql實(shí)現(xiàn)一個(gè)角色權(quán)限后臺腳手架系統(tǒng),大家可以在過程中查缺補(bǔ)漏,提升水平
    2022-01-01
  • 解決mybatis用Map返回的字段全變大寫的問題

    解決mybatis用Map返回的字段全變大寫的問題

    這篇文章主要介紹了解決mybatis用Map返回的字段全變大寫的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • linux系統(tǒng)下java項(xiàng)目在后臺啟動(dòng)的4種方式總結(jié)

    linux系統(tǒng)下java項(xiàng)目在后臺啟動(dòng)的4種方式總結(jié)

    Linux是集多種功能于一身的操作系統(tǒng),它可以讓用戶查看和管理當(dāng)下正在運(yùn)行的進(jìn)程,包括Java程序,這篇文章主要給大家總結(jié)介紹了關(guān)于linux系統(tǒng)下java項(xiàng)目在后臺啟動(dòng)的4種方式,需要的朋友可以參考下
    2023-10-10
  • Java實(shí)現(xiàn)簡單的聊天室功能

    Java實(shí)現(xiàn)簡單的聊天室功能

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)簡單的聊天室功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • 使用spring的restTemplate注意點(diǎn)

    使用spring的restTemplate注意點(diǎn)

    這篇文章主要介紹了使用spring的restTemplate注意點(diǎn),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • Java設(shè)計(jì)模式之責(zé)任鏈模式詳解

    Java設(shè)計(jì)模式之責(zé)任鏈模式詳解

    這篇文章主要介紹了Java設(shè)計(jì)模式之責(zé)任鏈模式詳解,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)java的小伙伴們有非常好的幫助,需要的朋友可以參考下
    2021-04-04
  • MyBatis查詢結(jié)果resultType返回值類型的說明

    MyBatis查詢結(jié)果resultType返回值類型的說明

    這篇文章主要介紹了MyBatis查詢結(jié)果resultType返回值類型的說明,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11

最新評論