zoukankan      html  css  js  c++  java
  • Java 监听器

    1: 监听器的定义

    2:监听器的用途

    3:实例

    4:监听器分类

    5:Servlet3.0下监听器的使用

    6:实际项目常用的案例

    1: 监听器的定义:

    监听器实际上是一个类,这个类实现了特定的接口,然后将这个类在 web.xml 文件中进行描述,这样服务器在启动的时候就可以实例化这个类,启动监听器。当范围对象的状态发生变化的时候,服务器自动调用监听器对象中的方法。例如统计用户在线人数。

    web监听器是Servlet规范中定义的一种特殊类,用于监听ServletContext,HttpSession,ServletRequest等域对象的创建、销毁、以及属性的变化等,可以在事件发生前、发生后进行一些处理。

    2、监听器的用途

    • 1、统计在线人数和在线用户
    • 2、系统加载时进行信息的初始化工作
    • 3、统计网站的访问量
    • 4、跟Spring结合

    3、实例

    在web.xml文件中进行配置

    listenr-class中是自己定义的Listener的类路径

    public class MyRequestListener implements ServletContextListener{
    	public void contextInitialized(ServletContextEvent event){
    		System.out.println("启动监听器");
    	}
    
    	public void contextDestroy(ServletContextEvent event){
    		System.out.println("结束监听器");
    	}
    }
    

    编译器会为我们自动生成contextInitialized和contextDestroy两个函数

    监听器>过滤器>serlvet,在 web.xml 文件中配置的时候注意顺序

    4 监听器的分类

    按监听的对象划分,可以分为监听

    • ServletContext对象
    • HttpSession对象
    • ServletRequest对象

    按监听的事件划分

    • 域对象自身的创建和销毁
    • 域对象中属性的创建和消除
    • 绑定到session中的某个对象的状态

    由于很多监听的方式一致,因此我们只考虑其中的HttpSession对象:

    在web.xml中配置session超时

    <session-config>
        <session-timeout> 30 </session-timeout>
    </session-config>
    

    当超过30秒后,session会自动过期

    public class MyRequestListener implements HttpSessionListener{
    	public void sessionCreate(HttpSessionEvent event){
    		System.out.println("event 创建:");
    	}
    
    	public void sessionDestroy(HttpSessionEvent event){
    		System.out.println("event 销毁:");
    	}
    }
    

    另外,我们可以实现HttpSessionAttributeListener来实现session对象属性的增加(added)、删除(removed)、替换(replaced)的功能

    public class MyRequestListener implements HttpSessionAttributeListener{
    	public void attributeAdded(HttpSessionBindEvent event){
    		
    	}
    
    	public void attributeRemoved(HttpSessionBindEvent event){
    		
    	}
    
    	public void attributeReplaced(HttpSessionBindEvent event){
    		
    	}
    }
    

    5、Servlte3.0下的使用

    在Servlet3.0下通过注解@WebListener("")就可以实现了,不需要在web.xml中进行配置

    6、实战

    public class MyRequestListener implements HttpSessionListener{
    	private int counts = 0;//用于统计在线人数
    	public void sessionCreate(HttpSessionEvent event){
    		counts++;
    		event.getSession().getServletContext.setAttribute("name",counts); //用户人数+1
    	}
    
    	public void sessionDestroy(HttpSessionEvent event){
    		counts--;
    		event.getSession().getServletContext.setAttribute("name",counts);//用户人数-1
    	}
    }
    
    @WebListener
    public class MyRequestListener2 implements ServletRequestListener{
    	private ArrayList<User>list; //创建一个在线用户的列表
    	public void requestCreate(ServletRequestEvent event){
    		HttpServletRequest request = (HttpServletRequest) request.getServletContext();
    		String sessionId = request.getSession().getSessionId();
    		if(sessionId ...){
    			....
    			list.add(...);
    		}
    	}
    
    	public void requestDestroy(ServletRequestEvent event){
    
    	}
    }
    
    public class User{
    	private int sessionId;
    	
    }
    

    注意,以上代码只是大致的代码,并不全面和正确。

    参考:java Web开发技术应用 - 监听器

  • 相关阅读:
    mac os programming
    Rejecting Good Engineers?
    Do Undergrads in MIT Struggle to Obtain Good Grades?
    Go to industry?
    LaTex Tricks
    Convert jupyter notebooks to python files
    How to get gradients with respect to the inputs in pytorch
    Uninstall cuda 9.1 and install cuda 8.0
    How to edit codes on the server which runs jupyter notebook using your pc's bwroser
    Leetcode No.94 Binary Tree Inorder Traversal二叉树中序遍历(c++实现)
  • 原文地址:https://www.cnblogs.com/CBDoctor/p/4232859.html
Copyright © 2011-2022 走看看