当前位置:网站首页>Three web components (servlet, filter, listener)
Three web components (servlet, filter, listener)
2022-04-23 11:03:00 【yueseck】
Catalog
4.3 Filter configuration details
One 、Servlet
Click here to introduce the past servlet
Two 、Filter
1. Concept
* Filters in life : Water purifier , air cleaner , bandit 、
* web Filter in : When accessing the server's resources , Filters can intercept requests , Complete some special functions .
* The function of the filter :
* Generally used to complete general operations . Such as : validate logon 、 Unified coding 、 Sensitive character filtering ...
2. step
1. Define a class , Implementation interface Filter
2. Replication method
3. Configure interception path
1. web.xml
2. annotation
3. Code
@WebFilter("/*")// Before accessing all resources , Will execute the filter
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 Carried out ....");
// release
filterChain.doFilter(servletRequest,servletResponse);
}
@Override
public void destroy() {
}
}
4. Filter details
4.1web.xml To configure
<filter>
<filter-name>demo1</filter-name>
<filter-class>cn.itcast.web.filter.FilterDemo1</filter-class>
</filter>
<filter-mapping>
<filter-name>demo1</filter-name>
<!-- Intercept path -->
<url-pattern>/*</url-pattern>
</filter-mapping>
4.2 Filter execution process
1. Perform filter
2. Resources after implementation of release
3. Come back and execute the code below the filter release code
4.3 Filter configuration details
* Intercept path configuration :
1. Specific resource paths : /index.jsp Only visit index.jsp Resource time , The filter will be executed
2. Intercept Directory : /user/* visit /user All resources under , Filters are executed
3. Suffix blocking : *.jsp Access all suffixes named jsp Resource time , Filters are executed
4. Intercept all resources :/* When accessing all resources , Filters are executed
* Interception mode configuration : How resources are accessed
* Annotation configuration :
* Set up dispatcherTypes attribute
1. REQUEST: The default value is . Browser requests resources directly
2. FORWARD: Forward access to resources
3. INCLUDE: Contains access to resources
4. ERROR: Wrong jump resource
5. ASYNC: Asynchronous access to resources
* web.xml To configure
* Set up <dispatcher></dispatcher> Labels can be
5. Filter chain
* Execution order : If there are two filters : filter 1 And filters 2
1. filter 1
2. filter 2
3. Resource execution
4. filter 2
5. filter 1
* The filter sequence problem :
1. Annotation configuration : Compare according to the string comparison rules of class names , If it's worth less, execute it first
* Such as : AFilter and BFilter,AFilter Just do it first .
2. web.xml To configure : <filter-mapping> Who is defined above , Who will execute first
3、 ... and 、Listener
* Concept :web One of the three components of .
* Event monitoring mechanism
* event : One more thing
* Event source : Where it happened
* Monitor : An object
* Register to listen : Put the event 、 Event source 、 Listeners are bound together . When an event occurs on the event source , Execute listener code
* ServletContextListener: monitor ServletContext Object creation and destruction
* Method :
* void contextDestroyed(ServletContextEvent sce) :ServletContext The method is called before the object is destroyed
* void contextInitialized(ServletContextEvent sce) :ServletContext The method is called after the object is created
* step :
1. Define a class , Realization ServletContextListener Interface
2. Replication method
3. To configure
1. web.xml
<listener>
<listener-class>cn.itcast.web.listener.ContextLoaderListener</listener-class>
* Specify initialization parameters <context-param>
2. annotation :
* @WebListener
版权声明
本文为[yueseck]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231053377300.html
边栏推荐
- Manjaro installation and configuration (vscode, wechat, beautification, input method)
- MBA-day5数学-应用题-工程问题
- Gets the current time in character format
- 语雀文档编辑器将开源:始于但不止于Markdown
- MySQL分区表实现按月份归类
- Solutions to common problems in visualization (IX) background color
- Jupyter lab top ten high productivity plug-ins
- ConstraintLayout布局
- 【leetcode】199. Right view of binary tree
- 【leetcode】102. Sequence traversal of binary tree
猜你喜欢
Notes on concurrent programming of vegetables (IX) asynchronous IO to realize concurrent crawler acceleration
Learning Notes 6 - Summary of several deep learning convolutional neural networks
【leetcode】102. Sequence traversal of binary tree
Constraintlayout layout
Reading integrity monitoring techniques for vision navigation systems - 5 Results
Google Earth engine (GEE) - scale up the original image (taking Hainan as an example)
使用zerotier让异地设备组局域网
Typora operation skill description (I) md
C语言之结构体(进阶篇)
Introduction to data analysis 𞓜 kaggle Titanic mission (III) - > explore data analysis
随机推荐
Manjaro installation and configuration (vscode, wechat, beautification, input method)
ConstraintLayout布局
SWAT—Samba WEB管理工具介绍
STM32接电机驱动,杜邦线供电,然后反烧问题
UEditor之——图片上传组件大小4M的限制
VIM usage
Visualization Road (10) detailed explanation of segmentation canvas function
Cve-2019-0708 vulnerability exploitation of secondary vocational network security 2022 national competition
使用zerotier让异地设备组局域网
主流手机分辨率与尺寸
Esp32 learning - add folder to project
Constraintlayout layout
Restful、SOAP、RPC、SOA、微服务之间的区别
SVN的使用:
MySQL分区表实现按月份归类
关于JUC三大常用辅助类
Simple thoughts on the design of a microblog database
Mba-day5 Mathematics - application problems - engineering problems
高价买来的课程,公开了!phper资料分享
Strongest date regular expression