session实现用户登录
2022-09-17 16:20:04 # javaweb

使用session对象实现用户的登录验证功能,对于没有登录的用户不允许其访问网站页面,登录成功的用户信息将保存到session中,供success.jsp使用;如果直接通过url地址访问success,.jsp,则会因为session为null而重定向的login.html中。

login.html

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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
* {
padding: 0;
margin: 0;
}
.title {
font-size: 30px;
font-weight: 700;
text-align: center;
}
.body {
height: 110px;
width: 400px;
margin: 50px auto;
text-align: center;
}
.user,
.passwd {
margin: auto;
width: 60%;
height: 35px;
}
.user .left,
.passwd .left {
float: left;
margin-right: 10px;
}
.user .right,
.passwd .right{
float: left;
}
.foot {
position: relative;
margin: auto;
width: 100%;
height: 20%;
}
.foot .sub {
position: absolute;
left: 125px;
}
.foot .reset {
position: absolute;
right: 115px;
}
.sub button,
.reset button {
width: 70px;
height: 40px;
}
</style>
</head>
<body>
<form action="login.jsp" method="post" class="main">
<div class="title">用户登录</div>
<div class="body">
<div class="user">
<div class="left">用户:</div>
<div class="right">
<input type="text" name="user">
</div>
</div>
<div class="passwd">
<div class="left">密码:</div>
<div class="right">
<input type="password" name="passwd">
</div>
</div>
<div class="foot">
<div class="sub">
<button type="submit" name="submit">登录</button>
</div>
<div class="reset">
<button type="reset" name="reset">重置</button>
</div>
</div>
</div>
</form>

</body>
</html>

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
<%--
Created by IntelliJ IDEA.
User: DELL
Date: 2022/9/16
Time: 23:58
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html>
<head>
<title>Title</title>
<script>
window.onload=function(){
var t=4;
var span = document.querySelector("span");
var timer = setInterval(function(){
if(t===0) {
clearInterval(timer);
}
else {
span.innerHTML=t;
t--;
}

},1000)
}
</script>
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
String user = request.getParameter("user");
String passwd = request.getParameter("passwd");
if(user.length()==0 || passwd.length()==0){
out.println("<h3>请正确输入,<span>5</span>秒后返回登录<a href='login.html'>登录页面</a></h3>");
response.setHeader("Refresh","5;url=login.html");
}
else if(user.equals("kittates")&&passwd.equals("kittates") && user.length()!=0 &&passwd.length()!=0) {
session.setAttribute("loginUserName",user);
response.sendRedirect("success.jsp?name="+user);
}

else {
out.println("<h3>用户名或密码错误,<span>5</span>秒后返回登录<a href='login.html'>登录页面</a></h3>");
response.setHeader("Refresh","5;url=login.html");
}
%>
</body>
</html>

success.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
<%--
Created by IntelliJ IDEA.
User: DELL
Date: 2022/9/17
Time: 0:10
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<script>
window.onload=function(){
var t=4;
var span = document.querySelector("span");
var timer = setInterval(function(){
if(t===0) {
clearInterval(timer);
}
else {
span.innerHTML=t;
t--;
}

},1000)
}
</script>
</head>
<body>
<%
String loginUserName = (String)session.getAttribute("loginUserName");
if(loginUserName==null){
out.println("<h3>请先登录,<span>5</span>秒后返回登录<a href='login.html'>登录页面</a></h3>");
response.setHeader("Refresh","5;url=login.html");
}
else {
out.println("登录成功! 欢迎"+request.getParameter("name"));
}
%>
</body>
</html>

总结

总体来说还算可以,理解了session的使用方式,虽然和学的前端没有什么交集😢

此外配置了emoji😄,可以在页面上面使用emoji了👌