filter,(生命周期)&listener配置和创建

filter&listener配置和创建
(全文为学习笔记仅供参考,不足之处还望指教!)
一:filter
简介:
Filter:一个实现了特殊接口(Filter)的Java类. 实现对请求资源(jsp,servlet,html,)的过滤的功能.
​ 过滤器是一个运行在服务器的程序, 优先于请求资源(Servlet或者jsp,html)之前执行. 过滤器是javaweb技术中最为实用的技术.。
作用:
对目标资源(Servlet,jsp)进行过滤,应用场景:登录权限检查,解决网站乱码,过滤敏感字符 …
filter 有两种方式
1:配置文件方式,

	1.1 创建一个类实现Filter接口
    1.2. 在web.xml配置FIlter

2:注解方式,

  2.1. 创建一个类实现Filter接口
  2.2 在这个类上面添加@WebFilter("拦截的路径")

建议直接new Filte
在这里插入图片描述
3.1通过配置文件方式

    创建一个类实现Filter接口
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;


/**
 * 1. 第一步实现Filter接口
 * 2. 在web.xml配置过滤器
 */
public class FilterDemo01 implements Filter {

    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {

        System.out.println("FilterDemo01");

        chain.doFilter(req, resp);
    }

    public void init(FilterConfig config) throws ServletException {
        
    }

}
    在web.xml对过滤器进行配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
		  http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
           version="2.5">
    <filter>
        <filter-name>filterDemo01</filter-name>
        <!--全限定名: 包名.类名-->
        <filter-class>com.itheima.web.filter.FilterDemo01</filter-class>

        
    </filter>
    <filter-mapping>
        <filter-name>filterDemo01</filter-name>
        <!-- 拦截资源路径为demo01的目标资源 -->
        <url-pattern>/demo01</url-pattern>
    </filter-mapping>

</web-app>

3.2 通过注解方式

     - 创建一个类实现Filter接口
	 - 直接在这个类上面添加注解进行配置
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

/**
 * 1. 实现Filter接口
 * 2. 写注解@WebFilter("拦截的目标资源的路径")
 */
@WebFilter("/demo02")
public class FilterDemo02 implements Filter {

    /**
     * 1. 服务器启动时执行
     * @param config
     * @throws ServletException
     */
    public void init(FilterConfig config) throws ServletException {
        System.out.println("init");
    }

    /**
     * 1. 每次请求目标资源都会执行doFilter进行拦截
     * @param req
     * @param resp
     * @param chain
     * @throws ServletException
     * @throws IOException
     */
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        System.out.println("doFilter-demo02");
        chain.doFilter(req, resp);
    }

    /**
     * 1. 服务器关闭时执行destroy方法
     */
    public void destroy() {
        System.out.println("destroy");
    }
}

注:Filter生命周期
介绍:过滤器从创建到销毁的过程
方法/描述:

	init(FilterConfig):初始化     在服务器启动时执行一次,进行初始化工作。
	​doFilter(ServletReqeust req,ServletResponse resp,FilterChain chain):执行过滤的方法  	每次请求目标资源都会执行。
	​destroy():销毁 		在服务器关闭时会执行一次

实例:

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

/**
 * 1. 实现Filter接口
 * 2. 写注解@WebFilter("拦截的目标资源的路径")
 */
@WebFilter("/demo02")
public class FilterDemo02 implements Filter {

    /**
     * 1. 服务器启动时执行
     * @param config
     * @throws ServletException
     */
    public void init(FilterConfig config) throws ServletException {
        System.out.println("init");
    }
    
    /**
     * 1. 每次请求目标资源都会执行doFilter进行拦截
     * @param req
     * @param resp
     * @param chain
     * @throws ServletException
     * @throws IOException
     */
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        System.out.println("doFilter-demo02");
        chain.doFilter(req, resp);
    }

    /**
     * 1. 服务器关闭时执行destroy方法
     */
    public void destroy() {
        System.out.println("destroy");
    }
}

二:监听器Listener
创建监听器步骤:

		1. 创建一个类实现ServletContextListener
		2. 在web.xml配置

示例:MyContextListener.java

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;


/**
 * 1. 一个类实现ServletContextListener
 * 2. 在web.xml配置该监听器
 */
public class MyContextListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("contextInitialized");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("contextDestroyed");
    }
}

配置(web.xml)

<!--配置MyContextListener-->
    <listener>
        <listener-class>com.itheima.web.listener.MyContextListener</listener-class>
    </listener>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值