1 知识点
2 绑定与解绑代码演示
1 person类
package www.test.domian; import javax.servlet.http.HttpSessionBindingEvent; import javax.servlet.http.HttpSessionBindingListener; public class Person implements HttpSessionBindingListener{ private String id; private String name; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override //绑定的方法 public void valueBound(HttpSessionBindingEvent event) { System.out.println("person被绑定了"); } @Override //解绑方法 public void valueUnbound(HttpSessionBindingEvent event) { System.out.println("person被解绑了"); } }
2 TestPersonBindingServlet
package www.test.domian; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class TestPersonBindingServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); //将person对象绑到session中 Person p = new Person(); p.setId("100"); p.setName("zhangsanfeng"); session.setAttribute("person", p); //将person对象从session中解绑 session.removeAttribute("person"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
3 钝化与活化监听器
4 钝化与活化代码演示
1 自定义Customer 类
必须要实现:implements HttpSessionActivationListener,Serializable这两个接口
package www.test.domian; import java.io.Serializable; import javax.servlet.http.HttpSessionActivationListener; import javax.servlet.http.HttpSessionEvent; public class Customer implements HttpSessionActivationListener,Serializable{ private String id; private String name; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override //钝化 public void sessionWillPassivate(HttpSessionEvent se) { System.out.println("customer被钝化了"); } @Override //活化 public void sessionDidActivate(HttpSessionEvent se) { System.out.println("customer被活化了"); } }
2 TestCustomerActiveServlet 测试钝化
package www.test.domian; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class TestCustomerActiveServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); //将customer放到session中 Customer customer =new Customer(); customer.setId("200"); customer.setName("lucy"); session.setAttribute("customer", customer); System.out.println("customer被放到session域中了"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
3 TestCustomerActiveServlet2 测试活化
package www.test.domian; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class TestCustomerActiveServlet2 extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //从session域中获得customer HttpSession session = request.getSession(); Customer customer = (Customer) session.getAttribute("customer"); System.out.println(customer.getName()); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
4 配置文件
<?xml version="1.0" encoding="UTF-8"?> <Context> <!-- maxIdleSwap:session中的对象多长时间不使用就钝化 --> <!-- directory:钝化后的对象的文件写到磁盘的哪个目录下 配置钝化的对象文件在 work/catalina/localhost/钝化文件 --> <Manager className="org.apache.catalina.session.PersistentManager" maxIdleSwap="1"> <Store className="org.apache.catalina.session.FileStore" directory="webtest23" /> </Manager> </Context>