What are the functions of interceptors, filters, and listeners?

Hello everyone, I am the 11th student of the Chengdu Branch of the IT Cultivation Institute. I am an honest and kind java programmer. Today I will share with you, what are filters, interceptors, and listeners.

1. Background introduction

When we learn to build javaWeb projects, or inevitably develop, we will touch or use Filter, Interceptor and Listener to achieve some functions.

2. Knowledge analysis

1). What is Filter

The filter in servlet is a server-side program that implements the javax.servlet.Filter interface. The main purpose is to filter character encoding and make some business logic judgments. The working principle is that as long as you configure the client request to be intercepted in the web.xml file, it will help you intercept the request. At this time, you can uniformly set the encoding for the request or response (Request, Response) to simplify the operation; At the same time, it can also make logical judgments, such as whether the user has logged in, whether he has permission to access the page, and so on. It is started when your web application starts, it is initialized only once, and related requests can be intercepted later, and it is destroyed only when your web application is stopped or redeployed.

2). What is a listener

Now let's talk about Servlet's Listener, which is a server-side program that implements the javax.servlet.ServletContextListener interface. It is also started when the web application is started, initialized only once, and destroyed when the web application stops. The main functions are: Do some initialization content addition work, set some basic content, such as some parameters or some fixed objects and so on.

3). What is an interceptor

Interceptors are applied in aspect-oriented programming, which is to call a method before your service or a method, or call a method after the method. It is a reflection mechanism based on JAVA.

3. Frequently Asked Questions

What is the difference between filters, interceptors, and listeners?

(1). Analysis from the principle


a. Interceptors are based on java's reflection mechanism, while filters are based on function callbacks


b. The filter depends on the servlet container, while the interceptor does not depend on the servlet container


c. Interceptors can only work on action requests, while filters can work on almost all requests


d. Interceptors can access objects in the action context and value stack, while filters cannot


e. During the life cycle of the action, the interceptor can be called multiple times, while the filter can only be called once when the container is initialized

(2). Analysis from the application


a. When you have a bunch of things, you only want to choose certain things that meet your requirements. The tool for defining these requirements is the filter.


b. When a process is in progress, you want to intervene in its progress, or even terminate its progress, which is what interceptors do.


c. When an event occurs, you want to get detailed information about the event, but do not want to interfere with the process of the event itself, so you need to use a listener.

That's it for today's sharing, everyone is welcome to like, forward, leave a message, and make a brick~

Skill Tree.IT Cultivation Institute "We believe that everyone can become an engineer. From now on, find a senior brother to guide you, control the rhythm of your own learning, and no longer get confused on the way of learning."

Here is the skill tree. IT Cultivation Institute, where thousands of seniors have found their own learning routes, with transparent learning, visible growth, and one-to-one free guidance from seniors. Come and learn with me~ My invitation code: 16318412, or you can click this link directly: http://www.jnshu.com/login/1/16318412


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324692051&siteId=291194637
Recommended