简介:
监听器(Listener):Servlet的监听器,Servlet中一种特殊的类。它们能帮助开发者监听web中的特定事件,比如ServletContext,HttpSession,ServletRequest的创建和销毁;变量的创建、销毁和修改等。可以在某些动作前后增加处理,实现监控。其主要目的就是为了给Web应用程序添加事件处理机制,以便更好的监视和控制Web应用的状态变化。
原理:当范围对象的状态发生变化的时候,服务器自动调用监听器对象中的方法。
组成:
1、Java的事件监听机制涉及到三个组件:事件源,事件监听器,事件对象;
2、当事件源上发生操作时,它会调用事件监听器的一个方法,并且调用这个方法时,会传递事件对象过来;
3、事件监听器是由开发人员编写,开发人员在事件监听器中,通过事件对象可以拿到事件源,从而对事件源上的操作进行处理
监听器常用用途:
统计在线人数,利用HttpSessionLisener
加载初始化信息:利用ServletContextListener
统计网站访问量
实现访问监控
......
监听器的分类:
按监听对象的不同可以划分为三种:
1、ServletContext监控:对应监控application内置对象的创建和销毁。
当web容器开启时,执行contextInitialized方法;当容器关闭或重启时,执行contextDestroyed方法。
实现方式:直接实现ServletContextListener接口:
public class MyServletContextListener implements ServletContextListener{
public void contextDestroyed(ServletContextEvent sce) { }
public void contextInitialized(ServletContextEvent sce) { } }
2、HttpSession监控:对应监控session内置对象的创建和销毁。
当打开一个新的页面时,开启一个session会话,执行sessionCreated方法;当页面关闭session过期时,或者容器关闭销毁时,执行sessionDestroyed方法。
实现方式:直接实现HttpSessionListener接口:public class MyHttpSessionListener implements HttpSessionListener{
public void sessionCreated(HttpSessionEvent arg0) { }
public void sessionDestroyed(HttpSessionEvent arg0) { } }
3、ServletRequest监控:对应监控request内置对象的创建和销毁。
当访问某个页面时,出发一个request请求,执行requestInitialized方法;当页面关闭时,执行requestDestroyed方法。
实现方式,直接实现ServletRequestListener接口:public class MyServletRequestListener implements ServletRequestListener{
public void requestDestroyed(ServletRequestEvent arg0) { }
public void requestInitialized(ServletRequestEvent arg0) { } }
按照监听事件的划分:
1、监听事件自身的创建和销毁:同上面的按对象划分。
2、监听属性的新增、删除和修改也是划分成三种,分别针对于ServletContext、HttpSession、ServletRequest对象:
2.1、ServletContext,实现ServletContextAttributeListener接口:
通过调用ServletContextAttribtueEvent的getName方法可以得到属性的名称public class MyServletContextAttrListener implements ServletContextAttributeListener{
public void attributeAdded(ServletContextAttributeEvent hsbe) {
System.out.println("In servletContext added :name = "+hsbe.getName()); }
public void attributeRemoved(ServletContextAttributeEvent hsbe) {
System.out.println("In servletContext removed :name = "+hsbe.getName()); }
public void attributeReplaced(ServletContextAttributeEvent hsbe) {
System.out.println("In servletContext replaced :name = "+hsbe.getName()); } }
2.2、HttpSession,实现HttpSessionAttributeListener接口:
public class MyHttpSessionAttrListener implements HttpSessionAttributeListener{
public void attributeAdded(HttpSessionBindingEvent hsbe) {
System.out.println("In httpsession added:name = "+hsbe.getName()); }
public void attributeRemoved(HttpSessionBindingEvent hsbe) {
System.out.println("In httpsession removed:name = "+hsbe.getName()); }
public void attributeReplaced(HttpSessionBindingEvent hsbe) {
System.out.println("In httpsession replaced:name = "+hsbe.getName()); } }
2.3、ServletRequest,实现ServletRequestAttributeListener接口:
public class MyServletRequestAttrListener implements ServletRequestAttributeListener{
public void attributeAdded(ServletRequestAttributeEvent hsbe) {
System.out.println("In servletrequest added :name = "+hsbe.getName()); }
public void attributeRemoved(ServletRequestAttributeEvent hsbe) {
System.out.println("In servletrequest removed :name = "+hsbe.getName()); }
public void attributeReplaced(ServletRequestAttributeEvent hsbe) {
System.out.println("In servletrequest replaced :name = "+hsbe.getName()); } }
3、监听对象的状态:
针对某些POJO类,可以通过实现HttpSessionBindingListener接口,监听POJO类对象的事件。例如:
public class User implements HttpSessionBindingListener,Serializable{
private String username;
private String password;
public String getUsername() { return username; }
public void setUsername(String username) { this.username = username; }
public String getPassword() { return password; }
public void setPassword(String password) { this.password = password; }
public void valueBound(HttpSessionBindingEvent hsbe) {
System.out.println("valueBound name: "+hsbe.getName()); }
public void valueUnbound(HttpSessionBindingEvent hsbe) {
System.out.println("valueUnbound name: "+hsbe.getName()); } }
Session数据的钝化与活化:
由于session中保存大量访问网站相关的重要信息,因此过多的session数据就会服务器性能的下降,占用过多的内存。因此类似数据库对象的持久化,web容器也会把不常使用的session数据持久化到本地文件或者数据中。这些都是有web容器自己完成,不需要用户设定。
不用的session数据序列化到本地文件中的过程,就是钝化;
当再次访问需要到该session的内容时,就会读取本地文件,再次放入内存中,这个过程就是活化。
类似的,只要实现HttpSeesionActivationListener接口就是实现钝化与活化事件的监听:public class User implements HttpSessionBindingListener, HttpSessionActivationListener,Serializable{
private String username;
private String password;
public String getUsername() { return username; }
public void setUsername(String username) { this.username = username; }
public String getPassword() { return password; }
public void setPassword(String password) { this.password = password; }
public void valueBound(HttpSessionBindingEvent hsbe) {
System.out.println("valueBound name: "+hsbe.getName()); }
public void valueUnbound(HttpSessionBindingEvent hsbe) {
System.out.println("valueUnbound name: "+hsbe.getName()); }
public void sessionDidActivate(HttpSessionEvent hsbe) {
System.out.println("sessionDidActivate name: "+hsbe.getSource()); } p
ublic void sessionWillPassivate(HttpSessionEvent hsbe) {
System.out.println("sessionWillPassivate name: "+hsbe.getSource()); } }
监听器的注册:
当我们自己写了一个监听器类时,一定要在WEB应用中注册。
1、web.xml
<listener>
<listener-class>com.listener.MyServletContextListener</listener-class>
</listener>2、使用注解
@WebListener
publicclass MyHttpSessionAttributeListener implements HttpSessionAttributeListener{
}