java web用監聽器listener簡單的實現線上統計人數 ...
今天又重新學習了java web的基礎 監聽器技術,用sessionListener簡單的實現了網頁統計線上人數 (有一個bug就是當關閉網頁的時候 session不會關閉依舊存在,除非關閉伺服器),代碼如下(前端網頁代碼就不顯示了):
LoginServlet.java:
1 import javax.servlet.ServletException; 2 import javax.servlet.annotation.WebServlet; 3 import javax.servlet.http.HttpServlet; 4 import javax.servlet.http.HttpServletRequest; 5 import javax.servlet.http.HttpServletResponse; 6 import javax.servlet.http.HttpSession; 7 import java.io.IOException; 8 9 @WebServlet(name = "LoginServlet",urlPatterns = "/login") 10 public class LoginServlet extends HttpServlet { 11 @Override 12 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 13 String username=request.getParameter("username"); 14 HttpSession session=request.getSession(); 15 session.setAttribute("username",username); 16 //重新定向到index.jsp 17 response.sendRedirect("index.jsp"); 18 } 19 20 @Override 21 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 22 23 } 24 }
ExitServlet.java:
1 package servlet; 2 3 import javax.servlet.ServletException; 4 import javax.servlet.annotation.WebServlet; 5 import javax.servlet.http.HttpServlet; 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8 import javax.servlet.http.HttpSession; 9 import java.io.IOException; 10 11 @WebServlet(name = "ExitServlet",urlPatterns = "/exit") 12 public class ExitServlet extends HttpServlet { 13 @Override 14 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 15 //從session中移除loginName參數 16 response.getWriter().write("2222"); 17 HttpSession session=request.getSession(); 18 session.removeAttribute("username"); 19 //重新定向到login.jsp 20 response.sendRedirect("login.jsp"); 21 } 22 23 @Override 24 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 25 doPost(request, response); 26 } 27 }
OnlineListener.java:
1 package listener; 2 3 import bean.User; 4 5 import javax.servlet.ServletContextEvent; 6 import javax.servlet.ServletContextListener; 7 import javax.servlet.http.HttpSessionAttributeListener; 8 import javax.servlet.http.HttpSessionEvent; 9 import javax.servlet.http.HttpSessionListener; 10 import javax.servlet.http.HttpSessionBindingEvent; 11 12 @javax.servlet.annotation.WebListener() 13 public class OnlineListener implements ServletContextListener, 14 HttpSessionListener, HttpSessionAttributeListener { 15 public OnlineListener() { 16 } 17 @Override 18 public void contextInitialized(ServletContextEvent sce) { 19 20 } 21 @Override 22 public void contextDestroyed(ServletContextEvent sce) { 23 24 } 25 @Override 26 public void sessionCreated(HttpSessionEvent se) { 27 System.out.println("會話已創建"); 28 } 29 @Override 30 public void sessionDestroyed(HttpSessionEvent se) { 31 System.out.println("會話已銷毀"); 32 } 33 34 @Override 35 public void attributeAdded(HttpSessionBindingEvent sbe) { 36 //向session範圍中添加一個用戶 37 User.addUser(String.valueOf(sbe.getValue())); 38 System.out.println("session增加屬性為"+sbe.getName()+"值為:"+sbe.getValue()); 39 } 40 @Override 41 public void attributeRemoved(HttpSessionBindingEvent sbe) { 42 //將session會話中的用戶移除 43 User.removeUser(String.valueOf(sbe.getValue())); 44 System.out.println(sbe.getValue()+"屬性已被移除"); 45 } 46 @Override 47 public void attributeReplaced(HttpSessionBindingEvent sbe) { 48 //獲取舊的屬性值 49 String oldValue=String.valueOf(sbe.getValue()); 50 //獲取新的屬性值 51 String newValue=String.valueOf(sbe.getSession().getAttribute(sbe.getName())); 52 //移除舊的屬性,添加新的屬性 53 User.removeUser(oldValue); 54 User.addUser(newValue); 55 } 56 }
用兩個不同的瀏覽器運行結果如下圖所示:
項目工程目錄如下圖:
(堅持學習!努力加油!勉勵自己)