监听器统计网站在线人数与在线会员数

本文展示了如何使用JSP、EL表达式和session监听器实现在线用户管理,包括踢人功能及在线用户数量实时更新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

显示JSP界面中:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>显示界面</title>
</head>
<body>
<c:forEach var="m" items="${map}">
${m.key }
<a href="${pageContext.request.contextPath}/servlet/kick.do?username=${m.key}">踢人</a>
</c:forEach>
<br/>
在线总人数:${youke}<br/>
在线会员数:${huiyuan}
</body>
</html>


session监听器

package cn.csdn.web.listener;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionActivationListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import cn.csdn.web.domain.User;

public class SessionListener implements HttpSessionAttributeListener,HttpSessionListener {

public void attributeAdded(HttpSessionBindingEvent se) {
Object obj = se.getValue();
if(obj instanceof User){
HttpSession session = se.getSession();

ServletContext context = session.getServletContext();
Map map = (Map) context.getAttribute("map");
Integer huiyuan = (Integer) context.getAttribute("huiyuan");

if(map==null){
map = new HashMap();
context.setAttribute("map", map);
huiyuan=1;
context.setAttribute("huiyuan", huiyuan);
}else{
huiyuan++;
context.setAttribute("huiyuan", huiyuan);
}
User user = (User) obj;
map.put(user.getName(), session);
}
}

public void attributeRemoved(HttpSessionBindingEvent se) {

}
public void attributeReplaced(HttpSessionBindingEvent se) {

}
public void sessionCreated(HttpSessionEvent se) {
ServletContext context = se.getSession().getServletContext();
Integer youke = (Integer) context.getAttribute("youke");
if(youke==null){
youke=1;
context.setAttribute("youke", youke);
}else{
youke++;
context.setAttribute("youke", youke);
}
}

public void sessionDestroyed(HttpSessionEvent se) {
ServletContext context = se.getSession().getServletContext();
Integer huiyuan = (Integer) context.getAttribute("huiyuan");
Integer youke = (Integer) context.getAttribute("youke");
if(huiyuan!=null){
huiyuan--;
}
youke--;
context.setAttribute("huiyuan", huiyuan);
context.setAttribute("youke", youke);
}
}


附加踢人功能:

在servlet中:

package cn.csdn.web.servlet;


import java.io.IOException;
import java.util.Map;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


public class Kick extends HttpServlet {


/**
* @author sword
*/
private static final long serialVersionUID = 1L;


public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = request.getParameter("username");
Map<?, ?> map = (Map<?, ?>) this.getServletContext().getAttribute("map");
//System.out.println("---------------");
HttpSession session = (HttpSession) map.get("username");

if(session!=null){
session.invalidate();
map.remove(username);
}

request.getRequestDispatcher("/listUser.jsp").forward(request, response);
}


public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}


}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值