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

    监听器存在以下对象

    监听者:XxxxxListener - 所的监听者是的接口。

    被监听者 :任意对象都可以成为被监听者 - 早已经存在。

    监听到的事件:XxxxEvent- 永远是一个具体类,用来放监听到的数据

                                里面都有一个方法叫getSource() – 返回的是监听到对象

    在Javaweb中存在三个被监听对象:       HttpServletRequest       HttpSessoin       ServletContext

    监听者· 被监听着 监听对象
    HttpSessionActivationListener HttpSession – 监听HttpSession活化和顿化。 HttpSessionEvent
    HttpSessionAttributeListener HttpSession – 监听session的属性变化的。S.setAttributee(); HttpSessionBindingEvent
    HttpSessionBindingListener HttpSession - 监听哪一个对象,绑定到了session上。S.setAtrri(name,User);  
    HttpSessionListener HttpSesion – 监听sessioin创建销毁 HttpSessionEvent
    ServletContextAttributeListener ServletContext – 属性变化的  
    ServletContextListener servletContext 创建销毁  
    ServletRequestListener - serlvetRequestAttibuteListner Request -创建销毁  

    创建一个监听器:

    1、创建类实现监听者接口

    2、在web。xml中注册

    1 <listener>
    2         <listener-class>cn.test.listener.MyLitener</listener-class>
    3 </listener>

    Demo:实现统计在线人数和访问IP

    1、MySessionLitener

     1 public class MySessionLitener implements HttpSessionListener {
     2     private Integer onLine=1;
     3     public void sessionCreated(HttpSessionEvent se) {
     4         System.out.println("有人访问了。。。。");
     5         HttpSession h1= se.getSession();
     6         HttpSession h2=(HttpSession) se.getSource();
     7         System.out.println("h1:"+h1.hashCode());
     8         System.out.println("h2:"+h2.hashCode());
     9         ServletContext sc= se.getSession().getServletContext();
    10         sc.setAttribute("OnLine", onLine++);
    11         List<HttpSession> list=(List<HttpSession>) sc.getAttribute("sessions");
    12         if(list==null)
    13         {
    14             list=new ArrayList<HttpSession>();
    15             sc.setAttribute("sessions", list);
    16         }
    17         list.add(h2);
    18     }
    19 
    20     public void sessionDestroyed(HttpSessionEvent arg0) {
    21         System.out.println("有人退出。。。");
    22         
    23     }
    24 
    25 }

    2、IPFilter

     1 public class IPFilter implements Filter{
     2 
     3     public void destroy() {
     4         
     5     }
     6 
     7     public void doFilter(ServletRequest request, ServletResponse response,
     8             FilterChain chain) throws IOException, ServletException {
     9         HttpServletRequest req=(HttpServletRequest) request;
    10         if(req.getAttribute("IP")==null)
    11         {
    12             req.getSession().setAttribute("IP", req.getRemoteAddr());
    13         }
    14         chain.doFilter(req, response);
    15     }
    16 
    17     public void init(FilterConfig filterConfig) throws ServletException {
    18 
    19     }
    20     
    21 }

    3、jsp页面

    1  在线人数:${OnLine }<br/>
    2     访问IP:<br/>
    3     <%
    4     List<HttpSession> list=(List<HttpSession>)application.getAttribute("sessions");
    5     for(HttpSession s:list)
    6     {
    7         out.println(s.getAttribute("IP")+"<br/>");
    8     }
    9      %>
  • 相关阅读:
    Chrome使用video无法正常播放MP4视频的解决方案
    ArcGIS 按掩膜裁剪地形
    bilibili 分P视频 需下载哔哩哔哩投稿工具
    U盘/硬盘数据恢复
    visio 2010、2013、2016、2019安装包
    经纬度WGS84地理坐标系转换成CGCS2000坐标系步骤,必备!
    Git 配置多个用户邮箱以及在 Sourcetree 中使用
    Chrome 插件 一键保存当前打开的所有标签页
    Cesium粒子系统学习
    破解ArcGIS坐标系之惑: 从基本概念到常用操作
  • 原文地址:https://www.cnblogs.com/liuwt365/p/4136044.html
Copyright © 2011-2022 走看看