MyHttpSessionListener
统计在线人的信息
package cn.hncu.listeners; import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; public class MyHttpSessionListener implements HttpSessionListener { //有 游客/用户 来访问了 @Override public void sessionCreated(HttpSessionEvent se) { List<HttpSession> guests = (List<HttpSession>) se.getSession().getServletContext().getAttribute("guests");//在线人的集合 if(guests==null){//第一个访问网站的人--沙发 guests = new ArrayList<HttpSession>(); se.getSession().getServletContext().setAttribute("guests", guests);//设置guests属性 } guests.add(se.getSession());//将第一个用户的session添加到在线人集合 } @Override public void sessionDestroyed(HttpSessionEvent se) { List<HttpSession> guests = (List<HttpSession>) se.getSession().getServletContext().getAttribute("fuses"); if(guests.contains(se.getSession())){ guests.remove(se.getSession()); } } }
MyHttpSessionAttributeListener
统计在线用户的信息:
package cn.hncu.listeners; import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSessionAttributeListener; import javax.servlet.http.HttpSessionBindingEvent; public class MyHttpSessionAttributeListener implements HttpSessionAttributeListener{ //session添加属性了,就会调用下面的attributeAdded方法 @Override public void attributeAdded(HttpSessionBindingEvent se) { if(se.getName().equals("name")){//代表添加了name属性 System.out.println("用户"+se.getValue()+"登录了"); List<HttpSession> logins = (List<HttpSession>) se.getSession().getServletContext().getAttribute("logins"); if(logins==null){ logins = new ArrayList<HttpSession>(); se.getSession().getServletContext().setAttribute("logins", logins); } logins.add(se.getSession()); } } @Override public void attributeRemoved(HttpSessionBindingEvent se) { } @Override public void attributeReplaced(HttpSessionBindingEvent se) { } }
web.xml:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name></display-name> <filter> <filter-name>charset</filter-name> <filter-class>cn.hncu.filter.CharsetFilter</filter-class> </filter> <filter-mapping> <filter-name>charset</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <listener> <listener-class>cn.hncu.listeners.MyServletContextListener</listener-class> </listener> <listener> <listener-class>cn.hncu.listeners.MyServletRequestListenr</listener-class> </listener> <listener> <listener-class>cn.hncu.listeners.MyHttpSessionAttributeListener</listener-class> </listener> <listener> <listener-class>cn.hncu.listeners.MyHttpSessionListener</listener-class> </listener> <servlet> <servlet-name>LoginServlet</servlet-name> <servlet-class>cn.hncu.servlets.LoginServlet</servlet-class> </servlet> <servlet> <servlet-name>ShowServlet</servlet-name> <servlet-class>cn.hncu.servlets.ShowServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>LoginServlet</servlet-name> <url-pattern>/servlet/LoginServlet</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>ShowServlet</servlet-name> <url-pattern>/servlet/ShowServlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
演示结果:
完整源码链接:
https://github.com/chenhaoxiang/Java/tree/master/myOnlinesWeb
myOnlinesWeb.zip文件。