Listener监听器
javeee中有个8中监听器
3种分别对quest,session,context的监听
3种分别对quest,session,context增删改的监听
对session内对象的增删的监听,将session在磁盘钝化和重新加载的监听
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package listen; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.ServletRequestEvent; import javax.servlet.ServletRequestListener; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; public class ListenTest implements HttpSessionListener, ServletContextListener, ServletRequestListener { Log log = LogFactory.getLog(getClass()); @Override public void requestDestroyed(ServletRequestEvent sre) { // TODO Auto-generated method stub HttpServletRequest request = (HttpServletRequest) sre .getServletRequest(); long time = System.currentTimeMillis() - (Long) request.getAttribute("dateCreate"); log.info(request.getRemoteAddr() + "请求处理结束,用时" + time); } @Override public void requestInitialized(ServletRequestEvent sre) { // TODO Auto-generated method stub HttpServletRequest request = (HttpServletRequest) sre .getServletRequest(); String uri = request.getRequestURI(); uri = request.getQueryString() == null ? uri : (uri + "?" + request .getQueryString()); request.setAttribute("dateCreate", System.currentTimeMillis()); log.info("ip" + request.getRemoteAddr() + "请求" + uri); } @Override public void contextDestroyed(ServletContextEvent sce) { // TODO Auto-generated method stub ServletContext servletContext = sce.getServletContext(); log.info("即将关闭" + servletContext.getContextPath()); } @Override public void contextInitialized(ServletContextEvent sce) { // TODO Auto-generated method stub ServletContext servletContext = sce.getServletContext(); log.info("即将启动" + servletContext.getContextPath()); } @Override public void sessionCreated(HttpSessionEvent se) { // TODO Auto-generated method stub HttpSession session = se.getSession(); log.info("新创一个session,ID为:" + session.getId()); } @Override public void sessionDestroyed(HttpSessionEvent se) { // TODO Auto-generated method stub HttpSession session = se.getSession(); log.info("销毁一个session,id为:" + session.getId()); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package listen; import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSessionAttributeListener; import javax.servlet.http.HttpSessionBindingEvent; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; public class SessAttributeListenerTest implements HttpSessionAttributeListener { Log log = LogFactory.getLog(getClass()); @Override public void attributeAdded(HttpSessionBindingEvent event) { // TODO Auto-generated method stub HttpSession session = event.getSession(); String name = event.getName(); log.info("新建session属性" + name + "值为" + event.getValue()); } @Override public void attributeRemoved(HttpSessionBindingEvent event) { // TODO Auto-generated method stub HttpSession session = event.getSession(); String name = event.getName(); log.info("删除session属性" + name + "值为" + event.getValue()); } @Override public void attributeReplaced(HttpSessionBindingEvent event) { // TODO Auto-generated method stub HttpSession session = event.getSession(); String name = event.getName(); log.info("修改session属性" + name + "原来值为" + event.getValue() + "现在的值为" + session.getAttribute(name)); } }