Spring Mvc那点事---(25)Spring Mvc监听器绑定session对象状态
绑定到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结尾的文件
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。
- 上一篇: 关于防止一个账号在不同机器上重复登录
- 下一篇: 使用session监听禁止用户重复登录