当前位置:网站首页>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
边栏推荐
- Design rule check constraint (set_max_transition, set_max_capability)
- The execution results of i+=2 and i++ i++ under synchronized are different
- 服务端口不通排查
- Unsupported class file major version 60
- Since using low code development, the development efficiency has been increased by 10 times
- 使用Swagger生成 API 文档(go语言示例)
- Wechat jsapi payment pit summary
- MySQL index classification
- MySQL log
- Connectez - vous à MySQL
猜你喜欢

EditText控制从左上角开始

Wall Street cheat sheet

Design rule check constraint (set_max_transition, set_max_capability)

Understanding of data in memory

What is a federated index?

In 2022, 20 cities with the largest number of college students in China

How to close icloud when Apple ID of Apple mobile phone forgets password and frequently jumps out to log in

How to determine fragment restored from Backstack

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

If you master these skills, you can also write a high score resume in the eyes of HR
随机推荐
登录mysql
Go memory escape analysis
synchronized下的 i+=2 和 i++ i++执行结果居然不一样
Since using low code development, the development efficiency has been increased by 10 times
[leetcode] small thinking of optimal division
Lightroom Ambassador series: capturing nostalgia with MEG loeks
golang类型断言理解[go语言圣经]
View 的事件分发机制
逐向双碳:东数西算中的绿色需求与竞争焦点
Detailed explanation of search tree and hash table
Wechat applet notes
新来的同事问我 where 1=1 是什么意思???
Event distribution mechanism of view
Wall Street cheat sheet
标量、向量、数组和矩阵
Microsoft Word tutorial, how to insert page numbers and table of contents in word?
MySQL log
Installation and testing of open source deep learning framework plaidml
Unsupported class file major version 60
可测性设计学习笔记