一路繁花似锦绣前程
失败的越多,成功才越有价值

导航

 

 一、filter

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

/*
一、Filter
    1、实现Filter接口
    2、配置:
        注解方式:@WebFilter(String)
        web.xml方式:
            <filter>
                <filter-name>FirstFilter</filter-name>
                <filter-class>filter.FirstFilter</filter-class>
            </filter>
            <filter-mapping>
                <filter-name>FirstFilter</filter-name>
                <url-pattern>/*</url-pattern>
            </filter-mapping>
    3、执行过程:
        1、执行过滤器方法
        2、执行放行后的资源(过滤器链对象.doFilter(请求对象, 响应对象))
        3、继续执行过滤器方法
    4、生命周期:
        1、init:启动tomcat时执行该方法
        2、doFilter:每当请求被过滤规则匹配时执行该方法
        3、destroy:正常关闭tomcat时执行该方法
    5、过滤器配置详解
        * 拦截路径配置
            1、具体资源路径:/index.jsp 只有访问index.jsp资源时,过滤器才会被执行
            2、拦截目录:/user/* 访问/user下的所有资源时,过滤器都会被执行
            3、后缀名拦截:*.jsp 访问所有后缀名为jsp资源时,过滤器都会被执行
            4、拦截所有资源:/* 访问所有资源时,过滤器都会被执行
        * 拦截方式配置
            1、注解配置(设置dispatcherTypes属性):
                * REQUEST:默认值。浏览器直接请求资源
                * FORWARD:转发访问资源
                * INCLUDE:包含访问资源
                * ERROR:错误跳转资源
                * ASYNC:异步访问资源
            2、web.xml配置:
                <filter-mapping>
                    <dispatcher></dispatcher>
                </filter-mapping>
    6、过滤器链(配置多个过滤器)
        * 执行顺序:
            1、执行过滤器1(doFilter前)
            2、执行过滤器2(doFilter前)
            3、执行资源
            3、执行过滤器2(doFilter后)
            4、执行过滤器1(doFilter后)
        * 过滤器先后顺序问题:
            1、注解配置:按照类名的字符串比较规则比较,值小的先执行
                * 如:AFilter和BFilter,AFilter就先执行了
            2、web.xml配置:<filter-mapping>谁定义在上边,谁先执行
 */
@WebFilter("/*")//定义需要过滤的资源名称规则
public class FirstFilter 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.println("执行filter方法");
        //放行
        filterChain.doFilter(servletRequest, servletResponse);
        System.out.println("继续执行filter方法");
    }

    @Override
    public void destroy() {

    }
}

 

二、listener

/*
1、实现ServletContextListener接口
2、重写方法:
    void contextInitialized(ServletContextEvent):服务器启动时执行
    void contextDestroyed(ServletContextEvent):服务器正常关闭时执行
3、配置监听器:
    注解配置:
        @WebListener
    web.xml配置:
        * 配置:
            <listener>
                <listener-class>listener.FirstListener</listener-class>
            </listener>
        * 指定初始化参数:
            <context-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>/WEB-INF/classes/application.xml</param-value>
            </context-param>
*/
//@WebListener
public class FirstListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        //模拟加载配置文件
        ServletContext servletContext = sce.getServletContext();
        String cfg = (String) servletContext.getInitParameter("contextConfigLocation");
        String cfgpath = servletContext.getRealPath(cfg);
        try {
            FileInputStream fis = new FileInputStream(cfgpath);
            System.out.println(fis);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {

    }
}

 

posted on 2020-09-02 11:53  一路繁花似锦绣前程  阅读(165)  评论(0编辑  收藏  举报