session的序列化
我们先来一个小例子
我们来一个a.jsp
<html>
<head>
<title>a</title>
</head>
<body>
<%
session.setAttribute("name", "my name is Alice");
%>
</body>
</html>
在session中存入一个name属性
然后我们再来一个b.jsp
<html>
<head>
<title>b</title>
</head>
<body>
<%
String name = (String) session.getAttribute("name");
out.print(name);
%>
</body>
</html>
从session中取出属性name
然后输出一下
我们先访问a.jsp,然后再访问b.jsp
就能看到b.jsp输出了my name is Alice
如果我们关闭浏览器
重新开启然后访问b.jsp,那么就是null
因为关闭浏览器之后清除了session
关键的问题来了
如果我们重启服务器
然后访问b.jsp
我们发现,居然不是null
而是成功输出了my name is Alice
这是因为在关闭服务器的时候
服务器会在tomcat的work目录下
生成一个SESSIONS.ser的文件
然后重启的时候,会重新加载这个SESSIONS.ser文件
所以session有了这样的死而复生的能力
这个功能是tomcat服务器来控制的
那么如果想要关闭这个功能
可以打开tomcat中的conf目录
找到context.xml
找到一个属性
<Manager pathname="" />
把这个删了就行了
要这个功能的话就加上就行了
<Context>
<!--
Default set of monitored resources. If one of these changes, the
-->
<!-- web application will be reloaded. -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<WatchedResource>WEB-INF/tomcat-web.xml</WatchedResource>
<WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
<!--
Uncomment this to disable session persistence across Tomcat restarts
-->
<!--
<Manager pathname="" />
-->
</Context>