当前位置:网站首页>Listener listener
Listener listener
2022-07-01 03:23:00 【Al_ tair】
Monitor Listener
Hello, everyone , I'm Xiao Sheng , Let me share with you my study Javaweb The notes
Monitor Listener
summary
Listener Monitor it's JavaWeb One of the three components of ,JavaWeb The three major components of are :Servlet Program 、Listener Monitor 、Filter filter
The function of the monitor : Monitor a change ( Generally, it is object creation / The destruction , Property changes ), Trigger the corresponding method to complete the corresponding task
JavaWeb There are eight listeners in the , At present, the most commonly used is ServletContextListener
ServletContextListener
effect : monitor ServletContext Create or destroy ( When we Web When app starts , Will be created ServletContext) Life cycle monitoring
Application scenarios
- Load the initialized configuration file such as spring Configuration file for
- Task scheduling ( Cooperation timer Timer/TimerTask)
Method
void contextInitialized(ServletContextEvent sce) // establish Servletcontext Trigger when
void contextDestroyed(ServletContextEvent sce) // The destruction Servletcontext Trigger when
Code example
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.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">
<listener>
<listener-class>com.al_tair.listener.HttpServletContextListener</listener-class>
</listener>
</web-app>
public class HttpServletContextListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
System.out.println("ServletContext Be created ");
}
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
System.out.println("ServletContext Be destroyed ");
}
}
ServletContextAttributeListener
effect : monitor ServletContext Property changes
Method
void attributeAdded(ServletContextAttributeEvent event) // Add attribute
void attributeReplaced(ServletContextAttributeEvent event) // Replace attributes
void attributeRemoved(ServletContextAttributeEvent event) // Remove properties
Sample code

// Servlet
@WebServlet(urlPatterns = {
"/listener"})
public class ServletListenerDemo extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("doGet Called ");
ServletContext servletContext = req.getServletContext();
servletContext.setAttribute("name"," Niansheng ");
servletContext.setAttribute("name"," Zhang Luorong ");
servletContext.removeAttribute("name");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
// listener
public class HttpServletContextAttributeListener implements ServletContextAttributeListener {
@Override
public void attributeAdded(ServletContextAttributeEvent servletContextAttributeEvent) {
Object name = servletContextAttributeEvent.getServletContext().getAttribute("name");
if(name != null)
System.out.println(" Add attribute " + name.toString());
else
System.out.println(" Add attribute ");
}
@Override
public void attributeRemoved(ServletContextAttributeEvent servletContextAttributeEvent) {
System.out.println(" Remove properties ...");
}
@Override
public void attributeReplaced(ServletContextAttributeEvent servletContextAttributeEvent) {
System.out.println(" Replace attributes ...");
}
}
HttpSessionListener
effect : monitor Session Create or destroy , Life cycle monitoring
Method
void sessionCreated(HttpSessionEvent se) // establish session Called when the
void sessionDestroyed(HttpSessionEvent se) // The destruction session Called when the
HttpSessionAttributeListener
effect : monitor Session Changes in attributes
Method
void attributeAdded(ServletRequestAttributeEvent srae) // Add attribute
void attributeReplaced(ServletRequestAttributeEvent srae) // Replace attributes
void attributeRemoved(ServletRequestAttributeEvent srae) // Remove properties
ServletRequestListener
effect : monitor Request Create or destroy , namely Request Life cycle monitoring , Can be used to monitor , Some IP Frequency of visiting our website , logging , Access to resources
Method
void requestInitialized(ServletRequestEvent sre) // establish request
void requestDestroyed(ServletRequestEvent sre) // The destruction request
ServletRequestAttributeListener
effect : monitor Request Property changes
Method
void attributeAdded(ServletRequestAttributeEvent srae) // When adding attributes
void attributeReplaced(ServletRequestAttributeEvent srae) // Replace attributes
void attributeRemoved(ServletRequestAttributeEvent srae) // Remove properties
Other listeners
HttpSessionBindingListener Perception monitor
HttpSessionActivationListener Perception monitor
边栏推荐
- ctfshow爆破wp
- 伺服第二编码器数值链接到倍福PLC的NC虚拟轴做显示
- Design practice of current limiting components
- Best used trust automation script (shell)
- 世界上最好的学习法:费曼学习法
- ECMAScript 6.0
- Detailed explanation of ES6 deconstruction grammar
- pytest-fixture
- 完全背包问题
- Introduction to the core functions of webrtc -- an article to understand peerconnectionfactoryinterface rtcconfiguration peerconnectioninterface
猜你喜欢

Huawei operator level router configuration example | configuration static VPLS example

Avalanche problem and the use of sentinel
![Install vcenter6.7 [vcsa6.7 (vCenter server appliance 6.7)]](/img/83/e3c9d8eda9d5351d4c54928d3b090b.png)
Install vcenter6.7 [vcsa6.7 (vCenter server appliance 6.7)]

最新接口自动化面试题

彻底解决Lost connection to MySQL server at ‘reading initial communication packet

C # realize solving the shortest path of unauthorized graph based on breadth first BFS -- complete program display

XXL job User Guide

Hal library operation STM32 serial port

Chapitre 03 Bar _ Gestion des utilisateurs et des droits

终极套娃 2.0 | 云原生交付的封装
随机推荐
Leetcode 1482 guess, how about this question?
【EXSI】主机间传输文件
Introduction to webrtc concept -- an article on understanding source, track, sink and mediastream
[linear DP] longest common subsequence
Go tool cli for command line implementation
Redis tutorial
[applet project development -- JD mall] uni app commodity classification page (first)
Golang多图生成gif
HTB-Lame
如何校验两个文件内容是否相同
第03章_用户与权限管理
leetcode 1818 绝对值,排序,二分法,最大值
C#实现基于广度优先BFS求解无权图最短路径----完整程序展示
Redis分布式锁的8大坑
Metadata in NFT
不用加减乘除实现加法
Analyze datahub, a new generation metadata platform of 4.7K star
Redis 教程
第03章_用戶與權限管理
POI导出excel,按照父子节点进行分级显示