淺談java監(jiān)聽器的作用
監(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)建、銷毀和修改等??梢栽谀承﹦幼髑昂笤黾犹幚?,實現(xiàn)監(jiān)控。
2 監(jiān)聽器常用的用途
- 通常使用Web監(jiān)聽器做以下的內(nèi)容:
- 統(tǒng)計在線人數(shù),利用HttpSessionLisener
- 加載初始化信息:利用ServletContextListener
- 統(tǒng)計網(wǎng)站訪問量
- 實現(xiàn)訪問監(jiān)控
3 接下里看看一個監(jiān)聽器的創(chuàng)建以及執(zhí)行過程
首先需要創(chuàng)建一個監(jiān)聽器,實現(xiàn)某種接口,例如我想實現(xiàn)一個對在線人數(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)我使用我的瀏覽器訪問時,執(zhí)行結(jié)果如下:
當(dāng)打開另一個瀏覽器訪問時:
由于打開另一個瀏覽器訪問,相當(dāng)于另一個會話,因此在線人數(shù)會增加。
對于3.0版本的Servlet來說,還支持使用注解的方式進行配置。
那么接下來看看都有哪些監(jiān)聽器以及方法吧!
監(jiān)聽器的分類
1 按照監(jiān)聽的對象劃分:
按照監(jiān)聽對象的不同可以劃分為三種:
ServletContext監(jiān)控:對應(yīng)監(jiān)控application內(nèi)置對象的創(chuàng)建和銷毀。
當(dāng)web容器開啟時,執(zhí)行contextInitialized方法;當(dāng)容器關(guān)閉或重啟時,執(zhí)行contextDestroyed方法。
實現(xiàn)方式:直接實現(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)打開一個新的頁面時,開啟一個session會話,執(zhí)行sessionCreated方法;當(dāng)頁面關(guān)閉session過期時,或者容器關(guān)閉銷毀時,執(zhí)行sessionDestroyed方法。
實現(xiàn)方式:直接實現(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)訪問某個頁面時,出發(fā)一個request請求,執(zhí)行requestInitialized方法;當(dāng)頁面關(guān)閉時,執(zhí)行requestDestroyed方法。
實現(xiàn)方式,直接實現(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,實現(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,實現(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,實現(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類,可以通過實現(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ù)就會服務(wù)器性能的下降,占用過多的內(nèi)存。因此類似數(shù)據(jù)庫對象的持久化,web容器也會把不常使用的session數(shù)據(jù)持久化到本地文件或者數(shù)據(jù)中。這些都是有web容器自己完成,不需要用戶設(shè)定。
不用的session數(shù)據(jù)序列化到本地文件中的過程,就是鈍化;
當(dāng)再次訪問需要到該session的內(nèi)容時,就會讀取本地文件,再次放入內(nèi)存中,這個過程就是活化。
類似的,只要實現(xiàn)HttpSeesionActivationListener接口就是實現(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ā)布該工程的,首先看一下版本不匹配時,會發(fā)生什么!
我試圖創(chuàng)建一個web工程,并且選取了Servlet3.0版本:
然后我想要在tomcat6中發(fā)布,可以看到報錯了!
JDK版本不對....這是在平時開發(fā)如果對Servlet不熟悉的web新手,常犯的錯誤。
解決方法:
1 在創(chuàng)建時,直接發(fā)布到Tomcat容器中,此時Servlet僅僅會列出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的擴展,經(jīng)常在JSP或者Servlet中獲取數(shù)據(jù),那么getAttribute與getParameter有什么區(qū)別呢?
1 從獲取到數(shù)據(jù)的來源來說:
getAttribtue獲取到的是web容器中的值,比如:
我們在Servlet中通過setAttribute設(shè)定某個值,這個值存在于容器中,就可以通過getAttribute方法獲??;
getParameter獲取到的是通過http傳來的值,比如這樣一個http請求:
http:localhost:8080/test/test.html?username=xingoo
還有其他的GET和POST方式,都可以通過getParameter來獲取。
2 從獲取到的數(shù)據(jù)類型來說:
getAttribute返回的是一個對象,Object。
getParameter返回的是,前面頁面中某個表單或者http后面參數(shù)傳遞的值,是個字符串。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Javaweb監(jiān)聽器實例之統(tǒng)計在線人數(shù)
- java監(jiān)聽器實現(xiàn)在線人數(shù)統(tǒng)計
- java-RGB調(diào)色面板的實現(xiàn)(事件監(jiān)聽器之匿名內(nèi)部類)
- Java NIO.2 使用Path接口來監(jiān)聽文件、文件夾變化
- Java設(shè)計模式之監(jiān)聽器模式實例詳解
- Java可以如何實現(xiàn)文件變動的監(jiān)聽的示例
- Java Swing中JList選擇事件監(jiān)聽器ListSelectionListener用法示例
- Java監(jiān)聽器的作用及用法代碼示例
- Java基于ServletContextListener實現(xiàn)UDP監(jiān)聽
相關(guān)文章
spring boot項目打包成war在tomcat運行的全步驟
這篇文章主要給大家介紹了關(guān)于spring boot項目打包成war在tomcat運行的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用spring boot具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
IDEA2023創(chuàng)建MavenWeb項目并搭建Servlet工程的全過程
Maven提供了大量不同類型的Archetype模板,通過它們可以幫助用戶快速的創(chuàng)建Java項目,這篇文章主要給大家介紹了關(guān)于IDEA2023創(chuàng)建MavenWeb項目并搭建Servlet工程的相關(guān)資料,需要的朋友可以參考下2023-10-10
Java 將PPT幻燈片轉(zhuǎn)為HTML文件的實現(xiàn)思路
本文以Java程序代碼為例展示如何通過格式轉(zhuǎn)換的方式將PPT幻燈片文檔轉(zhuǎn)為HTML文件,本文通過實例代碼圖文相結(jié)合給大家分享實現(xiàn)思路,需要的朋友參考下吧2021-06-06
Java實戰(zhàn)角色權(quán)限后臺腳手架系統(tǒng)的實現(xiàn)流程
只學(xué)書上的理論是遠遠不夠的,只有在實戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+Springboot+Maven+myBaits-Plus+Vue+Element-UI+Mysql實現(xiàn)一個角色權(quán)限后臺腳手架系統(tǒng),大家可以在過程中查缺補漏,提升水平2022-01-01
linux系統(tǒng)下java項目在后臺啟動的4種方式總結(jié)
Linux是集多種功能于一身的操作系統(tǒng),它可以讓用戶查看和管理當(dāng)下正在運行的進程,包括Java程序,這篇文章主要給大家總結(jié)介紹了關(guān)于linux系統(tǒng)下java項目在后臺啟動的4種方式,需要的朋友可以參考下2023-10-10
MyBatis查詢結(jié)果resultType返回值類型的說明
這篇文章主要介紹了MyBatis查詢結(jié)果resultType返回值類型的說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11

