定义一个page1
<%@ page language="java" import="java.util.*,java.text.*" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>session</title>
</head>
<body>
<h1>session对象</h1>
<%SimpleDateFormat ndf=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","geyao");//设置session的最大期限为十秒session.setMaxInactiveInterval(10);
%>
<hr>
Session创建时间: <%=ndf.format(d) %><br>
Session的ID编号:<%=session.getId() %><br>
从Session中获取用户名:<%=session.getAttribute("username") %><br>
<a href="session_page2.jsp" target="_blank">跳转到page2</a>
</body>
</html>
定义一个page2
<%@ page language="java" import="java.util.*,java.text.*" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>session</title>
</head>
<body>
<h1>session对象</h1><hr>Session的ID编号:<%=session.getId() %><br>
从Session中获取用户名:<%=session.getAttribute("username") %><br>
Session中保存的属性:<%String[] name=session.getValueNames();for(int i=0;i<name.length;i++){out.println(name[i]+" "); }
%>
</body>
</html>
运行结果
过十秒