对session钝化和活化关于sessionid的查看
1. 编写以下两个类
@WebServlet("/sessionDemo2")
public class SessionDemo2 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
session.setAttribute("msg","hello");
System.out.println(session);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
@WebServlet("/sessionDemo1")
public class SessionDeno1 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
Object msg = session.getAttribute("msg");
System.out.println(msg);
System.out.println(session);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
2. 运行代码,且找到该项目的部署的项目,打包成war格式后放到本地tomcat的webapps下
项目位置的out目录下
进入aritifacts
3. 停下tomcat,打开本地的tomcat
会自己生成(day16的文件夹)
4.访问项目sessionDemo1和sessionDemo2
5. 重启服务器,注意此时要正常关闭服务器
可以看到序列化的session文件
重启后session文件消失
再次访问sessionDemo2,显然两次的id不一样了(但数据不变,因为在此期间没有关闭过浏览器,用同一个浏览器获得的)
有点问题,忘记输出两次的msg的值了,后来再试了,msg值依然为hello