session的生命周期
程序员文章站
2022-04-23 16:55:50
...
一 创建
二 活动
三 销毁
四 实例
1、session_page1.jsp
<%@ page language="java" import="java.util.*,java.text.*" contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h1>session内置对象</h1>
<hr>
<%
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
Date d = new Date(session.getCreationTime());
session.setAttribute("username", "admin");
session.setAttribute("password", "123456");
session.setAttribute("age", 20);
//设置当前session最大生成期限单位是秒
//session.setMaxInactiveInterval(10);//10秒钟
//session.invalidate();//销毁当前会话。
%>
Session创建时间:<%=sdf.format(d)%><br>
Session的ID编号:<%=session.getId()%><BR>
从Session中获取用户名:<%=session.getAttribute("username") %><br>
<%
//session.invalidate();//销毁当前会话
%>
<a href="session_page2.jsp" target="_blank">跳转到Session_page2.jsp</a>
</body>
</html>
2、session_page2.jsp
<%@ page language="java" import="java.util.*,java.text.*" contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h1>session内置对象</h1>
<hr>
<%
//SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
//Date d = new Date(session.getCreationTime());
//session.setAttribute("username", "admin");
%>
Session的ID编号:<%=session.getId()%><BR>
从Session中获取用户名:<%=session.getAttribute("username") %><br>
Session中保存的属性有:<%
String[] names =session.getValueNames();
for(int i=0;i<names.length;i++)
{
out.println(names[i]+" ");
}
%> <br>
</body>
</html>
五 测试某次会话中通过超链接打开的新页面属于同一次会话
六 测试只要当前会话页面没有全部关闭,重新打开新的浏览器窗口访问同一项目资源时,属于同一次会话
七 测试通过调用session.invalidate()方法对session进行销毁
在代码中调用session.invalidate();//销毁当前会话
八 Tomcat超时时间设置方法
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 设置会话一分钟后过期 -->
<session-config>
<session-timeout>1</session-timeout>
</session-config>
</web-app>
九 小知识点
1、Tomcat后台管理方法