uniapp开发app框架在提升开发效率中的独特优势与应用探索
706
2022-10-20
使用HttpSessionListener-实战
HttpSessionListener-
定义-
package lee;
import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.*;import java.util.*;
@WebListener
public class OnlineListener
implements HttpSessionListener
{
// 当用户与服务器之间开始session时触发该方法
public void sessionCreated(HttpSessionEvent se)
{
HttpSession session = se.getSession();
ServletContext application = session.getServletContext();
// 获取session ID
String sessionId = session.getId();
// 如果是一次新的会话
if (session.isNew())
{
String user = (String)session.getAttribute("user");
// 未登录用户当游客处理
user = (user == null) ? "游客" : user;
Map
application.getAttribute("online");
if (online == null)
{
online = new Hashtable
}
// 将用户在线信息放入Map中
online.put(sessionId , user);
application.setAttribute("online" , online);
}
}
// 当用户与服务器之间session断开时触发该方法
public void sessionDestroyed(HttpSessionEvent se)
{
HttpSession session = se.getSession();
ServletContext application = session.getServletContext();
String sessionId = session.getId();
Map
application.getAttribute("online");
if (online != null)
{
// 删除该用户的在线信息
online.remove(sessionId);
}
application.setAttribute("online" , online);
}
}
测试jsP
<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<%@ page import="java.util.*" %>
"http://w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
在线用户:
<%
Map
.getAttribute("online");
for (String sessionId : online.keySet())
{%>
<%}%>
测试结果
HttpSessionListener-应用场景
应用场景:用来统计当前在线人数
实现HttpSessionListener
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class MyHttpSessionListener implements HttpSessionListener {
@Override
public void sessionCreated(HttpSessionEvent httpSessionEvent) {
System.out.println("httpsession被创建");
}
@Override
public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
System.out.println("httpsession被销毁");
}
}
登陆界面去创建HttpSessionListenter
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
<%-- 创建HttpSessionListenter--%>
request.getSession();
%>
登出销毁HttpSessionListenter
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
<%-- 销毁HttpSessionListener--%>
request.getSession().invalidate();
%>
实现统计登陆人数(多线程并发)
web.xml中配置监听
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
统计人数实在最大ServletContextListener这个域当中
因为HttpSessionListener-只在当前会话中有效
(1)创建ServletContextListener-并设置初始值为0
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class myServletContextListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
ServletContext sc = servletContextEvent.getServletContext();
sc.setAttribute("count", 0);
}
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
}
}
2)变更在线人数
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class MyHttpSessionListener implements HttpSessionListener {
@Override
public void sessionCreated(HttpSessionEvent httpSessionEvent) {
System.out.println("httpsession被创建");
countPersion( httpSessionEvent.getSession().getServletContext(), true);
}
@Override
public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
System.out.println("httpsession被销毁");
countPersion(httpSessionEvent.getSession().getServletContext(), false);
}
/*
* 变更在线的人数
* */
public void countPersion(ServletContext sc, boolean isAdd) {
// 为了防止多线程并发问题加锁
synchronized (sc) {
// 获得当前的在线人数
Integer count = (Integer) sc.getAttribute("count");
if(isAdd) {
sc.setAttribute("count", ++count);
} else {
sc.setAttribute("count", --count);
}
}
}
}
(3)前端页面上去获取显示
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
<%-- 创建HttpSessionListenter--%>
request.getSession();
%>
当前的在线人数 ${count}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~