15-Listener统计在线人数

Listener 过滤器实现统计在线人数(流程图+核心逻辑)

流程图:
在这里插入图片描述

核心逻辑:
login.html

1
2
3
4
5
<form action="/demo/login" method="post">
账户:<input type="text" name="username" id=""> <br>
密码:<input type="password" name="password" id=""> <br>
<input type="submit" value="登陆">
</form>

User.java

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
/**
* 事件源:Java对象
* 监听器:HttpSessionBindingListener
* 绑定:java对象实现监听器接口
* 事件:java对象在Session中的状态发生改变
*/
public class User implements HttpSessionBindingListener {
@Override
public void valueBound(HttpSessionBindingEvent event) {
System.out.println("HttpSession 与 User 绑定");
// 有用户登陆成功
// 判断是否是第一个登陆的人
ServletContext servletContext = event.getSession().getServletContext();
Integer count = (Integer) servletContext.getAttribute("count");
// 第一个登陆为1,非第一个则++
count = null == count ? 1 : (count += 1);
servletContext.setAttribute("count", count);
}

@Override
public void valueUnbound(HttpSessionBindingEvent event) {
System.out.println("HttpSession 与 User 解绑");
// 有用户注销登陆
ServletContext servletContext = event.getSession().getServletContext();
Integer count = (Integer) servletContext.getAttribute("count");
// count--是因为在同浏览器下重复登陆时session.setAttribute(name, value)
// 每次会覆盖value值进而触发监听器valueBound()的count++
count--;
servletContext.setAttribute("count", count);
}

private int id;
private String username;
private String password;

public User() {
}

public User(int id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}

LoginServlet.java

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
@WebServlet(name = "LoginServlet", urlPatterns = "/login")
public class LoginServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
if ("root".equals(username) && "1234".equals(password)) {
// 登陆成功,修改登陆状态,转发到showIndex页
User user = new User();
user.setUsername(username);
user.setPassword(password);
request.getSession().setAttribute("existUser", user);
// 转发会导致每次刷新页面都会重新登陆,然后被统计新新用户中
//request.getRequestDispatcher("/showIndex").forward(request, response);
response.sendRedirect("/demo/showIndex");
} else {
// 登陆失败,转发到login.html
request.getRequestDispatcher("/demo/login.html").forward(request, response);
}
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}

LogoutServlet.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@WebServlet(name = "LogoutServlet", urlPatterns = "/logout")
public class LogoutServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 注销登陆:将existUser从session域中移除 或 销毁session
//request.getSession().removeAttribute("existUser");
request.getSession().invalidate();
// 注销成功
request.getRequestDispatcher("/showIndex").forward(request, response);
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}

ShowIndexServlet.java

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
@WebServlet(name = "ShowIndexServlet", urlPatterns = "/showIndex")
public class ShowIndexServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
User existUser = (User) request.getSession().getAttribute("existUser");
StringBuffer responseBody = new StringBuffer();
if (null == existUser) {
// 不在登陆状态
responseBody.append("您还没有登陆,<a href='/demo/login.html'>请登录</a>");
} else {
// 在登陆状态
responseBody.append("欢迎回来,").append(existUser.getUsername()).append(" <a href='/demo/logout'>注销</a>");
}

// 获取在线人数
ServletContext servletContext = getServletContext();
Integer count = (Integer) servletContext.getAttribute("count");
System.out.println("在线人数:" + count);
if (null == count) {
// 没有人登陆时,0人
count = 0;
}
responseBody.append("<br>在线人数为:").append(count).append("人");

response.setContentType("text/html;charset=utf-8");
response.getWriter().write(responseBody.toString());
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}

15-Listener统计在线人数
https://janycode.github.io/2017/05/22/04_网页技术/04_Servlet/15-Listener统计在线人数/
作者
Jerry(姜源)
发布于
2017年5月22日
许可协议