通过设置内建对象session对象的过期时间,使用户提交的信息保存300秒,超时后让用户重新登录。保存到session中,并在有效期限内刷新页面,显示用户的提交信息,如果超过预设时间则弹出对话框,确认后返回提交信息页面
5_2_1.jsp
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37<%@ 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
复制代码
1
2
3
4
5
6
7
8<% 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
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69<%@ 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>
实现效果:
最后
以上就是健康咖啡豆最近收集整理的关于jsp之内置对象session过期时间的全部内容,更多相关jsp之内置对象session过期时间内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复