本文主要介紹了java使用監(jiān)聽(tīng)器實(shí)現(xiàn)一個(gè)統(tǒng)計(jì)網(wǎng)站在線人數(shù)的示例,具有一定的參考價(jià)值,有需要的朋友可以了解一下。
(1)創(chuàng)建一個(gè)監(jiān)聽(tīng)器實(shí)現(xiàn)類
要大致統(tǒng)計(jì)一個(gè)網(wǎng)站的在線人數(shù),首先,可以通過(guò)ServletContextListener監(jiān)聽(tīng),當(dāng)Web應(yīng)用上下文啟動(dòng)時(shí),在ServletContext中添加一個(gè)List,用來(lái)準(zhǔn)備存放在線的用戶名;然后,可以通過(guò)HttpSessionAttributeListener監(jiān)聽(tīng),當(dāng)用戶登錄成功把用戶名設(shè)置到Session中時(shí)同時(shí)將用戶名存放到ServletContext中的List列表中;最后通過(guò)HttpSessionListener監(jiān)聽(tīng),當(dāng)用戶注銷會(huì)話時(shí)將用戶名從應(yīng)用上下文范圍中的List列表中刪除。
所以,編寫(xiě)OnLineListener類實(shí)現(xiàn)ServletContextListener、HttpSessionAttributeListener、HttpSessionListener接口,具體代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
package com.web.servlet; import Java.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; //在線人數(shù)統(tǒng)計(jì)監(jiān)聽(tīng)器實(shí)現(xiàn)類 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) { //初始化一個(gè)application對(duì)象 this .application = arg0.getServletContext(); //設(shè)置一個(gè)列表屬性,用于保存在想用戶名 this .application.setAttribute( "online" , new LinkedList<String>()); } //往會(huì)話中添加屬性時(shí)會(huì)回調(diào)的方法 public void attributeAdded(HttpSessionBindingEvent arg0) { //取得用戶名列表 List<String> online = (List<String>) this .application .getAttribute( "online" ); if ( "username" .equals(arg0.getName())) { //將當(dāng)前用戶名添加到列表中 online.add((String) arg0.getValue()); } //將添加后的列表重新設(shè)置到application屬性中 this .application.setAttribute( "online" , online); } public void attributeRemoved(HttpSessionBindingEvent arg0) { // TODO Auto-generated method stub } public void attributeReplaced(HttpSessionBindingEvent arg0) { // TODO Auto-generated method stub } public void sessionCreated(HttpSessionEvent arg0) { // TODO Auto-generated method stub } //會(huì)話銷毀時(shí)會(huì)回調(diào)的方法 public void sessionDestroyed(HttpSessionEvent arg0) { //取得用戶名列表 List<String> online = (List<String>) this .application .getAttribute( "online" ); //取得當(dāng)前用戶名 String username = (String) arg0.getSession().getAttribute( "username" ); //將此用戶名從列表中刪除 online.remove(username); //將刪除后的列表重新設(shè)置到application屬性中 this .application.setAttribute( "online" , online); } } |
(2)在web.xml中注冊(cè)監(jiān)聽(tīng)器
監(jiān)聽(tīng)器實(shí)現(xiàn)好后,還需要在web.xml文件中進(jìn)行注冊(cè)才能起作用,只需要在web.xml中像如下添加元素即可
1
2
3
4
5
6
7
|
<!-- 注冊(cè)一個(gè)監(jiān)聽(tīng)器 --> < listener > <!-- 指定監(jiān)聽(tīng)器實(shí)現(xiàn)類的全限定名 --> < listener-class > com.web.servlet.OnlineListener </ listener-class > </listener |
最后,我們創(chuàng)建幾個(gè)Servlet來(lái)測(cè)試這個(gè)監(jiān)聽(tīng)器實(shí)現(xiàn)的功能。
處理用戶登錄的Servlet類代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
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" ); //設(shè)置相應(yīng)內(nèi)容類型 String username= request.getParameter( "username" ); //獲取請(qǐng)求參數(shù)中的用戶名 //往session中添加屬性,會(huì)觸發(fā)HttpSessionAttributeListener中的attributeAdded方法 if (username != null && !username.equals( "" )) { request.getSession().setAttribute( "username" ,username); } //從應(yīng)用上下文中獲取在線用戶名列表 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( "當(dāng)前用戶是:" + 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)); } //注意: 要對(duì)鏈接URL進(jìn)行自動(dòng)重寫(xiě)處理 out.println( "<hr/><a href=" / " mce_href=" / "" " + response.encodeURL(" logout ") + " / ">注銷</a>" ); out.println( " </BODY>" ); out.println( "</HTML>" ); out.flush(); out.close(); } } |
處理用戶登錄Servlet的類代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
package com.web.servlet; import java.io.*; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.*; //處理用戶注銷會(huì)話的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" ); //銷毀會(huì)話,會(huì)觸發(fā)SessionLinstener中的sessionDestroyed方法 request.getSession().invalidate(); //從應(yīng)用上下文中獲取在線用戶名列表 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/ ">主頁(yè)</a>" ); out.println( " </BODY>" ); out.println( "</HTML>" ); out.flush(); out.close(); } } |
然后創(chuàng)建一個(gè)index.html文件,用來(lái)供用戶登錄,代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<!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容器總,并啟動(dòng)。打開(kāi)瀏覽器訪問(wèn)即可
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。