当前位置:网站首页>Filters, interceptors, listeners
Filters, interceptors, listeners
2022-07-28 03:56:00 【Program three two lines】
filter 、 Interceptor 、 The monitor is tomcat Position in container

Concept
1、servlet:servlet It's a kind of running on the server side java Applications , Platform - and protocol-independent , It can generate web It works in the middle layer between the client request and the server response ;
2、filter:filter It's a reusable piece of code , It can be used to convert HTTP request , Response and header information . It cannot generate a request or response , It just modifies the request or response to a resource ;
3、listener: Monitor , adopt listener Can be strong web An execution action in the server , And make corresponding response according to its requirements . Is in the application,session,request Three objects are functional components that automatically execute code when creating, dying, or adding, modifying, or deleting properties ;
4、interceptor: Interceptors are more refined applications of filters , It can be applied not only to service Before and after the method can also be applied to before and after other methods Interceptor ;
The difference between filter and interceptor
1、 The interceptor is based on java The reflection mechanism of , And filters are based on function callbacks .
2、 Interceptors don't rely on servlet Containers , The filter depends on servlet Containers .
3、 Interceptors can only be used to action The request works , Filters work on almost all requests .
4、 Interceptors can access action Context 、 Value the objects in the stack , And the filter can't access .
5、 stay action In the life cycle of , Interceptors can be called many times , The filter can only be called once when the container is initialized .
6、 Interceptors can get IOC Each of the containers bean, And filters don't work , That's important , Inject a... Into the interceptor service, You can call the business logic
filter 、 Interceptor 、 The life cycle of the listener
1.servlet
servle The life cycle of starts with being loaded web In the memory of the server , And in web Service termination or reload servlet It's over ;
servlet Once loaded web The server , Generally not from web Delete... From server memory , until web Server down ;
Load : Load when starting the server servlet Example ;
initialization :web When the server receives the request , Or start at some point in between , call init();
call : From the first visit to many subsequent visits , Only call doGet() or dopost)() Method ;
The destruction ; Called when the server is stopped destroy() Method , Destroy instance ;
2.Filter
Need to achieve javax.servlet Bag Filter Three methods of interface init(),doFilter(),destroy();
load : Load an instance of the filter when starting the server , And call init() Method ;
call : Only methods are called each time a request is made doFilter() To deal with ;
The destruction : Call before the server shuts down destroy() Method , Destroy instance ;
3.Listener
web.xml The loading order of is :context-param->listener->filter->servlet
4.Interceptor
Initialize the interceptor after loading the configuration file , When someone is right action At the request of , call interceptor Method , Finally, it is destroyed according to the stop of the server
duty
1、Servlet
Create and return a complete... Containing dynamic content based on the nature of the customer request html page
Create a that can be embedded into an existing html Part of the page html page (html fragment )
Read the hidden data sent by the client
Read the display data sent by the client
With other server resources ( Including databases and java Applications for ) communicate
2.Filter
filter Can arrive at a request servlet Pre process user requests , You can also leave servlet Time processing http Respond to :
In execution servlet Before , First, execute filter Program , And do some pretreatment for it ;
stay servlet Intercept after being called servlet Implementation
4.Listener
Listening container 、 application 、 Creation and destruction of requests , And set the attribute value during
servlet2.4 The specification provides 8 individual listener Interface , It can be divided into three categories , They are as follows ;
The first category : And HttpContext Relevant listener Interface , Include :ServletContextListener、ServletContextAttributeListener
The second category : And HttpSession Relevant listner Interface . Include :HttpSessionListener、HttpSessionAttributeListener、 HttpSessionBindingListener、 HttpSessionActivationListener
The third category : And ServletRequest Relevant Listener Interface , Include :ServletRequestListener、ServletRequestAttributeListener
5.Interceptor
Similar to filters , Through layers of interception , Handle user requests and responses ;
————————————————
Copyright notice : This paper is about CSDN Blogger 「 o king The way of architecture 」 The original article of , follow CC 4.0 BY-SA Copyright agreement , For reprint, please attach the original source link and this statement .
Link to the original text :https://blog.csdn.net/Tomcat_king/article/details/121633153
边栏推荐
- [openvx] VX for basic use of objects_ image
- Prefix-Tuning: Optimizing Continuous Prompts for Generation
- Developing rc522 module based on c8t6 chip to realize breathing lamp
- [错题]Concatenation
- R notes mice
- Leetcode skimming: dynamic programming 08 (segmentation and subsets)
- 数据挖掘-02
- Summary of static blog building tools
- 超好用的 PC 端长截图工具
- 常用的弱网测试工具
猜你喜欢

简单、好用的性能测试工具推荐
![Error no matching function for call to 'std:: exception:: exception (const char [15])' problem solving](/img/d1/2fbdf42cf60b5382b5a35c64b172b9.png)
Error no matching function for call to 'std:: exception:: exception (const char [15])' problem solving

MySQL是怎么保证高可用的

MySQL Basics (create, manage, add, delete, and modify tables)

超好用的 PC 端长截图工具

CH340 RTS DTR引脚编程驱动OLED

Selenium--WEB自动化测试工具

numeric_ Limits the range and related attributes of each data type learned

常用的弱网测试工具

LightPicture – 精致图床系统
随机推荐
程序人生 | 测试工程师还只会点点点?7个捷径教给你快速学习新技术...
How does MySQL ensure high availability
pip-script. py‘ is not present Verifying transaction: failed
Screenshot of deepstream detection results
[P4] check the differences between the two historical versions of the library file
Data mining-02
Practical scripts of mangopapa (contents)
Day08 redis的基础知识
[wrong question]mocha and railgun
Leetcode skimming: dynamic programming 08 (segmentation and subsets)
Linux - MySQL advanced (day19)
cookie与Session
Greed 122. The best time to buy and sell stocks II
After 95, Alibaba P7 published the payroll: it's really heartbreaking
Interview essential skills: SQL query special training!
[openvx] VX for basic use of objects_ distribution
Advanced Mathematics (Seventh Edition) Tongji University exercises 3-4 personal solutions (first 8 questions)
Advanced Mathematics (Seventh Edition) Tongji University exercises 3-6 personal solutions
Interface automation test, complete introduction
Detailed explanation of pointer written test questions (C language)