filter and listener to learn the basics (a)

It for filter and listener also a beginner, the main purpose of writing this article is to facilitate their understanding of the content filter and listener, some not so professional and comprehensive description, please bear with me.

A, fileter Overview

That filterfilterIt is one of Javaweb three components, mainly for filtering resources , but resources are mainly reflected in the filterIntercept processingRequests and responses of the client and the server.
Because fileter can intercept requests and responses, can be done by some common filter operation, such as login verification, testing and so sensitive character.

Two, filter the simple use

(1) Create a filter

Create a filter must first create a class, and then go to achieveFilter interface, And finally replication method corresponding side of the interface.
In IDEA which can directly select a new filter, as shown below:
Here Insert Picture Description

Examples of simple code (2) filter is

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

@WebFilter("/*")  //"/*表示访问所有资源之间,都会执行改过滤器"
public class FilterTest1 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("filterTest1被执行了");

        //在进行相应处理后,对请求或相应放行
        filterChain.doFilter(servletRequest,servletResponse);

    }

    @Override
    public void destroy() {

    }

}

It should be noted that, in == @ WebFilterThe default value is inside the comment== intercept path, here is the "/ *" means to intercept all.
Further after intercepting the request or response to the corresponding treatment, and then consider whether to allow the request or response by.

Three, listener Overview

That listenerMonitorIt is also one of the three components of Javaweb. Java GUI programming in thereEvent ListenersThe concept here is similar, can be handled by an event listener, and here the treatment is primarily aimed at HttpServletRequest, HttpSession and ServletContext.
These objects can be attained by monitoring the preparation process target listeners, and make a corresponding operation according to its properties.

Four, listener simple usage

Depending on the objects being treated listener, the interface created by the listener to achieve different, here toServletContextListener InterfaceFor example, you can monitor the life cycle of ServletContext object is actually listening lifecycle of Web applications.
Corresponding code are shown below:

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

@WebListener()
public class ListenerTest1 implements ServletContextListener{

    /**
     * 监听ServletContext对象创建的。ServletContext对象服务器启动后自动创建。
     *
     * 在服务器启动后自动调用
     */
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {

        System.out.println("ServletContext对象被创建了。。。");
    }

    /**
     * 在服务器关闭后,ServletContext对象被销毁。当服务器正常关闭后该方法被调用
     */
    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        System.out.println("ServletContext对象被销毁了。。。");
    }
}

Reference material

Here Insert Picture Description
2020.02.17

Published 52 original articles · won praise 59 · views 6820

Guess you like

Origin blog.csdn.net/ataraxy_/article/details/104357873