当前位置:网站首页>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
边栏推荐
猜你喜欢

几行事务代码,让我赔了16万
![[us match preparation] complete introduction to word editing formula](/img/e4/5ef19d52cc4ece518e79bf10667ef4.jpg)
[us match preparation] complete introduction to word editing formula

Introduction and installation of Solr

完全背包问题

Stop saying that you can't solve the "cross domain" problem

Chapter 03_ User and authority management

POI exports excel and displays hierarchically according to parent-child nodes

E15 solution for cx5120 controlling Huichuan is620n servo error

Hal library operation STM32 serial port

安装VCenter6.7【VCSA6.7(vCenter Server Appliance 6.7) 】
随机推荐
mybati sql 语句打印
[exsi] transfer files between hosts
岭回归和lasso回归
Ridge regression and lasso regression
Metadata in NFT
Cookie&Session
Introduction to core functions of webrtc -- an article on understanding SDP PlanB unifiedplan (migrating from PlanB to unifiedplan)
Example of Huawei operator level router configuration | example of configuring optionc mode cross domain LDP VPLS
Keil5中如何做到 0 Error(s), 0 Warning(s).
家居网购项目
Druid monitoring statistics source
Introduction and installation of Solr
JS日常开发小技巧(持续更新)
终极套娃 2.0 | 云原生交付的封装
HTB-Lame
Servlet [first introduction]
多元线性回归
几行事务代码,让我赔了16万
gcc使用、Makefile总结
安装VCenter6.7【VCSA6.7(vCenter Server Appliance 6.7) 】