servlet监听器Listener(理论+例子)
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在实际项目开发中,用到的不多。这里最好知道有这么回事就成。
servlet监听器Listener(理论+例子)的更多相关文章
- java之Servlet监听器Listener
常用应用场景:单点登录.统计在线人数 一.简介 (一)概述 1.Listener 用于监听 java web程序中的事件,例如创建.修改.删除Session.request.context等,并触发响 ...
- Servlet 监听器Listener详解
转自:http://blog.csdn.net/u012228718/article/details/41730799 一.简介 (一)概述 1.Listener 用于监听 Javaweb程序中的事件 ...
- Java进阶(十三)servlet监听器
servlet监听器 Listener是Servlet的监听器,它可以监听客户端的请求.服务端的操作等.通过监听器,可以自动激发一些操作,比如监听在线的用户的数量.当 增加一个HttpSession时 ...
- Servlet监听器——实现在线登录人数统计小例子
一.概念 servlet监听器的主要目的是给web应用增加事件处理机制,以便更好的监视和控制web应用的状态变化,从而在后台调用相应处理程序. 二.监听器的类型 1.根据监听对象的类型和范围,分为3类 ...
- [原创]java WEB学习笔记47:Servlet 监听器简介, ServletContext(Application 对象), HttpSession (Session 对象), HttpServletRequest (request 对象) 监听器,利用listener理解 三个对象的生命周期
本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...
- Servlet的监听器Listener
Servlet的监听器Listener,它是实现了javax.servlet.ServletContextListener 接口的服务器端程序,它也是 随web应用的启动而启动,只初始化一次,随web ...
- 监听器Listener
监听器 6个事件类,均以event结尾 *某些操作,如启动/关闭容器,创建/销毁会话,都将触发一种事件发生,当发生了某种事件,容器将创建对应的事件类对象 8个监听接口,均以Listener结尾 监听器 ...
- JavaWeb—监听器Listener
1.简介 Listener是Servlet的监听器,Servlet 监听器用于监听一些重要事件的发生,监听器对象在事情发生前.发生后可以做一些必要的处理. JavaWeb里面的listener是通过观 ...
- Spring Boot使用监听器Listener
之前介绍了在Spring Boot中使用过滤器:https://www.cnblogs.com/zifeiy/p/9911056.html 接下来介绍使用监听器Listener. 下面是一个例子: p ...
随机推荐
- J2EE中关于tomcat的maxIdle、maxActive、maxActive相关配置
一.基本概念 1 maxActive 连接池的最大数据库连接数.设为0表示无限制,一般把maxActive设置成可能的并发量就行了 2 maxIdle 最大的空闲连接数 3 maxWait 最大建立连 ...
- SOAP-XML请求(iOS应用下集成携程api)
用携程机票为例: 携程联盟 飞机票.门票 联盟ID:278639 站点ID:739462 密钥KEY:BE57B925-E8CE-4AA2-AC8E-3EE4BBBB686F API_URL:open ...
- angular留言板
今天使用angularJs写了一个留言板,简单的享受了下angular处理数据的双向绑定的方便:注释已经都写到行间了 <!DOCTYPE html> <html lang=" ...
- 在表单中元素的onchange事件的兼容性问题
onchange:在值发生改变的时候触发 text:当光标离开的时候如果内容有变化就触发 radio/check:标准浏览器下点击的时候只要值变了就触发 非标准浏览器下焦点离开的时候如果值变了就触发
- SQL SERVER的连接方式
最近在做项目的时候,遇到了SQLSERVER的连接,以前是很模糊的,现在做一个简单的总结. 针对SQL_SERVER,连接指定的方式有两种,一种是Where条件指定方式,另外一种是采用On连指定连接条 ...
- web app iphone4 iphone5 iphone6 iphone6 Plus响应式布局 适配代码
来源:http://www.phptext.net/article_view.php?id=387 -------------------------------------------------- ...
- ASP.NET中获取当日,当周,当月,当年的日期
ASP.NET中获取当日,当周,当月,当年的日期 在ASP.NET开发中,经常会碰到要获取当日,当周,当月,当年的日期. 以下将源码贴出来和大家分享. aspx中代码如下: <table ce ...
- android studio 使用问题 解决方法
1. Error:Execution failed for task ':app:transformClassesWithDexForDebug'. > com.android.build.ap ...
- codevs http://www.codevs.cn/problem/?problemset_id=1 循环、递归、stl复习题
12.10高一练习题 1.要求: 这周回顾复习的内容是循环.递归.stl. 不要因为题目简单就放弃不做,现在就是练习基础. 2.练习题: (1)循环 题目解析与代码见随笔分类 NOI题库 htt ...
- SQL Server对Xml字段的操作
T-Sql操作Xml数据 一.前言 SQL Server 2005 引入了一种称为 XML 的本机数据类型.用户可以创建这样的表,它在关系列之外还有一个或多个 XML 类型的列:此外,还允许带有变量和 ...