`
stephen830
  • 浏览: 2969206 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

HttpSessionAttributeListener

    博客分类:
  • java
 
阅读更多

HttpSessionAttributeListener

监听已登录用户

当进行session操作时,如下:就会调用本监听

session.setAttribute("username","tom");

session.removeAttribute("username");

 

 

 

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

import com.stephen.utility.DateTool;

@WebListener
public class MyHttpSessionAttributeListener implements HttpSessionAttributeListener {

	@Override
	public void attributeAdded(HttpSessionBindingEvent event) {
		System.out.println(DateTool.date2String()+"->[MyHttpSessionAttributeListener]"+event.getName()+"="+event.getValue()+" attributeAdded");
	}

	@Override
	public void attributeRemoved(HttpSessionBindingEvent event) {
		System.out.println(DateTool.date2String()+"->[MyHttpSessionAttributeListener]"+event.getName()+"="+event.getValue()+" attributeRemoved");
	}

	@Override
	public void attributeReplaced(HttpSessionBindingEvent event) {
		System.out.println(DateTool.date2String()+"->[MyHttpSessionAttributeListener]"+event.getName()+"="+event.getValue()+" attributeReplaced");
	}

}

 

 

 

    package com.nbrc.lddw.util;  
      
    import javax.servlet.http.HttpSession;  
    import javax.servlet.http.HttpSessionAttributeListener;  
    import javax.servlet.http.HttpSessionBindingEvent;  
    import javax.servlet.http.HttpSessionBindingListener;  
      
    import org.apache.commons.logging.Log;  
    import org.apache.commons.logging.LogFactory;  
    import org.springframework.context.ApplicationContext;  
    import org.springframework.web.context.support.WebApplicationContextUtils;  
      
    import com.nbrc.lddw.interceptor.AuthorizeInterceptor;  
    import com.nbrc.lddw.model.OnlineInfo;  
    import com.nbrc.lddw.model.User;  
    import com.nbrc.lddw.service.OnlineUserService;  
    /** 
     *  
     * @author fox 
     * @date 2009-02-09 
     * @description 已登录用户的监听 
     */  
    public class OnlineUserListener implements HttpSessionAttributeListener {  
        private static Log log = LogFactory.getLog(OnlineUserListener.class);  
      
        public void attributeAdded(HttpSessionBindingEvent hse) {  
            log.info("value bound! make session info ...");  
            HttpSession session = hse.getSession();       
            ApplicationContext context =   
                WebApplicationContextUtils.getRequiredWebApplicationContext(session.getServletContext());  
            OnlineUserService svr = (OnlineUserService)context.getBean("onlineService");  
            User u = null;  
            if(session.getAttribute(AuthorizeInterceptor.USR_KEY)!=null)  
                u = (User) session.getAttribute(AuthorizeInterceptor.USR_KEY);  
            if(u!=null && svr.findByUserId(u.getId())==null){             
                    OnlineInfo info = new OnlineInfo();  
                    info.setSessionId(session.getId());  
                    info.setUserId(u.getId());  
                    svr.save(info);  
            }else{  
                    log.error("can't get user in session");  
            }         
        }  
      
        public void attributeRemoved(HttpSessionBindingEvent hse) {  
            HttpSession session = hse.getSession();  
            ApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(session.getServletContext());  
            OnlineUserService svr = (OnlineUserService)context.getBean("onlineService");  
            if(svr.findById(session.getId())!=null){  
                svr.removeById(session.getId());                  
            }  
        }  
      
        public void attributeReplaced(HttpSessionBindingEvent se) {  
            // TODO Auto-generated method stub  
              
        }  
          
      
    }  

 

 

 

分享到:
评论

相关推荐

    servlet2.4doc

    attributeAdded(HttpSessionBindingEvent) - Method in interface javax.servlet.http.HttpSessionAttributeListener Notification that an attribute has been added to a session. attributeAdded...

    HttpSessionBindingListener实现在线人员查看即人数统计

    NULL 博文链接:https://franciswmf.iteye.com/blog/1831974

    学习servlet的实例和参考api

    学习servlet的实例 说明文件 blog ...类 HttpSession Cookie HttpSessionAttributeListener HttpServlet HttpServletRequest HttpServeltResponse <br>

    企业drp系统经典源码

    企业drp系统经典实现。 包括: v1.0 ... * HttpSessionAttributeListener * ServletContextListener v3.1 * JfreeChart的应用 v3.2 * 采用Ajax实现下拉列表的联动 v3.3 * 验证码的使用

    servlet-api-2.4.jar.zip

    javax.servlet.Filter javax.servlet.Servlet javax.servlet.FilterChain ...javax.servlet.FilterConfig ...javax.servlet.http.HttpSessionAttributeListener javax.servlet.http.HttpSessionActivationListener

    javax.servlet.jar下载

    javax.servlet.http.HttpSessionAttributeListener.class javax.servlet.http.HttpServlet.class javax.servlet.http.HttpServletRequest.class javax.servlet.http.HttpServletResponse.class javax.servlet....

    监听器显示登陆用户列表,并实现踢人功能

    监听器显示登陆用户列表,并实现踢人功能 HttpSessionAttributeListener

    java监听器学习 统计在线人数

    实现在线人的显示,通过实现ServletContenxListener,HttpSessionListener,HttpSessionAttributeListener来实现在线人的展示

    JAVA J2EE 类库文档

    HttpSessionAttributeListener HttpSessionBindingEvent HttpSessionBindingListener HttpSessionContext HttpSessionEvent HttpSessionListener HttpUtils RequestDispatcher Servlet ServletConfig ...

    SpringSpringMvcMybatis

    ################ tips...借助Junit和Spring TestContext framework 和RestController监听器的作用: 监听对象 监听对象的属性 1:统计在线人数和在线用户 HttpSessionListener httpSessionAttributeListener 2:系统启

    java session出现的错误

    好郁闷哦,搞了两个多小时,我把先把HttpSessionAttributeListener 接口的方法实现了

    Servlet监听器例子

    关于Servlet监听器的几个例子,用于统计在线人数等等

Global site tag (gtag.js) - Google Analytics