天天看點

JavaWeb——監聽器

一、監聽器概述

1、它是一個接口,内容由我們來實作

2、它需要注冊,例如注冊在按鈕上

3、監聽器中的方法會在特殊事件發生時被調用

我們用一個簡單的例子來說明監聽器的工作原理:

在監聽器中有三個名詞:事件源,事件,監聽器

比如在一個酒店裡發生了打架事件,警察來捉人。

事件源:打架的人

事件:打架

監聽器:警察

監聽器中的方法:捉人(一個監聽器可以有多個方法,對不同的事件調用不同的方法)

二、JavaWeb中的監聽器

事件源及其對應的監聽:

  • ServletContext

    (1)生命周期監聽:

    ServletContextListener

    ,它有兩個方法,一個是在出生時調用,一個在死亡是調用

    (2)屬性監聽:

    ServletContextAttributeListener

    ,它有三個方法,一個在添加屬性時調用,一個在替換屬性時調用,一個在移除屬性時調用
  • HttpSession

    (1)生命周期監聽:

    HttpSessionListener

    ,它有兩個方法,一個是在出生時調用,一個在死亡是調用

    (2)屬性監聽:

    HttpSessionAttributeListener

    ,它有三個方法,一個在添加屬性時調用,一個在替換屬性時調用,一個在移除屬性時調用
  • ServletRequest

    (1)生命周期監聽:

    ServletRequestListener

    ,它有兩個方法,一個是在出生時調用,一個在死亡是調用

    (2)屬性監聽:

    ServletRequestAttributeListener

    ,它有三個方法,一個在添加屬性時調用,一個在替換屬性時調用,一個在移除屬性時調用

生命周期監聽器舉例:這裡的例子是

ServletContextListener

,因為

ServleContext

在伺服器開啟時建立,在伺服器關閉時銷毀

package com.jiayifan;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

public class AListener implements ServletContextListener {

    public void contextDestroyed(ServletContextEvent sce)  { 
        System.out.println("哈哈,我離開了!!!!");
    }

    public void contextInitialized(ServletContextEvent sce)  { 
         System.out.println("哈哈,我來了!!!!!");
    }
}
           

在web.xml中配置監聽器:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
  <display-name>Dome1</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <listener>
    <listener-class>com.jiayifan.AListener</listener-class>
  </listener>
</web-app>
           

web.xml的配置就省略了,jsp頁面的代碼

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
application.setAttribute("lalala", "hahaha");
%>
</body>
</
           

程式運作結果:

JavaWeb——監聽器
JavaWeb——監聽器

事件對象:

  • ServletContextEvent

  • HttpSeesion

  • ServletRequestEvent

我們可以通過事件對象擷取事件源,比如我們可以通過

ServletContextEvent

來擷取

ServletContext

對象。

屬性監聽器示例:

package com.jiayifan;

import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.annotation.WebListener;

public class BListener implements ServletContextAttributeListener {
    //添加屬性時調用
    public void attributeAdded(ServletContextAttributeEvent scae)  { 
        System.out.println("你添加了一個名字為" + scae.getName() + "值為" + scae.getValue() + "的屬性");
    }
    //移除屬性時調用
    public void attributeRemoved(ServletContextAttributeEvent scae)  { 

    }
    //修改屬性時調用
    public void attributeReplaced(ServletContextAttributeEvent scae)  { 

    }

}
           

程式運作截圖:

JavaWeb——監聽器

上面列舉的是三大域的監聽器,而JavaWeb中有8個監聽器,上面已經列舉了其中的6個,因為上面的6個比較簡單,而且基本的用法也極其類似,是以放在一塊講解,下面再來介紹一下其餘的兩個。

三、感覺監聽(與HttpSession相關)

1、這個監聽器添加到JavaBean上

2、這個監聽器不需要在web.xml中配置

HttpSessionBindingListener

這個監聽器是放在JavaBean上的,它将在該JavaBean放入HttpSeeion中時調用方法,或者在被移除HttpSession時調用方法

程式示例:

package com.jiayifan;

import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;

public class User implements HttpSessionBindingListener{
    private String name;
    private String password;
    public User(String name, String password) {
        super();
        this.name = name;
        this.password = password;
    }
    public User() {
        super();
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    @Override
    public void valueBound(HttpSessionBindingEvent event) {
        System.out.println("啊,session添加了我");
    }
    @Override
    public void valueUnbound(HttpSessionBindingEvent event) {
        System.out.println("哇,session放棄了我");
    }
}
           

jsp頁面代碼

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="com.jiayifan.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
session.setAttribute("user", new User());
session.removeAttribute("user");
%>
</body>
</html>
           

程式運作截圖:

JavaWeb——監聽器

四、鈍化活化監聽器(HttpSessionActivationListener)

1、這個監聽器添加到JavaBean上

2、這個監聽器不需要在web.xml中配置

因為這個監聽器并不常用,并且不好示範,是以這裡隻講一下概念。

session的序列化和反序列化:

當我們關閉伺服器時,伺服器會将session儲存到本地,這種操作就是session的序列化,在伺服器又開啟時,伺服器又将session從本地讀取出來,這樣的操作就是session的反序列化。

session的鈍化和活化:

當伺服器的通路量很高時,如果有session很長時間都沒有被通路,伺服器就會把session儲存到本地,然後把記憶體中的session移除來擴大記憶體,這就是session的鈍化,當使用者又通路該session時,伺服器又将session從本地讀取到記憶體中,這是session的活化。