当前位置:网站首页>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
边栏推荐
- How mysterious is "PIP not an internal or external command, nor a runnable program or batch file"
- The execution results of i+=2 and i++ i++ under synchronized are different
- Detailed explanation of search tree and hash table
- Handwritten promise
- Kyma application connectivity feature introduction
- Analysis of test questions in Chapter 13 of PMP preparation
- Torch network model is converted to onnx format and visualized
- Centos7 installing PHP
- Solve NPM compilation times node_ modules/optipng-bin/vendor/optipng ENOENT
- QT pro文件配置ffmpeg宏
猜你喜欢
Design rule check constraint (set_max_transition, set_max_capability)
Wall Street cheat sheet
Introduction to system mode development of rouya wechat mall
How to close icloud when Apple ID of Apple mobile phone forgets password and frequently jumps out to log in
半路自学测试成功转行,第一份测试工作就拿10k
Why my order by create_ Time ASC becomes order by ASC
Event distribution mechanism of view
3 R complex data types
测试人如何规划自己的未来?才能实现入行2年达到25k?
Niuke.com: sum of three numbers
随机推荐
Compilation of programs
How to make a computer installation and startup USB flash disk
What is the difference between union and union all
How to download putty using alicloud image?
Deep feature synthesis and genetic feature generation, comparison of two automatic feature generation strategies
新来的同事问我 where 1=1 是什么意思???
Properties to YML
Golang type assertion understanding [go language Bible]
Lightroom Ambassador series: capturing nostalgia with MEG loeks
How to close icloud when Apple ID of Apple mobile phone forgets password and frequently jumps out to log in
Zhangqiming, vice director of the United Front Work Department of the CPC Anhui Provincial Committee, led a team to investigate the HoloNet Royal Hefei R & D base
[leetcode 16 solution] the sum of the nearest three numbers
Generate API documents using swagger (go language example)
Continuously improve the overall performance of adaoracle Oracle Oracle
system()
设计规则检查约束(set_max_transition、set_max_capacitance)
View 的事件分发机制
MySQL Basics
牛客網:三數之和
Centos7 installing MySQL 5.7