springboot实现在线用户统计

第三步骤:控制编写。

新建UserStatisticsController 类

1、后台模拟登录,返回当前登录人数,方法logUser。

2、后台模拟注销账号,返回当前登录人数,方法logOut。

3、session超时自动销毁,也会调用sessionDestroyed方法

4、具体代码如下所示

import com.example.excelimport.listener.UserStatisticsListener;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

@RestController

public class UserStatisticsController {

@RequestMapping(“/logUser”)

public String logUser(HttpServletRequest request, HttpServletResponse response, User user){

request.getSession().setAttribute(“name”,request.getParameter(“name”)); return request.getParameter(“name”)+“登录成功!当前在线用户人数是:”+ UserStatisticsListener.count;

}

@RequestMapping(“/logOut”)

public String logOut(HttpServletRequest request, HttpServletResponse response){

request.getSession().invalidate();

return request.getParameter(“name”)+“登出成功!当前在线用户人数是:”+UserStatisticsListener.count;

}

}

你可能感兴趣的:(java,java,后端)