Listener(监听器)

18 篇文章 0 订阅

Listener

  1. Listener是Servlet的监听器,它可以监听客户端的请求、服务端的操作等。通过监听器,可以自动激发一些操作,比如监听在线的用户的数量。
  2. 当增加一个HttpSession时,就激发sessionCreated(HttpSessionEvent se)方法,这样就可以给在线人数加1

常用的监听接口

ServletContextAttributeListener

ServletContextAttributeListener监听对ServletContext属性的操作,比如增加、删除、修改属性

package learn.listener;

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


public class MyServletContextAttributeListener implements ServletContextAttributeListener {


    public void attributeAdded(ServletContextAttributeEvent scae)  { 
    	System.out.println("attribute added");
        System.out.println(scae.getName() + ":" + scae.getValue());
         
    }


    public void attributeRemoved(ServletContextAttributeEvent scae)  { 
    	System.out.println("attribute removed");
        System.out.println(scae.getName() + ":" + scae.getValue());
    }


    public void attributeReplaced(ServletContextAttributeEvent scae)  { 
    	System.out.println("attribute replaced");
        System.out.println(scae.getName() + ":" + scae.getValue());
    }
	
}

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

<% application.setAttribute("aa", "bb"); %>
<% application.setAttribute("aa", "CC"); %>
<% application.setAttribute("aa", "DD"); %>
<% application.setAttribute("aa", "EE"); %>
<% application.removeAttribute("aa"); %>
</body>
</html>
	<listener>
		<listener-class>learn.listener.MyServletContextListener</listener-class>
	</listener>

结果是:

initializedorg.apache.catalina.core.ApplicationContextFacade@1a0dcaa

attribute added
org.apache.jasper.runtime.JspApplicationContextImpl:org.apache.jasper.runtime.JspApplicationContextImpl@2705ba70
attribute added
aa:bb
attribute replaced
aa:bb
attribute replaced
aa:CC
attribute replaced
aa:DD
attribute removed
aa:EE

ServletContextListener

ServletContextListener监听ServletContext。当创建ServletContext时,激发contextInitialized(ServletContextEvent sce)方法;当销毁ServletContext时,激发contextDestroyed(ServletContextEvent sce)方法

package learn.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.sound.midi.Soundbank;

/**
 * Application Lifecycle Listener implementation class MyServletContextListener
 *
 */
public class MyServletContextListener implements ServletContextListener {

    
    public void contextDestroyed(ServletContextEvent sce)  { 
         System.out.println("destroyed" + sce.getServletContext());
    } 

	
    public void contextInitialized(ServletContextEvent sce)  { 
         System.out.println("initialized" + sce.getServletContext());
    }
	
}

	<listener>
		<listener-class>learn.listener.MyServletContextAttributeListener</listener-class>
	</listener>

结果是:
initializedorg.apache.catalina.core.ApplicationContextFacade@45dd4eda
当修改后保存结果是:
destroyedorg.apache.catalina.core.ApplicationContextFacade@45dd4eda
initializedorg.apache.catalina.core.ApplicationContextFacade@687f4396
在服务器启动时先初始化Listener然后再初始化Filter然后再启动Servelt的,在关闭时是先关闭Servlet然后关闭Filter最后销毁Listener,我们可以类比链式Filter。

HttpSessionListener

HttpSessionListener监听HttpSession的操作。当创建一个Session时,激发session Created(HttpSessionEvent se)方法;当销毁一个Session时,激发sessionDestroyed (HttpSessionEvent se)方法。

package learn.listener;

import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class MySessionListener implements HttpSessionListener {


    public void sessionCreated(HttpSessionEvent se)  { 
         System.out.println("session created");
         
         System.out.println(se.getSession());
    }

    public void sessionDestroyed(HttpSessionEvent se)  { 
    	System.out.println("session destroyed");
        
        System.out.println(se.getSession());
    }
}

	<listener>
		<listener-class>learn.listener.MySessionListener</listener-class>
	</listener>

当你随便打开一个支持session的网页时,结果输出:
attribute added
org.apache.jasper.runtime.JspApplicationContextImpl:org.apache.jasper.runtime.JspApplicationContextImpl@6c7eea1
session created
org.apache.catalina.session.StandardSessionFacade@519bc193
当你访问个新的页面:
session created
org.apache.catalina.session.StandardSessionFacade@275d7b9e
当你改动该listener:
destroyedorg.apache.catalina.core.ApplicationContextFacade@69930714
initializedorg.apache.catalina.core.ApplicationContextFacade@6653395f

HttpSessionAttributeListener

HttpSessionAttributeListener监听HttpSession中的属性的操作。当在Session增加一个属性时,激发attributeAdded(HttpSessionBindingEvent se) 方法;当在Session删除一个属性时,激发attributeRemoved(HttpSessionBindingEvent se)方法;当在Session属性被重新设置时,激发attributeReplaced(HttpSessionBindingEvent se) 方法

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

<% session.setAttribute("aa", "bb");

   session.setAttribute("aa", "cc");
   
   session.setAttribute("aa", "dd");
   
   session.removeAttribute("aa");
%>

</body>
</html>
package learn.listener;

import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class MySessionListener implements HttpSessionListener {


    public void sessionCreated(HttpSessionEvent se)  { 
         System.out.println("session created");
         
         System.out.println(se.getSession());
    }

    public void sessionDestroyed(HttpSessionEvent se)  { 
    	System.out.println("session destroyed");
        
        System.out.println(se.getSession());
    }
    
}

	<listener>
		<listener-class>learn.listener.MySessionAttributeListener</listener-class>
	</listener>

结果是:
attribute added
aa:bb
attribute replaced
aa:bb
attribute replaced
aa:cc
attribute removed
aa:dd

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值