标签:
本文研究的总结。欢迎转载,但请注明出处:http://blog.csdn.net/pistolove/article/details/44310967
import java.util.ArrayList; import javax.servlet.annotation.WebListener; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; import com.my.entity.User; import com.my.util.SessionUtil; /** * 计数监听器 */ @WebListener public class MyHttpSessionListener implements HttpSessionListener { private int userNumber = 0; @Override public void sessionCreated(HttpSessionEvent arg0) { userNumber++; arg0.getSession().getServletContext().setAttribute("userNumber", userNumber); } @Override public void sessionDestroyed(HttpSessionEvent arg0) { userNumber--; arg0.getSession().getServletContext().setAttribute("userNumber", userNumber); ArrayList<User> userList = null;//在线用户List userList = (ArrayList<User>)arg0.getSession().getServletContext().getAttribute("userList"); if(SessionUtil.getUserBySessionId(userList, arg0.getSession().getId())!=null){ userList.remove(SessionUtil.getUserBySessionId(userList, arg0.getSession().getId())); } } }
import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import javax.servlet.ServletRequestEvent; import javax.servlet.ServletRequestListener; import javax.servlet.annotation.WebListener; import javax.servlet.http.HttpServletRequest; import com.my.entity.User; import com.my.util.SessionUtil; @WebListener public class MyServletRequestListener implements ServletRequestListener { private ArrayList<User> userList;//在线用户List @Override public void requestDestroyed(ServletRequestEvent arg0) { } @Override public void requestInitialized(ServletRequestEvent arg0) { userList = (ArrayList<User>)arg0.getServletContext().getAttribute("userList"); if(userList==null) userList = new ArrayList<User>(); HttpServletRequest request = (HttpServletRequest) arg0.getServletRequest(); String sessionIdString = request.getSession().getId(); if(SessionUtil.getUserBySessionId(userList,sessionIdString)==null){ User user = new User(); user.setSessionIdString(sessionIdString); user.setFirstTimeString(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); user.setIpString(request.getRemoteAddr()); userList.add(user); } arg0.getServletContext().setAttribute("userList", userList); } }
import java.util.ArrayList; import com.my.entity.User; public class SessionUtil { public static Object getUserBySessionId(ArrayList<User> userList, String sessionIdString) { for (int i = 0; i < userList.size(); i++) { User user = userList.get(i); if (user.getSessionIdString().equals(sessionIdString)) { return user; } } return null; } }
public class User { private String sessionIdString; private String ipString; private String firstTimeString; public String getSessionIdString() { return sessionIdString; } public void setSessionIdString(String sessionIdString) { this.sessionIdString = sessionIdString; } public String getIpString() { return ipString; } public void setIpString(String ipString) { this.ipString = ipString; } public String getFirstTimeString() { return firstTimeString; } public void setFirstTimeString(String firstTimeString) { this.firstTimeString = firstTimeString; } }
import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.annotation.WebListener; @WebListener("This is My Servlet 3.0 Listener") public class MyServletContextListener implements ServletContextListener { @Override public void contextDestroyed(ServletContextEvent arg0) { System.out.println("contextDestroyed"); } @Override public void contextInitialized(ServletContextEvent arg0) { System.out.println("contextInitialized"); } }
版权声明:本文博主原创文章,博客,未经同意不得转载。
标签:
原文地址:http://www.cnblogs.com/mengfanrong/p/4841990.html