JavaWeb三大组件——Listener

   

目录

 Listener

八个Web监听器

注册Listener

父pom文件

pom文件

使用注解方式

ServletListener 

 HelloServlet 

 启动类

通过RegistrationBean注册

ServletListener

HelloServlet

MyWebConfig 

启动类

 运行结果


     JavaWeb三大组件——Listener_第1张图片

 Listener

作用:监听某个事件的发生,状态的改变
内部机制:接口回调

八个Web监听器

接口名 作用
ServletContextListener ServletContext对象的创建
HttpSessionListener Session对象的创建
ServletRequestListener Request对象的创建
ServletContextAttributeListener ServletContext属性变化
HttpSessionAttributeListener Session属性变化
ServletRequestAttributeListener Request属性变化
HttpSessionActivationListener Session钝化与活化
HttpSessionBindingListener Session与对象的绑定

注册Listener

JavaWeb三大组件——Listener_第2张图片

 

父pom文件



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent

        2.2.5.RELEASE
         
    
    com.chensir
    springboot
    0.0.1-SNAPSHOT
    springboot
    springboot
    
        8
    

    pom

    
        servlet
    

    
        
            cn.hutool
            hutool-all
            5.8.3
        







        
            org.projectlombok
            lombok
            1.18.22
        

        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


pom文件



    4.0.0
    
        com.chensir
        springboot
        0.0.1-SNAPSHOT
        ../pom.xml
    

    servlet

    

        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


使用注解方式

使用注解 在启动类上必须加 @ServletComponentScan注解,若使用bean方式注册,则不需要

ServletListener 

package com.chensir.javaWeb;

import cn.hutool.core.date.StopWatch;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.annotation.WebListener;

@WebListener
@Slf4j
public class ServletListener implements ServletRequestListener {

    // StopWatch存储一组任务耗时时间
    StopWatch stopWatch = null;

    @Override
    public void requestInitialized(ServletRequestEvent sre) {
        stopWatch = new StopWatch("监控");
        stopWatch.start();
        log.info("-------有人来了----------");
    }

    @Override
    public void requestDestroyed(ServletRequestEvent sre){
        stopWatch.stop();
        log.info("-------有人出来了,耗时:{}----------",stopWatch.getTotalTimeSeconds());

        // todo:如果超过多少时间,发信息给开发者
    }
}

 HelloServlet 

package com.chensir.javaWeb;

import cn.hutool.core.thread.ThreadUtil;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(value = {"/hello"})
@Slf4j
public class HelloServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.setContentType("text/html;charset=utf-8");
        resp.getWriter().write("你好,宝宝");

        log.info("给航天员打招呼!");

        // 睡眠5s
        ThreadUtil.safeSleep(5000);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
    }
}

 启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

@SpringBootApplication
@ServletComponentScan
public class ServletApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServletApplication.class, args);
    }

}

通过RegistrationBean注册

ServletListener

JavaWeb三大组件——Listener_第3张图片

 

HelloServlet

JavaWeb三大组件——Listener_第4张图片

 MyWebConfig 

import com.chensir.javaWeb.HelloServlet;
import com.chensir.javaWeb.ServletListener;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyWebConfig {

    @Bean
    public ServletRegistrationBean servletRegistrationBean() {
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();
        servletRegistrationBean.setServlet(new HelloServlet());
        servletRegistrationBean.addUrlMappings("/helloBaby");
        return servletRegistrationBean;
    }

    @Bean
    public ServletListenerRegistrationBean servletListenerRegistrationBean() {
        ServletListenerRegistrationBean bean = new ServletListenerRegistrationBean();
        bean.setListener(new ServletListener());
        // 启用
        bean.setEnabled(true);
        return bean;
    }
}

启动类

JavaWeb三大组件——Listener_第5张图片

 运行结果

JavaWeb三大组件——Listener_第6张图片

 JavaWeb三大组件——Listener_第7张图片

 

你可能感兴趣的:(java,servlet,web,java)