当前位置:网站首页>Listener in JSP
Listener in JSP
2022-06-12 20:28:00 【Fairy-KunKun】

Servlet The monitor in
demand :mood The system counts the number of online people

The listener belongs to a third party , It should not affect the main program , Pluggable .
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;
}
}
Define a listener , Special monitoring session Data changes .

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 ) {
}
}
To configure
<listener >
<listener-class >com.njwbhz.mood.listener.OnlineListener</listener-class>
</listener>
Start the test
<%@ 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> Number of people online </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>
Number of people online :<%=OnlineCounter.getOnline ( )%>
</body>
</html>
explain :
The monitor is web Application services , Independent listener , Used to listen for state changes of objects .
Servlet There are many kinds of listeners in ,

law : listening object +Listener, Implement the corresponding interface

Use annotations for configuration
Look at another listener

ServletContext When the object is created ?
The server is created at startup .
Servlet Configured with load-on-startupàtomcat Startup time , Some preparatory work can be done ( Initialization work ) hand servlet complete .
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 ) {
}
}
Start the server

explain : stay web In the program , To initialize the program , Three solutions :
(1) Monitor
(2) filter
(3)Servlet
边栏推荐
- 1. Getting to know R
- Lx09 query tr list of SAP WM preliminary level
- 华尔街备忘单(Wall Street Cheat Sheet)
- P5076 【深基16.例7】普通二叉树(简化版)
- Golang type assertion understanding [go language Bible]
- Understanding of data in memory
- [untitled]
- What is disk IO?
- Viewpoint sharing | Li Wei, an expert of Gewu titanium intelligent technology products: underlying logic and scenario practice of unstructured data platform
- Lightroom 大使系列:用 Meg Loeks 捕捉怀旧之情
猜你喜欢

Halcon angle and radian interchange

半路自学测试成功转行,第一份测试工作就拿10k

How mysterious is "PIP not an internal or external command, nor a runnable program or batch file"

20 shortcut keys for vs code!
![[leetcode] small thinking of optimal division](/img/da/ea01225047158c9ca53ac1c585bcd9.jpg)
[leetcode] small thinking of optimal division

Alipay payment episode 11: monitoring after successful payment callback

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

Experience Technology Department of ant group launched the 2023rd school recruitment

Low code enables rural construction to open "smart mode"

Overview of object detection
随机推荐
Dynamic memory management
Niuke.com: sum of three numbers
Restful API 接口规范
Why my order by create_ Time ASC becomes order by ASC
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
Golang type assertion understanding [go language Bible]
标量、向量、数组和矩阵
EDI 855 purchase order confirmation
Golden, silver and four job hopping season, teach you these tips to improve the interview success rate
4 R basic exercises
MySQL installation and Application
CentOS7安装MySQL5.7操作说明
The latest Ningxia construction safety officer simulation question bank and answers in 2022
Scalars, vectors, arrays, and matrices
入行5年从10k的功能测试到年薪40w的测试开发,花7天时间整理的超全学习路线
Alipay payment episode 11: monitoring after successful payment callback
In 2022, FISCO bcos MVP recognized that the channel was open and invited you to become an open source leader
Connectez - vous à MySQL
centos7 安装 mysql 5.7