Home > Java > javaTutorial > body text

How to use java listers

(*-*)浩
Release: 2020-09-17 16:52:05
Original
3001 people have browsed it

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.

How to use java listers

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>
Copy after login

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应用时调用
Copy after login

How to get the application object:

ServletContext application = event.getServletContext();
Copy after login
Copy after login

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);
    }

}
Copy after login

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内的某个属性值时触发
Copy after login

How to obtain the application object:

ServletContext application = event.getServletContext();
Copy after login
Copy after login

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) {
        // ...      
    }

}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!