springboot学习系列六:springboot项目中filter和listener的使用

目录

 

Springboot项目中使用filter和listener

Filter在springboot项目中的应用

Listener在springboot项目中的应用


Springboot项目中使用filter和listener

 

Filter在springboot项目中的应用

package com.example.demo.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

@WebFilter(filterName = "userFilter",urlPatterns = "/*")
public class UserFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("注意,注意:这是我过滤器filter的初始化方法!");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("注意,注意:这是我过滤器filter的dofilter方法!");
    }

    @Override
    public void destroy() {
        System.out.println("注意,注意:这是我过滤器filter的销毁方法!");
    }
}
package com.example.demo;

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

@SpringBootApplication
@ServletComponentScan
public class DemoApplication {

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

}

 

在springboot项目中使用filter,相应的类需要实现filter类,类需要添加“@WebFilter(filterName=””,urlPattern=””)”注解。入口类*Application.java需要添加注解“@ServletComponentScan”。

(1)注解@WebFilter

此注解用来将一个类声明为过滤器。该注解常用的属性有:

1)filterName:用于指定过滤器的name,与xml文件中的<filter-name>标签作用相同。

2)urlPattern:用来指定一组过滤器的URL匹配规则,与xml配置文件中的<url-pattern>标签作用相同。

3)value:作用跟urlPattern属性相同,但是两者不能够同时使用。

(2)注解@ServletComponentScan

在springboot的入口类或者主类中使用此注解后,servlet、filter、listener可通过@WebServlet、@WebFilter、@WebListener注解自动注册,无需其他代码。

Listener在springboot项目中的应用

package com.example.demo.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

@WebListener
public class UserListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("注意,注意:这是我的listener的初始化方法!");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("注意,注意:这是我的listener的销毁方法!");
    }
}
package com.example.demo;

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

@SpringBootApplication
@ServletComponentScan
public class DemoApplication {

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

}

 

在springboot项目中使用监听器,相应的类需要根据需要实现不同的监听类,类都需要添加“@WebListener”注解。入口类*Application.java需要添加注解“@ServletComponentScan”。

(1)监听器listener

1)根据监听对象的不同,我们可以将监听器分为:ServletContext、HttpSession、ServletRequest三种。

ServletContext监听ServletContext对象的创建和销毁。(application级别的,在整个web中只有一个)

ServletContextListener其接口方法分别为:

contextInitialized(ServletContextEvent arg0)   -- 创建时执行

contextDestroyed(ServletContextEvent arg0)  -- 销毁时执行

HttpSession监听session对象的创建和销毁。(session级别的,对应每个对话)

HttpSessionListener其接口方法分别为:

sessionCreated(HttpSessionEvent se)   -- 创建时执行

sessionDestroyed(HttpSessionEvent se) -- 销毁时执行

ServletRequest监听request对象的创建和销毁。(request级别的,对应客户每次发出的请求)

ServletRequestListener 其接口方法分别为:

requestInitialized(ServletRequestEvent sre) -- 创建时执行

 requestDestroyed(ServletRequestEvent sre) -- 销毁时执行

 

2)根据监听事件可以分为监听对象创建、销毁,如监听对象域中属性的添加和删除。

ServletContextAttribute监听servletContext对象中属性的改变

ServletContextAttributeListener 其接口方法分别为:

attributeAdded(ServletContextAttributeEvent event) -- 添加属性时执行

attributeReplaced(ServletContextAttributeEvent event) -- 修改属性时执行

attributeRemoved(ServletContextAttributeEvent event) -- 删除属性时执行

HttpSessionAttribute监听session对象中属性的改变

HttpSessionAttributeListener其接口方法分别为:

attributeAdded(HttpSessionBindingEvent event) -- 添加属性时执行

attributeReplaced(HttpSessionBindingEvent event) -- 修改属性时执行

attributeRemoved(HttpSessionBindingEvent event) -- 删除属性时执行

ServletRequestAttribute监听request对象中属性的改变

ServletRequestAttributeListener其接口方法分别为:

attributeAdded(ServletRequestAttributeEvent srae) -- 添加属性时执行

attributeReplaced(ServletRequestAttributeEvent srae) -- 修改属性时执行

attributeRemoved(ServletRequestAttributeEvent srae) -- 删除属性时执行

(2)监听器的作用

1)监听web应用中某些对象、信息的创建、销毁、增加、修改、删除等动作的发生,然后做出相应的响应处理。

2)统计在线人数和在线用户。

3)系统加载时进行信息的初始化。

4)统计网站的访问量。

5)记录用户的访问路径等。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值