(一)绑定与解绑的监听器(HttpSessionBindingListener)
绑定状态:将一个对象放入到Session域中
解绑状态:将一个对象从Session域中移除
(二)写个例子查看绑定状态和解绑状态
(a)Person.java
package cn.shu.domain;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
/*Person实现对象感知监听器,不需要再web.xml中注册*/
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 arg0) {
/*绑定的方法*/
System.out.println("Person被绑定了");
}
@Override
public void valueUnbound(HttpSessionBindingEvent arg0) {
/*解绑的方法*/
System.out.println("Person被解绑了");
}
}
(b)TestBindingServlet.java
package cn.shu.domain;
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;
/**
* Servlet implementation class TestBindingServlet
*/
public class TestBindingServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public TestBindingServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/*先获得Session,创建person对象,再将person对象绑定(放)到Session域中*/
HttpSession session = request.getSession();
Person p = new Person();
p.setId("99");
p.setName("stephen chow");
session.setAttribute("personname", p);
/*person对象和session域中解绑*/
session.removeAttribute("personname");
/*在浏览器中输入http://localhost:8080/Listener/testBindingServlet
* 经行测试*/
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
(c)web.xml
Servlet的配置,不用Listener相关配置,对象感知监听器比较特殊
TestBindingServlet
TestBindingServlet
cn.shu.domain.TestBindingServlet
TestBindingServlet
/testBindingServlet
(d)启动项目,在浏览器中输入:http://localhost:8080/Listener/testBindingServlet,控制台:
使用setAttribute()和removeAttribute()来将对象放入session域和移除出session域
(三)钝化与活化的监听器(HttpSessionActivationListener)
钝化状态:将session域中的对象持久化到硬盘中
活化状态:将硬盘中的对象恢复到session域中
(四)示例钝化状态和活化状态
/*实现钝化与活化监听器,想要钝化必须实现Serializable接口*/
(a)Customer.java
package cn.shu.domain;
import java.io.Serializable;
import javax.servlet.http.HttpSessionActivationListener;
import javax.servlet.http.HttpSessionEvent;
/*实现钝化与活化监听器,想要钝化必须实现Serializable接口*/
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 sessionDidActivate(HttpSessionEvent arg0) {
/*活化*/
System.out.println("customer被活化");
}
@Override
public void sessionWillPassivate(HttpSessionEvent arg0) {
/*钝化*/
System.out.println("customer被钝化");
}
}
(b)TestCustomerActiveServlet.java
package cn.shu.domain;
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;
/**
* Servlet implementation class TestCustomerActiveServlet
*/
public class TestCustomerActiveServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public TestCustomerActiveServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/*将customer放到session域中*/
HttpSession session = request.getSession();
Customer customer = new Customer();
customer.setId("99");
customer.setName("大鱼海棠");
session.setAttribute("customername", customer);
System.out.println("customer被放到session域中了");
//response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
(c)TestCustomerActiveAgainServlet.java
package cn.shu.domain;
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;
/**
* Servlet implementation class TestCustomerActiveServlet
*/
public class TestCustomerActiveServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public TestCustomerActiveServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/*将customer放到session域中*/
HttpSession session = request.getSession();
Customer customer = new Customer();
customer.setId("99");
customer.setName("大鱼海棠");
session.setAttribute("customername", customer);
System.out.println("customer被放到session域中了");
//response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
(d)web.xml
/*监听器同样不需要配置,只需要配置Servlet*/
TestCustomerActiveServlet
TestCustomerActiveServlet
cn.shu.domain.TestCustomerActiveServlet
TestCustomerActiveServlet
/testCustomer
TestCustomerActiveAgainServlet
TestCustomerActiveAgainServlet
cn.shu.domain.TestCustomerActiveAgainServlet
TestCustomerActiveAgainServlet
/testCustomer2
(e)context.xml
(f)文件结构如下:
(g)启动项目,在浏览器中输入:http://localhost:8080/Listener/testCustomer
在context.xml配置文件中设定customer一分钟不使用就钝化,所以一分钟后会打印customer被钝化的信息
在浏览器中输入:http://localhost:8080/Listener/testCustomer2
customer可以活化,但一分钟后有钝化了