HttpSesstionActivationLIstener示例
HttpSesstionActivationLIstener示例:
http://www.cnblogs.com/xdp-gacl/p/3969249.html
钝化的session会已sessionid为文件名,存储于\apache-tomcat-7.0.35\work\Catalina\localhost\TestBootWeb\stono目录下面;
META-INF/context.xml:
<Context> <Manager className="org.apache.catalina.session.PersistentManager" maxIdleSwap="1"> <Store className="org.apache.catalina.session.FileStore" directory="stono" /> </Manager> </Context>
HttpSessionActivationListener:
package com.stono.servlet.listeners; import java.io.Serializable; import javax.servlet.http.HttpSessionActivationListener; import javax.servlet.http.HttpSessionEvent; /* 文档中提到进行JVM迁移的时候才能激发此事件; */ public class MyHttpSessionActivationListener implements HttpSessionActivationListener, Serializable { private static final long serialVersionUID = 1L; public MyHttpSessionActivationListener() { } public MyHttpSessionActivationListener(String name) { this.name = name; } private String name; public String getName() { return name; } @Override public void sessionDidActivate(HttpSessionEvent arg0) { System.out.println("com.stono.servlet.listeners.MyHttpSessionActivationListener.sessionDidActivate(HttpSessionEvent):" + arg0.getSession().getId()); } @Override public void sessionWillPassivate(HttpSessionEvent arg0) { System.out.println("com.stono.servlet.listeners.MyHttpSessionActivationListener.sessionWillPassivate(HttpSessionEvent):" + arg0.getSession().getId()); } public void setName(String name) { this.name = name; } }
servlet:
package com.stono.servlet.listenerorder; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.stono.servlet.listeners.MyHttpSessionActivationListener; public class TestServlet1 extends HttpServlet { private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { HttpSession session = req.getSession(); session.setAttribute("a", new MyHttpSessionActivationListener("stono")); PrintWriter writer = resp.getWriter(); writer.println("this is from com.stono.servlet.listenerorder.TestServlet1"); } }