当前位置:网站首页>JSP中的监听器
JSP中的监听器
2022-06-12 20:24:00 【Fairy-KunKun】

Servlet中的监听器
需求:mood系统中统计在线人数

监听器属于第三者,不应该影响主体程序,具有可插拔性。
package com.njwbhz.mood.util;
public class OnlineCounter {
private static int cnt = 0;
public static synchronized void in () {
cnt++;
}
public static synchronized void out () {
cnt--;
}
public static int getOnline () {
return cnt;
}
}
定义一个监听器,专门监听session数据变化。

package com.njwbhz.mood.listener;
import com.njwbhz.mood.util.OnlineCounter;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
public class OnlineListener implements HttpSessionAttributeListener {
@Override
public void attributeAdded ( HttpSessionBindingEvent httpSessionBindingEvent ) {
System.out.println (
"a user is coming"
);
OnlineCounter.in ( );
}
@Override
public void attributeRemoved ( HttpSessionBindingEvent httpSessionBindingEvent ) {
System.out.println (
"a user is outing"
);
OnlineCounter.out ( );
}
@Override
public void attributeReplaced ( HttpSessionBindingEvent httpSessionBindingEvent ) {
}
}
配置
<listener >
<listener-class >com.njwbhz.mood.listener.OnlineListener</listener-class>
</listener>
启动测试
<%@ page import="com.njwbhz.mood.util.OnlineCounter" %>
<%
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>
<base href="<%=basePath%>">
<title>在线人数</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
在线人数:<%=OnlineCounter.getOnline ( )%>
</body>
</html>
说明:
监听器为web应用服务的,独立的监听程序,用于监听对象的状态变化。
Servlet中的监听器有很多种,

规律:监听对象+Listener,实现相应的接口

使用注解做配置
再看一个监听器

ServletContext对象什么时候被创建?
服务器启动时被创建。
Servlet配置了load-on-startupàtomcat启动时,可以将一些准备工作(初始化工作)交给servlet完成。
package com.njwbhz.mood.servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
@WebServlet ( value = "/startup", loadOnStartup = 1 )
public class StartServlet extends HttpServlet {
@Override
public void init ( ) throws ServletException {
System.out.println ( "StartServlet execute init" );
}
}
package com.njwbhz.mood.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class MyServletContextListener implements ServletContextListener {
@Override
public void contextInitialized ( ServletContextEvent servletContextEvent ) {
System.out.println (
"MyServletContextListener execute init"
);
}
@Override
public void contextDestroyed ( ServletContextEvent servletContextEvent ) {
}
}
启动服务器

说明:在web程序中,要为程序做初始化工作,三种解决方案:
(1)监听器
(2)过滤器
(3)Servlet
边栏推荐
- 代理与反射
- 牛客网:三数之和
- MySQL log
- 新来的同事问我 where 1=1 是什么意思???
- [leetcode 7 solution] integer inversion
- Centos7 installing MySQL 5.7
- Experience Technology Department of ant group launched the 2023rd school recruitment
- P5076 [deep base 16. Example 7] common binary tree (simplified version)
- Low code enables rural construction to open "smart mode"
- golang类型断言理解[go语言圣经]
猜你喜欢

Properties to YML

A Zhu and Xu Baobao's high-rise game - difference

When will the index fail

Introduction to the characteristics of building a balancer decentralized exchange market capitalization robot

Generate API documents using swagger (go language example)

解决cvxpy报错The solver GLPK_MI is not installed

2022年春招,测试工程师全套面试攻略,一篇吃透全部技术栈(全是干货)

Explain

System log

Macro definitions and functions
随机推荐
代理与反射
The difference between MySQL full table scanning and indexing
golang类型断言理解[go语言圣经]
How mysterious is "PIP not an internal or external command, nor a runnable program or batch file"
Niuke net: somme des trois nombres
【生成对抗网络学习 其三】BiGAN论文阅读笔记及其原理理解
Connectez - vous à MySQL
P5076 [Fondation profonde 16. Exemple 7] Arbre binaire commun (version simplifiée)
The Milvus graphical management tool Attu is coming!
What does SQL replace or
Event distribution mechanism of view
[leetcode 7 solution] integer inversion
入行5年从10k的功能测试到年薪40w的测试开发,花7天时间整理的超全学习路线
华尔街备忘单(Wall Street Cheat Sheet)
sklearn中随机森林RandomForestClassifier的参数含义
PostgreSQL database replication - background first-class citizen process walreceiver PG_ stat_ wal_ Receiver view
【GAMES101】课堂笔记8–着色(着色频率、图形管线、纹理映射)
go --- 监控文件变化
Process creation fork (), demise wait()
What is an index?