Springboot入坑(二):Springboot中Filter、Listener的用法

前言

上一篇文章中我们讲到了快速搭建Springboot项目以及整合Mybatis,但是在实际的开发中我们还需要用到很多的东西。后面的文章中我们会陆续的加入各个知识点,慢慢的丰满我们的demo。

Filter的用法

我们先来了解下Filter

Filter也称之为过滤器,它是Servlet技术中最实用的技术,Web开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp, Servlet, 静态图片文件或静态 html 文件等进行拦截,从而实现一些特殊的功能。例如实现URL级别的权限访问控制、过滤敏感词汇、压缩响应信息等一些高级功能。

它主要用于对用户请求进行预处理,也可以对HttpServletResponse进行后处理。使用Filter的完整流程:Filter对用户请求进行预处理,接着将请求交给Servlet进行处理并生成响应,最后Filter再对服务器响应进行后处理。

import org.springframework.core.annotation.Order;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
/**
 * 使用@WebFilter注解标注过滤器
 * 属性filterName声明过滤器的名称,可选
 * 属性urlPatterns指定要过滤 的URL模式,也可使用属性value来声明.(指定要过滤的URL模式是必选属性)
 * @Order(1)控制加载顺序
 * @author 刘铖
 * @since 2018/4/9
 */
@Order(1)
//重点
@WebFilter(filterName = "myfilter", urlPatterns = "/*")
public class IndexFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {}
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.print("================需要过滤器做什么?==================");
        if("2".equals(servletRequest.getParameter("id"))){
            servletResponse.getWriter().write("Cannot query the data");
        }else{
            filterChain.doFilter(servletRequest,servletResponse);
        }
    }
    @Override
    public void destroy() {}
}

过滤器写好了该如何使用呢?还需要配置web.xml吗?

在springboot中使用过滤器不需我们在去配置web.xml了,那如何让程序扫描到它呢如下

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
/**
 * 使用 @ServletComponentScan注解后,ServletFilterListener
 * 可以直接通过 @WebServlet@WebFilter@WebListener 注解自动注册,无需其他代码。
 * @author 刘铖
 * @since 2018-04-09
 **/
@SpringBootApplication
//重点
@ServletComponentScan
public class ABountyNetApplication1 {
    public static void main(String[] args) {
        SpringApplication.run(ABountyNetApplication1.class, args);
    }
}

这样我们就完成了一个简单拦截器。

重点就是两个注解 @ServletComponentScan @WebFilter顺带也可以去了解一下@order

Listener的用法

监听器用于监听web应用中某些对象、信息的创建、销毁、增加,修改,删除等动作的发生,然后作出相应的响应处理。当范围对象的状态发生变化的时候,服务器自动调用监听器对象中的方法。常用于统计在线人数和在线用户,系统加载时进行信息初始化,统计网站的访问量等等。

按监听的对象划分,可以分为
ServletContext对象监听器
HttpSession对象监听器
ServletRequest对象监听器
按监听的事件划分
对象自身的创建和销毁的监听器
对象中属性的创建和消除的监听器

session中的某个对象的状态变化的监听器

因为分类比较多我们只实现一种

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

/**
 * 使用@WebListener注解,实现ServletContextListener接口
 * @author 刘铖
 * @since 2018/4/9
 */

@WebListener
public class IndexListener implements ServletContextListener{
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        System.out.println("ServletContex初始化");
        System.out.println(servletContextEvent.getServletContext().getServerInfo());
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        System.out.println("ServletContex销毁");
    }
}

如何使用呢和过滤器一样,也不需要在去配置web.xml

重点 @WebListener

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值