Listener是Servlet的监听器,它可以监听客户端的请求、服务端的操作等。通过监听器,可以自动激发一些操作,比如监听在线的用户的数量。当增加一个HttpSession时,就激发sessionCreated(HttpSessionEvent se)方法,这样就可以给在线人数加1。常用的监听接口有以下几个:
ServletContextAttributeListener监听对ServletContext属性的操作,比如增加、删除、修改属性。
ServletContextListener监听ServletContext。当创建ServletContext时,激发contextInitialized(ServletContextEvent sce)方法;当销毁ServletContext时,激发contextDestroyed(ServletContextEvent sce)方法。
HttpSessionListener监听HttpSession的操作。当创建一个Session时,激发session Created(HttpSessionEvent se)方法;当销毁一个Session时,激发sessionDestroyed (HttpSessionEvent se)方法。
HttpSessionAttributeListener监听HttpSession中的属性的操作。当在Session增加一个属性时,激发attributeAdded(HttpSessionBindingEvent se) 方法;当在Session删除一个属性时,激发attributeRemoved(HttpSessionBindingEvent se)方法;当在Session属性被重新设置时,激发attributeReplaced(HttpSessionBindingEvent se) 方法。
package com.xling.tools.application;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class AppListener
extends HttpServlet implements ServletContextListener,
ServletContextAttributeListener, HttpSessionListener,
HttpSessionAttributeListener, ServletRequestListener,
ServletRequestAttributeListener {
//Notification that the web module is ready to process requests
int userCount = 0;
public void contextInitialized(ServletContextEvent sce) {
System.out.println("Application Run!");
}
//Notification that the servlet context is about to be shut down
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("Application ended!");
}
//Notification that a new attribute has been added to the servlet context
public void attributeAdded(ServletContextAttributeEvent scab) {
}
//Notification that an attribute has been removed from the servlet context
public void attributeRemoved(ServletContextAttributeEvent scab) {
}
//Notification that an attribute of the servlet context has been replaced
public void attributeReplaced(ServletContextAttributeEvent scab) {
}
//Notification that a session was created
public void sessionCreated(HttpSessionEvent se) {
userCount ++;
System.out.println(userCount + " users on line!");
}
//Notification that a session was invalidated
public void sessionDestroyed(HttpSessionEvent se) {
userCount --;
System.out.println(userCount + " users on line");
}
//Notification that a new attribute has been added to a session
public void attributeAdded(HttpSessionBindingEvent se) {
}
//Notification that an attribute has been removed from a session
public void attributeRemoved(HttpSessionBindingEvent se) {
}
//Notification that an attribute of a session has been replaced
public void attributeReplaced(HttpSessionBindingEvent se) {
System.out.println(se.getName() + " : " + se.getValue() + " " + se.getSession().getAttribute(se.getName()));
}
//Notification that a request was created
public void requestInitialized(ServletRequestEvent sre) {
}
//Notification that a request was invalidated
public void requestDestroyed(ServletRequestEvent sre) {
}
//Notification that a new attribute has been added to a request
public void attributeAdded(ServletRequestAttributeEvent srae) {
}
//Notification that an attribute has been removed from a request
public void attributeRemoved(ServletRequestAttributeEvent srae) {
}
//Notification that an attribute of a request has been replaced
public void attributeReplaced(ServletRequestAttributeEvent srae) {
}
}
分享到:
相关推荐
实验1:Java Web开发环境及Web编程基础;实验2 JSP、Servlet及JDBC应用开发;实验3 扩展JavaScript框架应用;实验4 Web应用综合设计; 天津理工大学
15.2.3 [listener]元素 457 15.2.4 [tag]元素 458 15.2.5 [tag-file]元素 460 15.2.6 [function]元素 461 15.3 传统标签的开发 462 15.3.1 实例一:[hello]标签 462 15.3.2 实例二:[max]标签 465 15.3.3 ...
15.2.3 [listener]元素 457 15.2.4 [tag]元素 458 15.2.5 [tag-file]元素 460 15.2.6 [function]元素 461 15.3 传统标签的开发 462 15.3.1 实例一:[hello]标签 462 15.3.2 实例二:[max]标签 465 15.3.3 ...
15.2.3 [listener]元素 457 15.2.4 [tag]元素 458 15.2.5 [tag-file]元素 460 15.2.6 [function]元素 461 15.3 传统标签的开发 462 15.3.1 实例一:[hello]标签 462 15.3.2 实例二:[max]标签 465 15.3.3 ...
15.2.3 [listener]元素 457 15.2.4 [tag]元素 458 15.2.5 [tag-file]元素 460 15.2.6 [function]元素 461 15.3 传统标签的开发 462 15.3.1 实例一:[hello]标签 462 15.3.2 实例二:[max]标签 465 15.3.3 ...
<servlet-name>ideawu</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>1 </servlet> <servlet-mapping> <servlet-name>...
第14章 讲解Servlet中的监听器(Listener)技术。 第15章 讲解Servlet过滤器(Filter)技术。 第16章 讲解使用Apache Commons FileUpload实现文件上传,同时讲解了JavaMail和如何运用Apache Commons Email组件发送...
//给客户发一个感谢消息,消息驱动Bean必须实现两个接口MessageDrivenBean和MessageListener 在对象创建的过程中将被容器调用,onMessage函数方法接收消息参数,将其强制转型为合适的消息类型,同时打印出消息...
//给客户发一个感谢消息,消息驱动Bean必须实现两个接口MessageDrivenBean和MessageListener 在对象创建的过程中将被容器调用,onMessage函数方法接收消息参数,将其强制转型为合适的消息类型,同时打印出消息...
//给客户发一个感谢消息,消息驱动Bean必须实现两个接口MessageDrivenBean和MessageListener 在对象创建的过程中将被容器调用,onMessage函数方法接收消息参数,将其强制转型为合适的消息类型,同时打印出消息...
//给客户发一个感谢消息,消息驱动Bean必须实现两个接口MessageDrivenBean和MessageListener 在对象创建的过程中将被容器调用,onMessage函数方法接收消息参数,将其强制转型为合适的消息类型,同时打印出消息...
//给客户发一个感谢消息,消息驱动Bean必须实现两个接口MessageDrivenBean和MessageListener 在对象创建的过程中将被容器调用,onMessage函数方法接收消息参数,将其强制转型为合适的消息类型,同时打印出消息...
//给客户发一个感谢消息,消息驱动Bean必须实现两个接口MessageDrivenBean和MessageListener 在对象创建的过程中将被容器调用,onMessage函数方法接收消息参数,将其强制转型为合适的消息类型,同时打印出消息...
消息驱动Bean必须实现两个接口MessageDrivenBean和MessageListener 在对象创建的过程中将被容器调用,onMessage函数方法接收消息参数,将其强制转型为合适的消息类型,同时打印出消息的内容。同时一个mail note将被...
消息驱动Bean必须实现两个接口MessageDrivenBean和MessageListener 在对象创建的过程中将被容器调用,onMessage函数方法接收消息参数,将其强制转型为合适的消息类型,同时打印出消息的内容。同时一个mail note将被...
这个项目包括5个实体,这5个实体之间具有复杂的关联关系,而且业务逻辑也相对复杂,希望让读者理论联系实际,真正将jsf+ejb 3+jpa整合真正运用到实际开发中。该案例采用目前最流行、最规范的java ee架构,整个应用...