Spring Mvc那點事---(25)Spring Mvc監聽器繫結session物件狀態
阿新 • • 發佈:2019-01-09
繫結到session物件中的屬性可以通過一些方式知道自己的狀態,可以知道繫結到session,從session中解除繫結,以及物件被儲存到到裝置上,比如硬碟,或者從硬碟中恢復等。要實現些功能,需要通過HttpSessionBindingListener介面和HttpSessionActivationListener介面來完成. 這兩個介面可以直接使用,不需要在web.xml中進行配置
1.HttpSessionBindingListener
HttpSessionBindingListener介面主要監聽物件自己繫結到session,以及何時從session物件中解除,主要有兩個方法,繫結和解除繫結。我們建立一個類。
public class User implements HttpSessionBindingListener { public String userName; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public void valueBound(HttpSessionBindingEvent arg0) { // TODO Auto-generated method stub System.out.println("開始繫結"+arg0.getName()); } public void valueUnbound(HttpSessionBindingEvent arg0) { // TODO Auto-generated method stub System.out.println("解除繫結"+arg0.getName()); } }
執行網站,可以看到如下結果<%@page import="com.selfListener.User"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% //繫結屬性 User user= new User(); user.setUserName("liming"); session.setAttribute("user",user); //刪除屬性 session.removeAttribute("user"); %> </body> </html>
開始繫結user
解除繫結user
2.HttpSessionActivationListener
HttpSessionActivationListener介面可以將session物件序列化儲存到硬碟上,在服務重啟後可以重新從硬碟上讀取,繼承HttpSessionActivationListener介面的時候,同時要繼承Serializable介面
public class Person implements HttpSessionActivationListener,Serializable {
public void sessionDidActivate(HttpSessionEvent arg0) {
// TODO Auto-generated method stub
System.out.println("sessionDidActivate--反序列化"+arg0.getSession().getAttribute("person")+"--sessionid"+arg0.getSession().getId());
}
public void sessionWillPassivate(HttpSessionEvent arg0) {
// TODO Auto-generated method stub
System.out.println("sessionWillPassivate--系列化到硬碟"+arg0.getSession().getAttribute("person")+"--sessionid"+arg0.getSession().getId());
}
public String personName;
public String getPersonName() {
return personName;
}
public void setPersonName(String personName) {
this.personName = personName;
}
}
<%
//繫結屬性
com.selfListener.Person p=new com.selfListener.Person();
p.setPersonName("lucy");
session.setAttribute("person",p);
%>
需要在web.xml新增配置
<listener>
<listener-class>com.selfListener.Person</listener-class>
</listener>
然後需要配置session儲存路徑
可以在tomcat下找到context.xml或者在專案下面找到context.xml檔案
修改儲存路徑配置
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
--><!-- The contents of this file will be loaded for each web application --><Context>
<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
-->
<!-- Uncomment this to enable Comet connection tacking (provides events
on session expiration as well as webapp lifecycle) -->
<!--
<Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
-->
<Manager className="org.apache.catalina.session.PersistentManager" maxIdleSwap="1">
<Store className="org.apache.catalina.session.FileStore" directory="F:/java/apache-tomcat-7.0.69/work"/>
</Manager>
</Context>
然後在這個目錄下就可以找到以.session結尾的檔案