java應用監聽器完成一個統計網站在耳目數的示例。本站提示廣大學習愛好者:(java應用監聽器完成一個統計網站在耳目數的示例)文章只能為提供參考,不一定能成為您想要的結果。以下是java應用監聽器完成一個統計網站在耳目數的示例正文
本文重要引見了java應用監聽器完成一個統計網站在耳目數的示例,具有必定的參考價值,有須要的同伙可以懂得一下。
(1)創立一個監聽器完成類
要年夜致統計一個網站的在耳目數,起首,可以經由過程ServletContextListener監聽,當Web運用高低文啟動時,在ServletContext中添加一個List,用來預備寄存在線的用戶名;然後,可以經由過程HttpSessionAttributeListener監聽,當用戶登錄勝利把用戶名設置到Session中時同時將用戶名寄存到ServletContext中的List列表中;最初經由過程HttpSessionListener監聽,當用戶刊出會話時將用戶名從運用高低文規模中的List列表中刪除。
所以,編寫OnLineListener類完成ServletContextListener、HttpSessionAttributeListener、HttpSessionListener接口,詳細代碼以下:
package com.web.servlet; import <a href="http://lib.csdn.net/base/javaee" class='replace_word' title="Java EE常識庫" target='_blank' style='color:#df3434; font-weight:bold;'>Java</a>.util.LinkedList; import java.util.List; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.http.HttpSessionAttributeListener; import javax.servlet.http.HttpSessionBindingEvent; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; //在耳目數統計監聽器完成類 public class OnlineListener implements ServletContextListener, HttpSessionAttributeListener, HttpSessionListener { private ServletContext application = null; public void contextDestroyed(ServletContextEvent arg0) { // TODO Auto-generated method stub } public void contextInitialized(ServletContextEvent arg0) { //初始化一個application對象 this.application = arg0.getServletContext(); //設置一個列表屬性,用於保留在想用戶名 this.application.setAttribute("online", new LinkedList<String>()); } //往會話中添加屬性時會回調的辦法 public void attributeAdded(HttpSessionBindingEvent arg0) { //獲得用戶名列表 List<String> online = (List<String>) this.application .getAttribute("online"); if ("username".equals(arg0.getName())) { //將以後用戶名添加到列表中 online.add((String) arg0.getValue()); } //將添加後的列表從新設置到application屬性中 this.application.setAttribute("online", online); } public void attributeRemoved(HttpSessionBindingEvent arg0) { // TODO Auto-generated method stub } public void attributeWordStrd(HttpSessionBindingEvent arg0) { // TODO Auto-generated method stub } public void sessionCreated(HttpSessionEvent arg0) { // TODO Auto-generated method stub } //會話燒毀時會回調的辦法 public void sessionDestroyed(HttpSessionEvent arg0) { //獲得用戶名列表 List<String> online = (List<String>) this.application .getAttribute("online"); //獲得以後用戶名 String username = (String) arg0.getSession().getAttribute("username"); //將此用戶名從列表中刪除 online.remove(username); //將刪除後的列表從新設置到application屬性中 this.application.setAttribute("online", online); } }
(2)在web.xml中注冊監聽器
監聽器完成好後,還須要在web.xml文件中停止注冊能力起感化,只須要在web.xml中像以下添加元素便可
<!-- 注冊一個監聽器 --> <listener> <!-- 指定監聽器完成類的全限制名 --> <listener-class> com.web.servlet.OnlineListener </listener-class> </listener
最初,我們創立幾個Servlet來測試這個監聽器完成的功效。
處置用戶登錄的Servlet類代碼:
package com.web.servlet; import java.io.IOException; import java.io.PrintWriter; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; //處置用戶登錄的Servlet public class LoginServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8");//設置響應內容類型 String username= request.getParameter("username");//獲得要求參數中的用戶名 //往session中添加屬性,會觸發HttpSessionAttributeListener中的attributeAdded辦法 if(username != null && !username.equals("")) { request.getSession().setAttribute("username",username); } //從運用高低文中獲得在線用戶名列表 List<String> online = (List<String>)getServletContext().getAttribute("online"); response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); out.println("<HTML>"); out.println(" <HEAD><TITLE>用戶列表</TITLE></HEAD>"); out.println(" <BODY>"); out.println("以後用戶是:" + username); out.print(" <hr/><h3>在線用戶列表</h3>"); int size = online == null ? 0 : online.size(); for (int i = 0; i < size; i++) { if(i > 0){ out.println("<br/>"); } out.println(i + 1 + "." + online.get(i)); } //留意: 要對鏈接URL停止主動重寫處置 out.println("<hr/><a href="/" mce_href="/""" + response.encodeURL("logout") + "/">刊出</a>"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } }
處置用戶登錄Servlet的類代碼
package com.web.servlet; import java.io.*; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.*; //處置用戶刊出會話的Servlet public class LogoutServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); //燒毀會話,會觸發SessionLinstener中的sessionDestroyed辦法 request.getSession().invalidate(); //從運用高低文中獲得在線用戶名列表 List<String> online = (List<String>)getServletContext().getAttribute("online"); response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); out.println("<HTML>"); out.println(" <HEAD><TITLE>用戶列表</TITLE></HEAD>"); out.println(" <BODY>"); out.print(" <h3>在線用戶列表</h3>"); int size = online == null ? 0 : online.size(); for (int i = 0; i < size; i++) { if(i > 0){ out.println("<br/>"); } out.println(i + 1 + "." + online.get(i)); } out.println("<hr/><a href="/" mce_href="/""index.html/">主頁</a>"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } }
然後創立一個index.html文件,用來供用戶登錄,代碼以下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>index.html</title> </head> <body> <form action = "login" method = "post"> 用戶名:<input type ="text" name = "username"/> <input type = "submit" value = "登錄"/><br/><br/> </form> </body> </html>
把WEB安排到Tomcat容器總,並啟動。翻開閱讀器拜訪便可
以上就是本文的全體內容,願望對年夜家的進修有所贊助,也願望年夜家多多支撐。