當HttpSession中止(通過顯示地失效或超時)時,Web容器會把HttpSession屬性從HttpSession中清除。
javax.servlet.http.HttpSessionBindingListener接口定義如下:
1 public interface HttpSessionBindingListener { 2 public void valueUnbound(HttpSessionBindingEvent hsbe); 3 public void valueBound(HttpSessionBindingEvent hsbe); 4 }
頁面打開時即通過打開事件訪問服務器端點時把實現了HttpSessionBindingListener接口的對象添加到屬性中后,在HttpSession失效時,通過該方法就能通知WebSocket端點實例,給了一個關閉WebSocket連接的機會,保證了HttpSession和WebSocket Session之間的強關聯性。
參考資料
《Java WebSocket編程》 P224-225