cookie实现自动登录功能
2022-09-17 20:02:30 # javaweb # cookie

使用cookie实现用户自动登录功能

login.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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<%--
Created by IntelliJ IDEA.
User: DELL
Date: 2022/9/17
Time: 20:12
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<style>
* {
padding: 0;
margin: 0;
}
.main {
padding-top: 20px;
margin: 60px auto;
width: 18%;
height: 210px;
border-radius: 5%;
}
.man {
width: 100%;
height: 100px;
}
.man .top,
.down {
position: relative;
width: 100%;
height: 30%;
}
.main .top .l,
.down .l {
position: absolute;
left: 20px;
}
.main .top .r,
.down .r {
position: absolute;
right: 10px;
}
.main .down {
margin-top: 15px;
}
.mess {
width: 100%;
height: 50px;
text-align: center;
}
.mess .top {
width: 100%;
height: 50%;
}
.mess .down {
margin-top: 0;
width: 100%;
height: 50%;
}
.submit {
margin-top: 20px;
text-align: center;
}
.submit button {
margin: auto;
display: inline-block;
width: 100px;
height: 30px;
}
</style>
</head>
<body>
<%-- 如果cookie存在并且存储着用户的登录信息,则直接登录跳转页面--%>
<%
Cookie c[] = request.getCookies();
if(c!=null){
for(int i=0;i<c.length;i++){
if(c[i].getName().equals("user")){
String user = c[i].getValue();
session.setAttribute("user",user);
response.sendRedirect("zy.jsp");
return;
}
}
}
%>
<form action="dealLogin.jsp" method="post" class="main">
<div class="man">
<div class="top">
<div class="l">用户名</div>
<div class="r"><input type="text" name="user"></div>
</div>
<div class="down">
<div class="l">密码</div>
<div class="r"><input type="password" name="passwd"></div>
</div>
</div>
<div class="mess">
<div class="top">
不保存用户名
<input type="radio" name="timer" value="0">
</div>
<div class="down">
<input type="radio" name="timer" value="5">5
<input type="radio" name="timer" value="10">10
<input type="radio" name="timer" value="20">20
</div>
</div>
<div class="submit">
<button type="submit">提交</button>
</div>
</form>
</body>
</html>

dealLogin.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
<%--
Created by IntelliJ IDEA.
User: DELL
Date: 2022/9/17
Time: 20:48
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
String user = request.getParameter("user");
String passwd = request.getParameter("passwd");
if(user.equals("kittates")&&passwd.equals("kittates")&&user!=null&&passwd!=null){
session.setAttribute("user",user);
session.setAttribute("passwd",passwd);
String timer = request.getParameter("timer");
if(timer!=null){
int num = Integer.parseInt(timer);
Cookie c = new Cookie("user",user);
c.setMaxAge(num);
response.addCookie(c);
// if(num>0){
// Cookie c = new Cookie("user",user);
// c.setMaxAge(num);
// response.addCookie(c);
// }
}
response.sendRedirect("zy.jsp");
}
else {
response.sendRedirect("login_cookie.jsp");
}
%>
</body>
</html>

zy.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
<%--
Created by IntelliJ IDEA.
User: DELL
Date: 2022/9/17
Time: 20:58
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
String user = (String)session.getAttribute("user");
if(user!=null){
out.println("<h3>welcome </h3>"+user);
}
else {
response.sendRedirect("login.jsp");
}
%>
</body>
</html>

总结

很带劲😢