Copyright: [Beijing] Java Youth: 456588754 https://blog.csdn.net/Amen_Wu/article/details/53510836
First, // Configure listener in web.xml
<listener>
<listener-class>cn.news.lisener.UserLisener</listener-class>
</listener>
Second, a write packet UserListener class at com.wu.listener
HttpSessionBindingListener Interface
valueBound(HttpSessionBindingEvent event)
When the object is added to the session, the method invoked by the container object is notified, no return value
valueUnbound(HttpSessionBindingEvent event)
When the object is removed from the session, the method invoked by the container object is notified, no return value
public class UserListener implements HttpSessionBindingListener {
//session生效
public void valueBound(HttpSessionBindingEvent arg0) {
Statistics.USER_ONLINE_STATISTICS++;
}
public void valueUnbound(HttpSessionBindingEvent arg0) {
Statistics.USER_ONLINE_STATISTICS--;
}
}
// in UserServlet
UserListener ul = new UserListener();
if (user != null) {
// 如果正确,保存用户的状态信息
if (user.getPwd().equals(upwd)) {
System.out.println("登录成功");
HttpSession session = request.getSession();
session.setAttribute("loginuser", user.getUname());
session.setAttribute("isadmin", user.getIsadmin());
session.setAttribute("ul", ul);
response.sendRedirect(request.getContextPath() + "/index.jsp");