jsp/servlet 标准不要求一个web容器支持分布式应用,但是他一定要支持HttpSessionActivationListener接口,以使代码可以支持分布式环境。
一般免费的web容器都不支持分布式,weblogic、websphere商务容器是支持的。
为了负载均衡(LoadBalance)或者fail-over(容错),web容器可以迁移一个session到其他的jvm。session的passivation(钝化)是指非活动的session被写入持久设备(比如硬盘)。activate(激活)是指从持久化设备上将session读取到内存中(JVM)。在分布式环境中切换的属性必须实现serializable接口。
一般情况下他和HttpSessionBindingListener一起使用。比如一个属性类:
public class attributeClass implements HttpSessionBindingListener, HttpSessionActivationListener {
// HttpSessionActivationListener
public void sessionDidActivate(HttpSessionEvent se) {
logout("sessionDidActivate("+se.getSession().getId()+")"); // 激活
}
public void sessionWillPassivate(HttpSessionEvent se) {
// 被传送到别的jvm或写到硬盘
logout("sessionWillPassivate(" + se.getSession().getId() + ")");
}
//HttpSessionBindingListener
public void valueBound(HttpSessionBindingEvent event) {
// 被设置到session中(setAttribute)
logout("valueBound(" + event.getSession().getId() + event.getValue() + ")");
}
public void valueUnbound(HttpSessionBindingEvent event) {
//从session中解除(removeAttribute)
logout("valueUnbound(" + event.getSession().getId() + event.getValue() + ")");
}
}这样你就可以将它加到session中
public class AAAServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
session.setAttribute("attribute", attributeClass);
}
}