JavaWeb三大组件【Servlet、Filter、Listener】

JavaWeb三大组件【Servlet、Filter、Listener】,第1张

目录

引言

Filter

Listener


引言

JavaWeb三大组件指的是:ServletFilterListener,这三个组件在JavaWeb开发中分别提供不同的功能。Servlet 是 JavaWeb 的三大组件之一,它属于动态资源,前面文章有写到(请参考):web阶段~Servlet学习_乔巴菌儿的博客-CSDN博客  今天,我们将进行Filter(过滤器)与Listener(监听器)的学习。

Filter

【概念】

web中的过滤器:当访问服务器的资源时,过滤器可以将请求拦截下来,完成一些特殊的功能。filter与servlet在很多的方面极其相似,但是也有不同,例如filter和servlet一样都又三个生命周期方法,同时他们在web.xml中的配置文件也是差不多的、 但是servlet主要负责处理请求,而filter主要负责拦截请求,和放行。

快速入门:

        1. 定义一个类,实现接口Filter

        2. 复写方法

        3. 配置拦截路径(web.xml、  注解)

//    @WebFilter("/*") //访问所有资源之前都会执行该过滤器
public class FilterDemo1 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("filterDemo1被执行了......");

        //执行
        filterChain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void destroy() {

    }
}

过滤器细节:

        web.xml配置


    demo1
    cn.itcast.web.filter.FilterDemo1


    demo1
    
    /*

【执行流程】

1. 执行过滤器

2. 执行放行后的资源
3. 回来执行过滤器放行代码下边的代码

【生命周期】

1. init:在服务器启动后,会创建Filter对象,然后调用init方法。用于加载资源,只执行一次。

2. doFilter:每一次请求被拦截资源时,会执行。执行多次

3. destroy:在服务器关闭后,Filter对象被销毁。如果服务器是正常关闭,则会执行destroy方法。用于释放资源,只执行一次。

@WebFilter("/*")
public class FilterDemo3 implements Filter {

    //每一次请求被拦截资源时,会执行(执行多次)
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        System.out.println("dofilter...");
        chain.doFilter(req, resp);
    }

    //在服务器启动后创建Filter对象,然后调用init方法(执行一次)
    //【加载资源】
    public void init(FilterConfig config) throws ServletException {
        System.out.println("init...");
    }

    //在服务器关闭后,Filter对象被销毁,如果服务器正常关闭,则会调用destroy方法(执行一次)
    //【释放资源】
    public void destroy() {
        System.out.println("destroy...");
    }
}

【配置详解】

1. 具体资源路径: /index.jsp   只有访问index.jsp资源时,过滤器才会被执行
2. 拦截目录: /user/*   访问/user下的所有资源时,过滤器都会被执行
3. 后缀名拦截: *.jsp   访问所有后缀名为jsp资源时,过滤器都会被执行
4. 拦截所有资源:/*   访问所有资源时,过滤器都会被执行

//@WebFilter("/index.jsp")
//1. 具体资源路径: /index.jsp 只有访问index.jsp资源时,过滤器才会被执行

//@WebFilter("/user/*")
//2. 拦截目录: /user/* 访问/user下的所有资源时,过滤器都会被执行

//@WebFilter("*.jsp")
public class FilterDemo4 implements Filter {

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        System.out.println("filterDemo4...");
        chain.doFilter(req, resp);
    }

    public void init(FilterConfig config) throws ServletException {

    }

    public void destroy() {

    }

}

【注解配置&web.xml配置】

设置dispatcherTypes属性:

        1. REQUEST:默认值。浏览器直接请求资源
        2. FORWARD:转发访问资源
        3. INCLUDE:包含访问资源
        4. ERROR:错误跳转资源
        5. ASYNC:异步访问资源

web.xml配置:

        设置标签即可

//浏览器直接请求index.jsp资源时,该过滤器会被执行
//@WebFilter(value = "/index.jsp",dispatcherTypes = DispatcherType.REQUEST)

//只有转发访问index.jsp时,该过滤器才会被执行
//@WebFilter(value = "/index.jsp",dispatcherTypes = DispatcherType.FORWARD)

//浏览器直接请求index.jsp或者转发访问index.jsp,该过滤器都会被执行
//@WebFilter(value = "/index.jsp",dispatcherTypes = {DispatcherType.FORWARD,DispatcherType.REQUEST})
public class FilterDemo5 implements Filter {

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        System.out.println("filterDemo5...");
        chain.doFilter(req, resp);
    }

    public void init(FilterConfig config) throws ServletException {

    }

    public void destroy() {

    }

}

【过滤器链】

配置多个过滤器:(如果有两个过滤器:过滤器1和过滤器2)

1. 过滤器1
2. 过滤器2
3. 资源执行
4. 过滤器2
5. 过滤器1 
过滤器先后顺序问题:

注解配置:按照类名的字符串比较规则比较,值小的先执行
        如: AFilter 和 BFilter,AFilter就先执行了。
2. web.xml配置: 谁定义在上边,谁先执行


Listener

这里参考此篇博客:JavaWeb三大组件(Servlet、Filter、Listener)_xiaojie119120的博客-CSDN博客_servlet三大组件

Listener就是监听器,我们在JavaSE开发或者Android开发时,经常会给按钮加监听器,当点击这个按钮就会触发监听事件,调用onClick方法,本质是方法回调。在JavaWeb的Listener也是这么个原理,但是它监听的内容不同,它可以监听Application、Session、Request对象,当这些对象发生变化就会调用对应的监听方法。 
应用域监听: 
Ø ServletContext(监听Application)

生命周期监听:ServletContextListener,它有两个方法,一个在出生时调用,一个在死亡时调用;

void contextInitialized(ServletContextEvent sce):创建Servletcontext时

void contextDestroyed(ServletContextEvent sce):销毁Servletcontext时

属性监听:ServletContextAttributeListener,它有三个方法,一个在添加属性时调用,一个在替换属性时调用,最后一个是在移除属性时调用。

void attributeAdded(ServletContextAttributeEvent event):添加属性时;

void attributeReplaced(ServletContextAttributeEvent event):替换属性时;

void attributeRemoved(ServletContextAttributeEvent event):移除属性时;

Ø HttpSession(监听Session)

生命周期监听:HttpSessionListener,它有两个方法,一个在出生时调用,一个在死亡时调用;

voidsessionCreated(HttpSessionEvent se):创建session时

void sessionDestroyed(HttpSessionEvent se):销毁session时

属性监听:HttpSessioniAttributeListener,它有三个方法,一个在添加属性时调用,一个在替换属性时调用,最后一个是在移除属性时调用。

void attributeAdded(HttpSessionBindingEvent event):添加属性时;

void attributeReplaced(HttpSessionBindingEvent event):替换属性时

void attributeRemoved(HttpSessionBindingEvent event):移除属性时

Ø ServletRequest(监听Request)

生命周期监听:ServletRequestListener,它有两个方法,一个在出生时调用,一个在死亡时调用;

voidrequestInitialized(ServletRequestEvent sre):创建request时

void requestDestroyed(ServletRequestEvent sre):销毁request时

属性监听:ServletRequestAttributeListener,它有三个方法,一个在添加属性时调用,一个在替换属性时调用,最后一个是在移除属性时调用。

voidattributeAdded(ServletRequestAttributeEvent srae):添加属性时

void attributeReplaced(ServletRequestAttributeEvent srae):替换属性时

void attributeRemoved(ServletRequestAttributeEvent srae):移除属性时

感知Session监听: 
1:HttpSessionBindingListener监听 
⑴在需要监听的实体类实现HttpSessionBindingListener接口 
⑵重写valueBound()方法,这方法是在当该实体类被放到Session中时,触发该方法 
⑶重写valueUnbound()方法,这方法是在当该实体类从Session中被移除时,触发该方法 
2:HttpSessionActivationListener监听 
⑴在需要监听的实体类实现HttpSessionActivationListener接口 
⑵重写sessionWillPassivate()方法,这方法是在当该实体类被序列化时,触发该方法 
⑶重写sessionDidActivate()方法,这方法是在当该实体类被反序列化时,触发该方法

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/web/992304.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存