Listener

Listener   监听器

监听容器中的某一动作,并根据此动作按照需求做出相应响应。

常用的监听器有:

监听对象产生和销毁:

ServletContextListener        

HttpSessionListener

ServletRequestListener

监听属性的改变:

ServletContextAttributeListener        

HttpSessionAttributeListener

ServletRequestAttributeListener

需要在web.xml中配置

<listener>
    <listener-class>
        com.xxx.xxx.MyOwnListener
    </listener-class>
</listener>

ServletContextListener

public class IntrospectorCleanupListener implements ServletContextListener
{
  public void contextInitialized(ServletContextEvent event)
  {    //CachedIntrospectionResults.acceptClassLoader(Thread.currentThread().getContextClassLoader());
  }
  
  public void contextDestroyed(ServletContextEvent event)
  {    //CachedIntrospectionResults.clearClassLoader(Thread.currentThread().getContextClassLoader());
    //Introspector.flushCaches();
  }
}

HttpSessionListener           利用session监听在线人数

public class HttpSessionMutexListener implements HttpSessionListener
{
  //当前用户数
	private int userCounts=0;
	@Override
	public void sessionCreated(HttpSessionEvent se) {
		//sessionCreated  用户数+1
		userCounts++;
		//重新在servletContext中保存userCounts
		se.getSession().getServletContext().setAttribute("userCounts", userCounts);
		
	}
	@Override
	public void sessionDestroyed(HttpSessionEvent se) {
		//sessionDestroyed  用户数-1
		userCounts--;
		//重新在servletContext中保存userCounts
		se.getSession().getServletContext().setAttribute("userCounts", userCounts);
		
		@SuppressWarnings("unchecked")
		ArrayList<User> userList=(ArrayList<User>) se.getSession().getServletContext().getAttribute("userList");
		String sessionId=se.getSession().getId();
		//如果当前用户在userList中  在session销毁时  将当前用户移出userList
		if(SessionUtil.getUserBySessionId(userList, sessionId)!=null){
			userList.remove(SessionUtil.getUserBySessionId(userList, sessionId));
		}
		//将userList集合  重新保存到servletContext
		se.getSession().getServletContext().setAttribute("userList", userList);
	} 
}

ServletRequestListener

 public class MyRequestListener implements ServletRequestListener{
    public void requestDestroyed(ServletRequestEvent sre) {
        HttpServletRequest request=(HttpServletRequest) sre.getServletRequest();                
    }

    public void requestInitialized(ServletRequestEvent sre) {
        HttpServletRequest request=(HttpServletRequest) sre.getServletRequest();
        String uri=request.getRequestURI();
        uri=request.getQueryString()==null?uri:(uri+"?"+request.getQueryString());
        log.info("ip"+request.getRemoteAddr()+uri);
        request.setAttribute("time", System.currentTimeMillis());        
    }
}

ServletContextAttributeListener        

HttpSessionAttributeListener

ServletRequestAttributeListener

以HttpSessionAttributeListener为例:

public class SessionAttributeListener  implements HttpSessionAttributeListener{

    Log log=LogFactory.getLog(getClass());
    public void attributeAdded(HttpSessionBindingEvent se) {
        HttpSession httpSession=se.getSession();
        log.info("新建属性:"+se.getName()+"值:"+se.getValue());       
    }

    public void attributeRemoved(HttpSessionBindingEvent se) {
        HttpSession httpSession=se.getSession();
        log.info(" 删除属性:"+se.getName()+"值:"+se.getValue());        
    }

    public void attributeReplaced(HttpSessionBindingEvent se) {
        HttpSession httpSession=se.getSession();
        log.info(" 修改属性:"+se.getName()+"原来的值:"+se.getValue()+"新值:"+httpSession.getAttribute(se.getName()));
        
    }

}

以上监听对象和监听属性的监听器方法中都可以通过传入的事件对象来获取当前引起触发事件的对象。

Listener生命周期从容器启动到容器关闭

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值