Steps to use Java listers: 1. Create an implementation class by implementing a specific interface (multiple listener interfaces can be implemented). 2. Directly use the @WebListener annotation to modify the implementation class and configure the implementation class to become a listener; or configure the implementation class to become a listener through web.xml.
Java listers means listeners, implementation classes used to monitor internal events of web applications. You can monitor the start and end of user sessions, the arrival of user requests, etc. When an event occurs, the internal method of the listener will be called back.
Steps to use Listener
Create an implementation class by implementing a specific interface (multiple listener interfaces can be implemented)
Configure the implementation class to become a listener, There are two configuration methods:
Directly use @WebListener annotation to modify the implementation class
Configure through web.xml, the code is as follows:
<listener> <listener-class>com.zrgk.listener.MyListener</lisener-class> </listener>
Commonly used Web event monitoring ServletContextListener
1. ServletContextListener
This interface is used to monitor the startup and shutdown of Web applications
Two methods of this interface:
contextInitialized(ServletContextEvent event); // 启动web应用时调用 contextDestroyed(ServletContextEvent event); // 关闭web应用时调用
How to get the application object:
ServletContext application = event.getServletContext();
Example:
@WebListener public class MyServetContextListener implements ServletContextListener{ //web应用关闭时调用该方法 @Override public void contextDestroyed(ServletContextEvent event) { ServletContext application = event.getServletContext(); String userName = application.getInitParameter("userName"); System.out.println("关闭web应用的用户名字为:"+userName); } //web应用启动时调用该方法 @Override public void contextInitialized(ServletContextEvent event) { ServletContext application = event.getServletContext(); String userName = application.getInitParameter("userName"); System.out.println("启动web应用的用户名字为:"+userName); } }
2 . ServletContextAttributeListener
This interface is used to monitor changes in attributes within the ServletContext scope (application).
Two methods of this interface:
attributeAdded(ServletContextAttributeEvent event);//当把一个属性存进application时触发 attributeRemoved(ServletContextAttributeEvent event);//当把一个属性从application删除时触发 attributeReplaced(ServletContextAttributeEvent event);//当替换application内的某个属性值时触发
How to obtain the application object:
ServletContext application = event.getServletContext();
Example:
@WebListener public class MyServletContextAttributeListener implements ServletContextAttributeListener{ //向application范围内添加一个属性时触发 @Override public void attributeAdded(ServletContextAttributeEvent event) { String name = event.getName();//向application范围添加的属性名 Object val = event.getValue(); //向application添加的属性对应的属性值 System.out.println("向application范围内添加了属性名为:"+name+",属性值为:"+val+"的属性"); } //删除属性时触发 @Override public void attributeRemoved(ServletContextAttributeEvent event) { // ... } //替换属性值时触发 @Override public void attributeReplaced(ServletContextAttributeEvent event) { // ... } }
The above is the detailed content of How to use java listers. For more information, please follow other related articles on the PHP Chinese website!