通過設置內建對象session對象的過期時間,使用戶提交的信息保存300秒,超時後讓用戶重新登錄。保存到session中,並在有效期限內刷新頁面,顯示用戶的提交信息,如果超過預設時間則彈出對話框,確認後返回提交信息頁面
5_2_1.jsp
<%@ page language="java" import="java.util.*" pageEncoding="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 '5_2_1.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>
<form action="5_2_3.jsp" method="post">
用戶名:<input type="text" name="name"><br/><br>
密碼 : <input type="password" name="password"><br><br>
<input type="submit" name="submit" value="提交">
<input type="reset" name="reset" value="取消">
</form>
</body>
</html>
5_2_3.jsp
<%
String name=request.getParameter("name");
String password = request.getParameter("password");
session.setAttribute("username", name);
session.setAttribute("userpassword", password);
response.sendRedirect("5_2_2.jsp");
%>
5_2_2.jsp
<%@ page import="java.text.SimpleDateFormat" language="java" import="java.util.*" pageEncoding="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 '5_2_2.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>
<%@page import="java.util.Date"%>
<%
String username=(String)session.getAttribute("username");
String userPassword=(String)session.getAttribute("userpassword");
out.print("用戶名:"+username+"<br>");
out.print("密碼:"+userPassword+"<br>");
if(session.isNew()){
//設置過期時間
session.setMaxInactiveInterval(1);
// session.setAttribute("expire", 10);
response.sendRedirect("5_2_1.jsp");
}
else{
Date d = new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String atime = df.format(session.getCreationTime());
String now = df.format(d);
out.print("session建立時間:"+atime+"<br>");
out.print("當前時間:"+now+"<br>");
//獲取session建立時間
long create_time = session.getCreationTime();
long access_time = session.getLastAccessedTime();
long current_time = System.currentTimeMillis();
//session存在時間
long exist_time = (current_time-create_time)/1000;
out.print("session已經存在"+exist_time+"秒");
response.setHeader("refresh", "1");
if(exist_time>=300){
out.println("session已過期");
session.invalidate();
%>
<script>
alert("session已過期");
</script>
<%
//response.sendRedirect("5_2_1.jsp");
}
}
%>
</body>
</html>
實現效果: