Filters, listeners, and interceptors are used in many development scenarios, and today we will talk about how they are used in daily development.
Concepts and usage scenarios
The listener
A Listener is a special class defined in the Servlet specification. Listen for creation and destruction events of domain objects such as servletContext, HttpSession, and servletRequest. Listen for the event that a property of a domain object has been modified. Perform necessary operations before and after an event occurs.
Implementation: implementing an interface javax.mail. Servlet. HTTP. HttpSessionListener
It can be mainly used in the following aspects:
Collect statistics on the number of online users
Load initialization information when the system starts
Counting website visits
Record the user access path.
The filter
Filter is the most practical Servlet technology, Web developers through Filter technology, Web server management of all Web resources. Filters are preprocessed after the request enters the Tomcat container, but before the request enters the servlet. The return at the end of the request is also after the servlet has finished processing it and before it is returned to the front end.
Such as Jsp, Servlet, static image files or static HTML files for interception, so as to achieve some special functions.
For example, the implementation of urL-level access control, filtering sensitive terms, compression of response information and other advanced functions.
It is used primarily for pre-processing user requests and can also be used for post-processing HttpServletResponse. The full flow of using a Filter: The Filter preprocesses the user request, passes the request to a Servlet for processing and generating the response, and finally the Filter postprocesses the server response. The filter only works around the servlet, so it can’t catch exceptions, get bean objects, and so on
Implementation: javax.servlet.filter interface
The interceptor
Interceptors are used to intercept a method or field before it is accessed and then add some action before or after it. Such as logging, security, etc. Most interceptor methods are implemented by dynamic proxy. You can use it to verify permissions, or to determine whether the user is logged in, or to determine whether the current time is the time to buy tickets like 12306. Generally, in project development, there is basically a place for exception blocking and handling. In contrast, we can see that the interceptor can do all the things the filter can do, and the filter can not be customized for the two interceptors.
Implementation: org. Springframework. Web. Servlet. HandlerInterceptor
The three contrast
Interceptors and filters: Before Filtering -> Before Blocking -> Action/Controller Execution -> After Blocking -> After Filtering
To give you a better understanding, here are some pictures from the Internet:
In actual combat
The project continues to use the project used in the previous article.
Adding filters
Custom filters can also be configured using Servlet3.0 annotations. Step 3 ** @webfilter ** is 3.0 annotations
1) Add ** @servletComponentScan ** to start class
2) Add a new Filter class, implements the corresponding interface
3) @webFilter marks a class as filter and is scanned by Spring
**urlPatterns: ** Intercepts rules and supports regex
Resp. SendRedirect (“/index.html”); Scenario: Permission control and user login (non-front-end and back-end separation scenarios)
Adding listeners
Listeners are associated with the listener design pattern. It’s like when you’re taking an exam, the examiners are watching you all the time, they’re watching you all the time, they’re watching you all the time, and they’re going to catch you if you cheat.
Adding interceptors
The preHandle method is called before our Controller method executes.
Adding a Configuration Class
Test class, write a controller to test
That’s it for today’s listeners, filters, and interceptors. I suggest you try it manually.
The paper come zhongjue shallow, and must know this to practice.
Follow me for replicable code (” Java Backend Technology full stack “) and other surprises