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生命周期从容器启动到容器关闭