-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhome.jsp
More file actions
100 lines (82 loc) · 3.2 KB
/
home.jsp
File metadata and controls
100 lines (82 loc) · 3.2 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<%@ page import="java.util.Date" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.net.URLEncoder" %>
<%@ page import="java.net.URLDecoder" %>
<%--
Created by IntelliJ IDEA.
User: cpucode
Date: 2020/11/7
Time: 22:30
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>响应时间</title>
</head>
<body>
<%
//1.获取所有Cookie
Cookie[] cookies = request.getCookies();
//没有cookie为lastTime
boolean flag = false;
//2.遍历cookie数组
if (cookies != null && cookies.length > 0){
for (Cookie cookie : cookies){
//3.获取cookie的名称
String name = cookie.getName();
//4.判断名称是否是:lastTime
if ("lastTime".equals(name)){
//没有cookie为lastTime
flag = true; //有lastTime的cookie
//设置Cookie的value
//获取当前时间的字符串,重新设置Cookie的值,重新发送cookie
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String str_date = sdf.format(date);
System.out.println("解码前 :" + str_date);
//URL编码
str_date = URLEncoder.encode(str_date, "utf-8");
System.out.println("编码后 :" + str_date);
cookie.setValue(str_date);
//设置cookie的存活时间
cookie.setMaxAge(60 * 60 * 24 * 30); //一个月
response.addCookie(cookie);
//响应数据
//获取Cookie的value,时间
String value = cookie.getValue();
System.out.println("解码前:" + value);
//URL解码:
value = URLDecoder.decode(value, "utf-8");
System.out.println("解码后:" + value);
%>
<h1>欢迎回来,您上次访问时间为:<%=value%></h1>
<input>
<%
break;
}
}
}
if (cookies == null || cookies.length == 0 || flag == false){
//没有,第一次访问
//设置Cookie的value
//获取当前时间的字符串,重新设置Cookie的值,重新发送cookie
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String str_date = sdf.format(date);
System.out.println("编码前 :" + str_date);
//URL编码
str_date = URLEncoder.encode(str_date, "utf-8");
System.out.println("编码后: " + str_date);
Cookie cookie = new Cookie("lastTime", str_date);
//设置cookie的存活时间
cookie.setMaxAge(60 * 60 * 24 * 30); //一个月
response.addCookie(cookie);
%>
<h1>您好,欢迎您首次访问</h1>
<span></span>
<%
}
%>
</body>
</html>