Java基于servlet監(jiān)聽器實(shí)現(xiàn)在線人數(shù)監(jiān)控功能的方法
本文實(shí)例講述了Java基于servlet監(jiān)聽器實(shí)現(xiàn)在線人數(shù)監(jiān)控功能的方法。分享給大家供大家參考,具體如下:
1、分析:
做一個網(wǎng)站在線人數(shù)統(tǒng)計(jì),可以通過ServletContextListener監(jiān)聽,當(dāng)Web應(yīng)用上下文啟動時,在ServletContext中添加一個List.用來準(zhǔn)備存放在線的用戶名,然后通過HttpSessionAttributeListener監(jiān)聽,當(dāng)用戶登錄成功,把用戶名設(shè)置到Session中。同時將用戶名方法到ServletContext的List中,最后通過HttpSessionListener監(jiān)聽,當(dāng)用戶注銷會話時,講用戶名從應(yīng)用上下文范圍中的List列表中刪除。
2、注意事項(xiàng)
測試時,需要啟動不同的瀏覽器來登陸不同的用戶,只有點(diǎn)擊注銷按鈕才能減少在線用戶,關(guān)閉瀏覽器不能減少在線用戶。
3、項(xiàng)目源代碼
(1)java代碼
OnlineListener類
package com.smalle.listener;
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;
public class OnlineListener implements
ServletContextListener, HttpSessionAttributeListener, HttpSessionListener {
private ServletContext application = null;
//應(yīng)用上下文初始時會回調(diào)的方法
@Override
public void contextInitialized(ServletContextEvent e) {
//初始化一個application對象
application = e.getServletContext();
//設(shè)置一個列表屬性,用于保存在線用戶名
this.application.setAttribute("online", new LinkedList<String>());
}
//往會話中添加屬性時的回調(diào)方法
@Override
public void attributeAdded(HttpSessionBindingEvent e) {
//取得用戶名列表
List<String> onlines = (List<String>) this.application.getAttribute("online");
if("username".equals(e.getName())){
onlines.add((String) e.getValue());
}
//將添加后的列表重新設(shè)置列application屬性中.
this.application.setAttribute("online", onlines);
}
//會話銷毀時會回調(diào)的方法
@Override
public void sessionDestroyed(HttpSessionEvent e) {
//取得用戶名列表
List<String> onlines = (List<String>) this.application.getAttribute("online");
//取得當(dāng)前用戶名
String username = (String) e.getSession().getAttribute("username");
//將此用戶從列表中刪除
onlines.remove(username);
//講刪除后的列表重新設(shè)置到application屬性中.
this.application.setAttribute("online", onlines);
}
public void sessionCreated(HttpSessionEvent e) {}
public void attributeRemoved(HttpSessionBindingEvent e) {}
public void attributeReplaced(HttpSessionBindingEvent e) {}
public void contextDestroyed(ServletContextEvent e) {}
}
LoginServlet類
package com.smalle.listener;
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;
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
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"); //獲取請求參數(shù)中的用戶名
//往session中添加屬性,會觸發(fā)HttpSessionAttributeListener中的attributeAdded方法
if(username != null && !username.equals("")) {
request.getSession().setAttribute("username",username);
}
//從應(yīng)用上下文中獲取在線用戶名列表
List<String> online = (List<String>)getServletContext().getAttribute("online");
System.out.println("LoginServlet" + online);
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("");
out.println(" <title>用戶列表</title>");
out.println(" ");
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));
}
//注意: 要對鏈接URL進(jìn)行自動重寫處理
out.println("<hr/><a href=\"" + response.encodeURL("logoutListener") + "\">注銷</a>");
out.println(" ");
out.println("");
out.flush();
out.close();
}
}
LogoutServlet類
package com.smalle.listener;
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;
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"); //設(shè)置響應(yīng)內(nèi)容類型
//銷毀會話,會觸發(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("");
out.println(" <title>用戶列表</title>");
out.println(" ");
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='\'index.html\''>主頁</a>");
out.println(" ");
out.println("");
out.flush();
out.close();
}
}
(2)web.xml代碼
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>testServlet</display-name>
<listener>
<listener-class>com.smalle.listener.OnlineListener</listener-class>
</listener>
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.smalle.listener.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/loginListener</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>LogoutServlet</servlet-name>
<servlet-class>com.smalle.listener.LogoutServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LogoutServlet</servlet-name>
<url-pattern>/logoutListener</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
(3)表現(xiàn)層代碼
<!DOCTYPE html>
<html>
<head>
<title>index.html</title>
<meta name="content-type" content="text/html; charset=UTF-8">
</head>
<body>
<form action="loginListener" method="post">
用戶名:<input type="text" name="username">
<input type="submit" value="登錄"><br><br>
</form>
</body>
</html>
更多關(guān)于java算法相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java網(wǎng)絡(luò)編程技巧總結(jié)》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設(shè)計(jì)有所幫助。
- 聊聊java 過濾器、監(jiān)聽器、攔截器的區(qū)別(終結(jié)篇)
- Java監(jiān)聽器三種實(shí)現(xiàn)方法代碼解析
- Java Web監(jiān)聽器Listener接口原理及用法實(shí)例
- java中接口和事件監(jiān)聽器的深入理解
- Javaweb監(jiān)聽器實(shí)例之統(tǒng)計(jì)在線人數(shù)
- java-RGB調(diào)色面板的實(shí)現(xiàn)(事件監(jiān)聽器之匿名內(nèi)部類)
- Java使用自定義注解實(shí)現(xiàn)為事件源綁定事件監(jiān)聽器操作示例
- Java設(shè)計(jì)模式之監(jiān)聽器模式實(shí)例詳解
- 基于java servlet過濾器和監(jiān)聽器(詳解)
- 如何實(shí)現(xiàn)Java監(jiān)聽器詳解
相關(guān)文章
解決idea中maven新增的配置文件xx.xml沒生效問題
這篇文章主要介紹了如何解決idea中maven新增的配置文件xx.xml沒生效問題,公司項(xiàng)目有用自己的`私服,Maven正常去私服下載jar包是沒問題的,但阿里云鏡像找不到相關(guān)的jar包報(bào)錯,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2024-06-06
SpringBoot自定義內(nèi)容協(xié)商的實(shí)現(xiàn)
在Spring Boot中,內(nèi)容協(xié)商是一種機(jī)制,它允許服務(wù)器根據(jù)客戶端的請求選擇返回不同的表示形式,本文就來詳細(xì)的介紹一下SpringBoot自定義內(nèi)容協(xié)商的實(shí)現(xiàn),感興趣的可以了解一下2024-09-09
如何解決Spring in action @valid驗(yàn)證不生效的問題
這篇文章主要介紹了如何解決Spring in action @valid驗(yàn)證不生效的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06
SpringBoot+Vue實(shí)現(xiàn)EasyPOI導(dǎo)入導(dǎo)出的方法詳解
項(xiàng)目開發(fā)過程中,很大的需求都有 導(dǎo)入導(dǎo)出功能。本文將利用SpringBoot+Vue實(shí)現(xiàn)EasyPOI導(dǎo)入導(dǎo)出功能,感興趣的可以了解一下2022-08-08
Java基礎(chǔ)之多線程的三種實(shí)現(xiàn)方式
這篇文章主要介紹了Java基礎(chǔ)之多線程的三種實(shí)現(xiàn)方式,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)java基礎(chǔ)的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04
JAVA實(shí)現(xiàn)的CrazyArcade泡泡堂游戲
CrazyArcade泡泡堂游戲,一款用Java編寫的JavaSwing游戲程序。 使用了MVC模式,分離了模型、視圖和控制器,使得項(xiàng)目結(jié)構(gòu)清晰易于擴(kuò)展,使用配置文件來設(shè)置游戲基本配置,擴(kuò)展地圖人物道具等。同時,該程序編寫期間用了單例模式、工廠模式、模板模式等設(shè)計(jì)模式。2021-04-04

