Listener采用了观察者模式(24种模式之一),Listener是servlet的监听器,他可以监听客户端的请求、服务器端的操作等, 通过监听器,可以自动激发一些操作。比如:监听在线用户数量

当增加一个HttpSession时,就会激发sessinCreated(HttpSessionEvent sce)方法,这样就可以给在线人数+1了。

常见的监听器接口:

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

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

实例:

首先配置web.xml

<!--servlet 监听器 start-->
<listener>
<listener-class>com.listener.MyServletContextListener</listener-class>
</listener>

<listener>
<listener-class>com.listener.MyServletContextAttributeListener</listener-class>
</listener>
<!-- servlet 监听器 end -->

web.xml

  <!--servlet 监听器  start-->
  <listener>
      <listener-class>com.listener.MyServletContextListener</listener-class>
  </listener>

  <listener>
      <listener-class>com.listener.MyServletContextAttributeListener</listener-class>
  </listener>
  <!-- servlet 监听器 end -->

2.ServletContextListener接口的调用

/**
* 在xml中配置监听器
* ServletContextListener 坚挺servletContext,当创建servletContext时,激发ContextInitialized(ServletContextEvent sce)方法
* 当销毁servletContext时,激发contextDestoryed(ServletContextEvent sce)方法
*/
package com.listener;

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

public class MyServletContextListener implements ServletContextListener
{

@Override
public void contextDestroyed(ServletContextEvent arg0)
{
System.out.println(arg0.toString());

}

@Override
public void contextInitialized(ServletContextEvent arg0)
{
System.out.println(arg0.toString());
}

}

MyServletContextListener

/**
 * 在xml中配置监听器
 * ServletContextListener 坚挺servletContext,当创建servletContext时,激发ContextInitialized(ServletContextEvent sce)方法
 * 当销毁servletContext时,激发contextDestoryed(ServletContextEvent sce)方法
 */
package com.listener;

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

public class MyServletContextListener implements ServletContextListener
{

    @Override
    public void contextDestroyed(ServletContextEvent arg0)
    {
        System.out.println(arg0.toString());

    }

    @Override
    public void contextInitialized(ServletContextEvent arg0)
    {
        System.out.println(arg0.toString());
    }

}

3.ServletContextAttributeListener接口的调用

package com.listener;

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

public class MyServletContextAttributeListener implements ServletContextAttributeListener
{

@Override
public void attributeAdded(ServletContextAttributeEvent arg0)
{
System.out.println("attributeAdd ");
System.out.println(arg0.getName() + ":" + arg0.getValue());

}

@Override
public void attributeRemoved(ServletContextAttributeEvent arg0)
{
System.out.println("attributeRemoved");
System.out.println(arg0.getName() + ":" + arg0.getValue());//value值将显示之前的 值
}

@Override
public void attributeReplaced(ServletContextAttributeEvent arg0)
{
System.out.println("attributeRepaced");
System.out.println(arg0.getName() + ":" + arg0.getValue());//value值将显示之前的 值

}

}

MyServletContextAttributeListener

package com.listener;

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

public class MyServletContextAttributeListener implements ServletContextAttributeListener
{

    @Override
    public void attributeAdded(ServletContextAttributeEvent arg0)
    {
        System.out.println("attributeAdd ");
        System.out.println(arg0.getName() + ":" + arg0.getValue());

    }

    @Override
    public void attributeRemoved(ServletContextAttributeEvent arg0)
    {
        System.out.println("attributeRemoved");
        System.out.println(arg0.getName() + ":" + arg0.getValue());//value值将显示之前的 值
    }

    @Override
    public void attributeReplaced(ServletContextAttributeEvent arg0)
    {
        System.out.println("attributeRepaced");
        System.out.println(arg0.getName() + ":" + arg0.getValue());//value值将显示之前的 值

    }

}

为了方便,没有用servlet举例,直接写的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("name1","value1");
application.setAttribute("name2","value2");
application.setAttribute("name1","value3");
application.setAttribute("name2","value4");
%>
<a href="listener2.jsp">next</a>
</body>
</html>

listener1.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("name1","value1");
    application.setAttribute("name2","value2");
    application.setAttribute("name1","value3");
    application.setAttribute("name2","value4");
%>
<a href="listener2.jsp">next</a>
</body>
</html>

<%@ 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.removeAttribute("name1");
%>
</body>
</html>

listener2.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.removeAttribute("name1");
%>
</body>
</html>

结束。listener在实际项目开发中,用到的不多。这里最好知道有这么回事就成。

珍惜现在,珍惜每一分,每一秒。 从不放弃,坚持。。。。。

最新文章

  1. tail命令详解
  2. IIS部署Nodejs步骤
  3. Android Adapter的几个方法
  4. php 实现接收客户端上传的图片
  5. 【matlab】输出固定位数的数字
  6. 将centos系统中的网卡em1还原为eth0
  7. PHP版3DES加解密类
  8. codeforces 616E. Sum of Remainders 数学
  9. AJAX程序实验
  10. wpf XAML xaml 进行 数据绑定,Resource DataContext ElementName
  11. 安卓TV开发(十) 智能电视开发之在线视频直播
  12. python实现将字符串中以大写字母开头的单词前面添加“_”下划线
  13. python day11 函数(第三篇)
  14. 一道简单的HashMap面试题所想到的...
  15. LeetCode - 872. Leaf-Similar Trees
  16. OnSen UI结合AngularJs打造”美团&quot;APP首页 --Hybrid Ap
  17. JS--label语句的使用
  18. 关于AJAX与form表单提交数据的格式
  19. SpringMVC学习笔记:数据的接收与返回
  20. ACM1004 Let the balloons fly

热门文章

  1. C# String.split()用法小结。String.Split 方法 (String[], StringSplitOptions)
  2. Oracle死锁查询及处理
  3. apt-get update更新源时,出现“Hash Sum mismatch”问题
  4. 漂浮QQ
  5. Solving GitHub FetchHead (MergeConflict) in Visual Studio 2013
  6. Linux时间函数之gettimeofday()函数之使用方法
  7. codevs3143 二叉树的序遍历
  8. jquery常用代码
  9. struts2使用Convention Plugin在weblogic上以war包部署时,找不到Action的解决办法
  10. 文本比较算法Ⅱ——Needleman/Wunsch算法