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

    监听器
    ServletContextListener  监听servlet的初始化和结束
    httpSessionListener      监听session的创建和结束 ,可以利用这个来统计在线登录的人数..
    package com.zh;
    
    import javax.servlet.http.HttpSessionEvent;
    import javax.servlet.http.HttpSessionListener;
    
    public class LoginListener implements HttpSessionListener{
        private static int count;
        public static int getCount(){
            return count;
        }
        public void sessionCreated(HttpSessionEvent arg0) {
            // TODO Auto-generated method stub
            count++;
        }
    
        public void sessionDestroyed(HttpSessionEvent arg0) {
            // TODO Auto-generated method stub
            count--;
        }
    }
    
    注意要在web.xml文件中 设置
    <listener>
          <listener-class>com.zh.Test1</listener-class>
      </listener>
    
    
    ServletContextAttributeListener    当servletContext对象被设置,删除,替换
    HttpSessionAttributeListener      当HttpSession对象被设置,删除,替换
    ServletRequestAttributeListener    当ServletRequest对象被设置,删除,替换
    HttpSessionBindingListener        当HttpSession中添加属性和删除属性
    注意: HttpSessionBindingListener不用在web.xml中设置       

    监听器

    ServletContextListener  监听servlet的初始化和结束

    httpSessionListener      监听session的创建和结束 ,可以利用这个来统计在线登录的人数..

    package com.zh;

     

    import javax.servlet.http.HttpSessionEvent;

    import javax.servlet.http.HttpSessionListener;

     

    public class LoginListener implements HttpSessionListener{

       private static int count;

       public static int getCount(){

          return count;

       }

       public void sessionCreated(HttpSessionEvent arg0) {

          // TODO Auto-generated method stub

          count++;

       }

     

       public void sessionDestroyed(HttpSessionEvent arg0) {

          // TODO Auto-generated method stub

          count--;

       }

    }

    注意要在web.xml文件中 设置

    <listener>

      <listener-class>com.zh.Test1</listener-class>

      </listener>

    ServletContextAttributeListener    当servletContext对象被设置,删除,替换

    HttpSessionAttributeListener      当HttpSession对象被设置,删除,替换

    ServletRequestAttributeListener    当ServletRequest对象被设置,删除,替换

    HttpSessionBindingListener        当HttpSession中添加属性和删除属性

    注意: HttpSessionBindingListener不用在web.xml中设置      

  • 相关阅读:
    [LeetCode] Insert Interval
    java 可变參数
    谈谈单元測试之(二):測试工具 JUnit 3
    我的csdn博客搬家了
    leetcode 229: Majority Element II
    向MapReduce转换:生成用户向量
    《31天成为IT服务达人》最新文件夹
    SD卡读写之FileNotFoundException: /storage/emulated/0object.txt: open failed: ENOENT (No such file or dir
    当写烂代码的人离职之后....
    JavaSE Map的使用
  • 原文地址:https://www.cnblogs.com/shaoshao/p/3349300.html
Copyright © 2011-2022 走看看